micro/transport/mock/mock_test.go

61 lines
1.1 KiB
Go
Raw Normal View History

2016-03-18 21:41:26 +03:00
package mock
import (
"testing"
"github.com/micro/go-micro/transport"
)
func TestTransport(t *testing.T) {
tr := NewTransport()
// bind / listen
l, err := tr.Listen("localhost:8080")
if err != nil {
t.Fatalf("Unexpected error listening %v", err)
}
defer l.Close()
// accept
go func() {
if err := l.Accept(func(sock transport.Socket) {
for {
var m transport.Message
if err := sock.Recv(&m); err != nil {
return
}
t.Logf("Server Received %s", string(m.Body))
if err := sock.Send(&transport.Message{
Body: []byte(`pong`),
}); err != nil {
return
}
}
}); err != nil {
t.Fatalf("Unexpected error accepting %v", err)
}
}()
// dial
c, err := tr.Dial("localhost:8080")
if err != nil {
t.Fatalf("Unexpected error dialing %v", err)
}
defer c.Close()
// send <=> receive
for i := 0; i < 3; i++ {
if err := c.Send(&transport.Message{
Body: []byte(`ping`),
}); err != nil {
return
}
var m transport.Message
if err := c.Recv(&m); err != nil {
return
}
t.Logf("Client Received %s", string(m.Body))
}
}