mgrpc: mock out workflow for generated code

The server unit test now manually mocks much of the generated code for a
given service, including back registration of the service. We avoid
having a package-level global descriptor in favor of a closures for the
handler dispatch function.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-11-14 18:23:40 -08:00
parent 538d13ce1b
commit 71e9170d05
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F

View File

@ -4,16 +4,40 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"reflect"
"testing" "testing"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
) )
// var serverMethods = map[string]Handler{ const serviceName = "testService"
// "Create": HandlerFunc(func(ctx context.Context, req interface{}) (interface{}, error) {
// }, // testingService is our prototype service definition for use in testing the full model.
// } //
// Typically, this is generated. We define it here to ensure that that package
// primitive has what is required for generated code.
type testingService interface {
Test(ctx context.Context, req *testPayload) (*testPayload, error)
}
type testingClient struct {
client *Client
}
func newTestingClient(client *Client) *testingClient {
return &testingClient{
client: client,
}
}
func (tc *testingClient) Test(ctx context.Context, req *testPayload) (*testPayload, error) {
resp, err := tc.client.Call(ctx, serviceName, "Test", req)
if err != nil {
return nil, err
}
return resp.(*testPayload), nil
}
type testPayload struct { type testPayload struct {
Foo string `protobuf:"bytes,1,opt,name=foo,proto3"` Foo string `protobuf:"bytes,1,opt,name=foo,proto3"`
@ -23,23 +47,40 @@ func (r *testPayload) Reset() { *r = testPayload{} }
func (r *testPayload) String() string { return fmt.Sprintf("%+#v", r) } 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.
type testingServer struct {
payload *testPayload
}
func (s *testingServer) Test(ctx context.Context, req *testPayload) (*testPayload, error) {
return s.payload, nil
}
func init() { func init() {
proto.RegisterType((*testPayload)(nil), "testpayload") proto.RegisterType((*testPayload)(nil), "testPayload")
proto.RegisterType((*Request)(nil), "Request") proto.RegisterType((*Request)(nil), "Request")
proto.RegisterType((*Response)(nil), "Response") proto.RegisterType((*Response)(nil), "Response")
} }
func TestServer(t *testing.T) { func TestServer(t *testing.T) {
server := NewServer() var (
ctx := context.Background() ctx = context.Background()
server = NewServer()
expectedResponse = &testPayload{Foo: "baz"}
testImpl = &testingServer{payload: expectedResponse}
)
if err := server.Register("test-service", map[string]Handler{ // more mocking of what is generated code. Unlike grpc, we register with a
"Test": HandlerFunc(func(ctx context.Context, req interface{}) (interface{}, error) { // closure so that the descriptor is allocated only on registration.
fmt.Println(req) registerTestingService := func(srv *Server, svc testingService) error {
return srv.Register(serviceName, map[string]Handler{
"Test": HandlerFunc(func(ctx context.Context, req interface{}) (interface{}, error) {
return svc.Test(ctx, req.(*testPayload))
}),
})
}
return &testPayload{Foo: "baz"}, nil if err := registerTestingService(server, testImpl); err != nil {
}),
}); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -58,14 +99,18 @@ func TestServer(t *testing.T) {
} }
defer conn.Close() defer conn.Close()
client := NewClient(conn) client := newTestingClient(NewClient(conn))
tp := &testPayload{ tp := &testPayload{
Foo: "bar", Foo: "bar",
} }
resp, err := client.Call(ctx, "test-service", "Test", tp)
resp, err := client.Test(ctx, tp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fmt.Println(resp)
if !reflect.DeepEqual(resp, expectedResponse) {
t.Fatalf("unexpected response: %+#v != %+#v", resp, expectedResponse)
}
} }