2019-01-14 15:27:25 +00:00
|
|
|
package memory
|
2016-03-18 18:41:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/transport"
|
2016-03-18 18:41:26 +00:00
|
|
|
)
|
|
|
|
|
2019-01-14 15:27:25 +00:00
|
|
|
func TestMemoryTransport(t *testing.T) {
|
2016-03-18 18:41:26 +00:00
|
|
|
tr := NewTransport()
|
|
|
|
|
|
|
|
// bind / listen
|
2019-07-18 00:18:40 +03:00
|
|
|
l, err := tr.Listen("127.0.0.1:8080")
|
2016-03-18 18:41:26 +00:00
|
|
|
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
|
2019-07-18 00:18:40 +03:00
|
|
|
c, err := tr.Dial("127.0.0.1:8080")
|
2016-03-18 18:41:26 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-06-01 15:22:27 +01:00
|
|
|
|
|
|
|
func TestListener(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
|
|
|
|
|
|
|
// bind / listen on random port
|
|
|
|
l, err := tr.Listen(":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// try again
|
|
|
|
l2, err := tr.Listen(":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l2.Close()
|
|
|
|
|
|
|
|
// now make sure it still fails
|
|
|
|
l3, err := tr.Listen(":8080")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l3.Close()
|
|
|
|
|
|
|
|
if _, err := tr.Listen(":8080"); err == nil {
|
|
|
|
t.Fatal("Expected error binding to :8080 got nil")
|
|
|
|
}
|
|
|
|
}
|