ttrpc: better test for concurrency

Improve the test to conduct two concurrent calls. The test server now
just doubles the input and we use a unix socket to better represent what
will be used in production.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-11-27 21:19:57 -08:00
parent bdb2ab7a81
commit 5e1096a4c2
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
@ -44,12 +45,25 @@ func (r *testPayload) String() string { return fmt.Sprintf("%+#v", r) }
func (r *testPayload) ProtoMessage() {} func (r *testPayload) ProtoMessage() {}
// testingServer is what would be implemented by the user of this package. // testingServer is what would be implemented by the user of this package.
type testingServer struct { type testingServer struct{}
payload *testPayload
}
func (s *testingServer) Test(ctx context.Context, req *testPayload) (*testPayload, error) { func (s *testingServer) Test(ctx context.Context, req *testPayload) (*testPayload, error) {
return s.payload, nil return &testPayload{Foo: strings.Repeat(req.Foo, 2)}, nil
}
// registerTestingService mocks more of what is generated code. Unlike grpc, we
// register with a closure so that the descriptor is allocated only on
// registration.
func registerTestingService(srv *Server, svc testingService) {
srv.Register(serviceName, map[string]Method{
"Test": func(ctx context.Context, unmarshal func(interface{}) error) (interface{}, error) {
var req testPayload
if err := unmarshal(&req); err != nil {
return nil, err
}
return svc.Test(ctx, &req)
},
})
} }
func init() { func init() {
@ -60,29 +74,15 @@ func init() {
func TestServer(t *testing.T) { func TestServer(t *testing.T) {
var ( var (
ctx = context.Background() ctx = context.Background()
server = NewServer() server = NewServer()
expectedResponse = &testPayload{Foo: "baz"} testImpl = &testingServer{}
testImpl = &testingServer{payload: expectedResponse}
) )
// more mocking of what is generated code. Unlike grpc, we register with a
// closure so that the descriptor is allocated only on registration.
registerTestingService := func(srv *Server, svc testingService) {
srv.Register(serviceName, map[string]Method{
"Test": func(ctx context.Context, unmarshal func(interface{}) error) (interface{}, error) {
var req testPayload
if err := unmarshal(&req); err != nil {
return nil, err
}
return svc.Test(ctx, &req)
},
})
}
registerTestingService(server, testImpl) registerTestingService(server, testImpl)
listener, err := net.Listen("tcp", ":0") addr := "\x00" + t.Name()
listener, err := net.Listen("unix", addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -91,24 +91,48 @@ func TestServer(t *testing.T) {
go server.Serve(listener) go server.Serve(listener)
defer server.Shutdown(ctx) defer server.Shutdown(ctx)
conn, err := net.Dial("tcp", listener.Addr().String()) conn, err := net.Dial("unix", addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer conn.Close() defer conn.Close()
client := newTestingClient(NewClient(conn)) client := newTestingClient(NewClient(conn))
tp := &testPayload{ const calls = 2
Foo: "bar", results := make(chan callResult, 2)
go roundTrip(ctx, t, client, "bar", results)
go roundTrip(ctx, t, client, "baz", results)
for i := 0; i < calls; i++ {
result := <-results
if !reflect.DeepEqual(result.received, result.expected) {
t.Fatalf("unexpected response: %+#v != %+#v", result.received, result.expected)
}
} }
}
type callResult struct {
input *testPayload
expected *testPayload
received *testPayload
}
func roundTrip(ctx context.Context, t *testing.T, client *testingClient, value string, results chan callResult) {
t.Helper()
var (
tp = &testPayload{
Foo: "bar",
}
)
resp, err := client.Test(ctx, tp) resp, err := client.Test(ctx, tp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !reflect.DeepEqual(resp, expectedResponse) { results <- callResult{
t.Fatalf("unexpected response: %+#v != %+#v", resp, expectedResponse) input: tp,
expected: &testPayload{Foo: strings.Repeat(tp.Foo, 2)},
received: resp,
} }
} }