2021-02-12 16:33:16 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMemoryTransport(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
|
|
|
ctx := context.Background()
|
|
|
|
// bind / listen
|
|
|
|
l, err := tr.Listen(ctx, "127.0.0.1:8080")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2021-09-30 21:13:13 +03:00
|
|
|
cherr := make(chan error, 1)
|
2021-02-12 16:33:16 +03:00
|
|
|
// accept
|
|
|
|
go func() {
|
2021-09-30 21:13:13 +03:00
|
|
|
if nerr := l.Accept(func(sock Socket) {
|
2021-02-12 16:33:16 +03:00
|
|
|
for {
|
|
|
|
var m Message
|
2021-09-30 21:13:13 +03:00
|
|
|
if rerr := sock.Recv(&m); rerr != nil {
|
|
|
|
cherr <- rerr
|
2021-02-12 16:33:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(os.Getenv("INTEGRATION_TESTS")) == 0 {
|
|
|
|
t.Logf("Server Received %s", string(m.Body))
|
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
if cerr := sock.Send(&Message{
|
2021-02-12 16:33:16 +03:00
|
|
|
Body: []byte(`pong`),
|
2021-04-27 08:32:47 +03:00
|
|
|
}); cerr != nil {
|
2021-09-30 21:13:13 +03:00
|
|
|
cherr <- cerr
|
2021-02-12 16:33:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 21:13:13 +03:00
|
|
|
}); nerr != nil {
|
|
|
|
cherr <- err
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// dial
|
|
|
|
c, err := tr.Dial(ctx, "127.0.0.1:8080")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error dialing %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2021-09-30 21:13:13 +03:00
|
|
|
select {
|
|
|
|
case err := <-cherr:
|
|
|
|
t.Fatal(err)
|
|
|
|
default:
|
|
|
|
// send <=> receive
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if err := c.Send(&Message{
|
|
|
|
Body: []byte(`ping`),
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var m Message
|
|
|
|
if err := c.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(os.Getenv("INTEGRATION_TESTS")) == 0 {
|
|
|
|
t.Logf("Client Received %s", string(m.Body))
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListener(t *testing.T) {
|
|
|
|
tr := NewTransport()
|
|
|
|
ctx := context.Background()
|
|
|
|
// bind / listen on random port
|
|
|
|
l, err := tr.Listen(ctx, ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// try again
|
|
|
|
l2, err := tr.Listen(ctx, ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l2.Close()
|
|
|
|
|
|
|
|
// now make sure it still fails
|
|
|
|
l3, err := tr.Listen(ctx, ":8080")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error listening %v", err)
|
|
|
|
}
|
|
|
|
defer l3.Close()
|
|
|
|
|
|
|
|
if _, err := tr.Listen(ctx, ":8080"); err == nil {
|
|
|
|
t.Fatal("Expected error binding to :8080 got nil")
|
|
|
|
}
|
|
|
|
}
|