2020-12-29 17:49:26 +02:00
|
|
|
package transport
|
2016-03-18 20:41:26 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-09 13:05:46 +02:00
|
|
|
"os"
|
2016-03-18 20:41:26 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-01-14 17:27:25 +02:00
|
|
|
func TestMemoryTransport(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tr := NewMemoryTransport()
|
2016-03-18 20:41:26 +02:00
|
|
|
|
|
|
|
// bind / listen
|
2019-07-17 23:18:40 +02:00
|
|
|
l, err := tr.Listen("127.0.0.1:8080")
|
2016-03-18 20:41:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// accept
|
|
|
|
go func() {
|
2020-12-29 17:49:26 +02:00
|
|
|
if err := l.Accept(func(sock Socket) {
|
2016-03-18 20:41:26 +02:00
|
|
|
for {
|
2020-12-29 17:49:26 +02:00
|
|
|
var m Message
|
2016-03-18 20:41:26 +02:00
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-09 13:05:46 +02:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("Server Received %s", string(m.Body))
|
|
|
|
}
|
2020-12-29 17:49:26 +02:00
|
|
|
if err := sock.Send(&Message{
|
2016-03-18 20:41:26 +02:00
|
|
|
Body: []byte(`pong`),
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("Unexpected error accepting %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// dial
|
2019-07-17 23:18:40 +02:00
|
|
|
c, err := tr.Dial("127.0.0.1:8080")
|
2016-03-18 20:41:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error dialing %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
// send <=> receive
|
|
|
|
for i := 0; i < 3; i++ {
|
2020-12-29 17:49:26 +02:00
|
|
|
if err := c.Send(&Message{
|
2016-03-18 20:41:26 +02:00
|
|
|
Body: []byte(`ping`),
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-29 17:49:26 +02:00
|
|
|
var m Message
|
2016-03-18 20:41:26 +02:00
|
|
|
if err := c.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-09 13:05:46 +02:00
|
|
|
if len(os.Getenv("IN_TRAVIS_CI")) == 0 {
|
|
|
|
t.Logf("Client Received %s", string(m.Body))
|
|
|
|
}
|
2016-03-18 20:41:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-01 16:22:27 +02:00
|
|
|
|
|
|
|
func TestListener(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tr := NewMemoryTransport()
|
2016-06-01 16:22:27 +02:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|