Add transport comms tests
This commit is contained in:
parent
4da744ebde
commit
6028c2f9e8
@ -7,7 +7,17 @@ import (
|
||||
"github.com/micro/go-micro/transport"
|
||||
)
|
||||
|
||||
func TestHTTPTransport_PortRange(t *testing.T) {
|
||||
func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
|
||||
parts := strings.Split(lsn.Addr(), ":")
|
||||
port := parts[len(parts)-1]
|
||||
|
||||
if port != expected {
|
||||
lsn.Close()
|
||||
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPTransportPortRange(t *testing.T) {
|
||||
tp := transport.NewTransport([]string{})
|
||||
|
||||
lsn1, err := tp.Listen(":44444-44448")
|
||||
@ -32,12 +42,62 @@ func TestHTTPTransport_PortRange(t *testing.T) {
|
||||
lsn2.Close()
|
||||
}
|
||||
|
||||
func expectedPort(t *testing.T, expected string, lsn transport.Listener) {
|
||||
parts := strings.Split(lsn.Addr(), ":")
|
||||
port := parts[len(parts)-1]
|
||||
func TestHTTPTransportCommunication(t *testing.T) {
|
||||
tr := transport.NewTransport([]string{})
|
||||
|
||||
if port != expected {
|
||||
lsn.Close()
|
||||
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
|
||||
l, err := tr.Listen(":0")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected listen err: %v", err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
fn := func(sock transport.Socket) {
|
||||
defer sock.Close()
|
||||
|
||||
for {
|
||||
var m transport.Message
|
||||
if err := sock.Recv(&m); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.Logf("Successfully received %+v", m)
|
||||
|
||||
if err := sock.Send(&m); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := l.Accept(fn); err != nil {
|
||||
t.Errorf("Unexpected accept err: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
c, err := tr.Dial(l.Addr())
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected dial err: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
m := transport.Message{
|
||||
Header: map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
Body: []byte(`{"message": "Hello World"}`),
|
||||
}
|
||||
|
||||
if err := c.Send(&m); err != nil {
|
||||
t.Errorf("Unexpected send err: %v", err)
|
||||
}
|
||||
|
||||
var rm transport.Message
|
||||
|
||||
if err := c.Recv(&rm); err != nil {
|
||||
t.Errorf("Unexpected recv err: %v", err)
|
||||
}
|
||||
|
||||
if string(rm.Body) != string(m.Body) {
|
||||
t.Errorf("Expected %v, got %v", m.Body, rm.Body)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user