35 lines
791 B
Go
35 lines
791 B
Go
package zmqtest
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/phayes/freeport"
|
|
"github.com/stretchr/testify/require"
|
|
"src.dualinventive.com/go/dinet"
|
|
)
|
|
|
|
// New creates a ZMQ connection to a new url. The connection and url is returned.
|
|
func New(t *testing.T) (dinet.Conn, string) {
|
|
url := getZMQURI(t)
|
|
return getPubConn(t, url), url
|
|
}
|
|
|
|
// getZMQURI returns a new ZMQURI
|
|
func getZMQURI(t *testing.T) string {
|
|
port, err := freeport.GetFreePort()
|
|
require.Nil(t, err)
|
|
|
|
return fmt.Sprintf("tcp://127.0.0.1:%d", port)
|
|
}
|
|
|
|
// getPubConn creates a new publish connection to the given uri
|
|
func getPubConn(t *testing.T, uri string) dinet.Conn {
|
|
conn, err := dinet.NewConn(dinet.TransportZmq)
|
|
require.Nil(t, err)
|
|
|
|
err = conn.Connect(uri + "?type=pub&bind=true")
|
|
require.Nil(t, err)
|
|
return conn
|
|
}
|