Beispiel #1
0
func newFakePluginServer(mockCtrl *gomock.Controller) *fakePluginServer {
	serverReader, clientWriter := io.Pipe()
	clientReader, serverWriter := io.Pipe()

	server := frame.NewServer(serverReader, serverWriter)
	client := frame.NewClient(clientWriter, clientReader)

	mockPlugin := plugintest.NewMockPlugin(mockCtrl)
	mockServiceGenerator := plugintest.NewMockServiceGenerator(mockCtrl)

	handler := multiplex.NewHandler()
	handler.Put("Plugin", api.NewPluginHandler(mockPlugin))
	handler.Put("ServiceGenerator", api.NewServiceGeneratorHandler(mockServiceGenerator))

	done := make(chan error)
	go func() {
		err := server.Serve(envelope.NewServer(protocol.Binary, handler))
		if err != nil {
			done <- err
		}
		close(done)
	}()

	return &fakePluginServer{
		errCh:            done,
		server:           server,
		ClientTransport:  client,
		Plugin:           mockPlugin,
		ServiceGenerator: mockServiceGenerator,
	}
}
Beispiel #2
0
func TestServiceGenerator(t *testing.T) {
	transport, done := fakeEnvelopeClient()
	defer done()

	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	serviceGenerator := plugintest.NewMockServiceGenerator(mockCtrl)

	go Main(&Plugin{
		Name:             "hello",
		ServiceGenerator: serviceGenerator,
	})

	pluginClient := api.NewPluginClient(multiplex.NewClient("Plugin", transport))
	defer pluginClient.Goodbye()

	handshake, err := pluginClient.Handshake(&api.HandshakeRequest{})
	require.NoError(t, err)
	assert.Equal(t, api.APIVersion, handshake.APIVersion)
	assert.Equal(t, version.Version, *handshake.LibraryVersion)
	assert.Equal(t, "hello", handshake.Name)
	assert.Contains(t, handshake.Features, api.FeatureServiceGenerator)

	sgClient := api.NewServiceGeneratorClient(multiplex.NewClient("ServiceGenerator", transport))
	req := &api.GenerateServiceRequest{
		RootServices: []api.ServiceID{1},
		Services: map[api.ServiceID]*api.Service{
			1: {
				Name:       "MyService",
				ThriftName: "MyService",
				Functions:  []*api.Function{},
				ParentID:   (*api.ServiceID)(ptr.Int32(2)),
				ModuleID:   1,
			},
			2: {
				Name:       "BaseService",
				ThriftName: "BaseService",
				Functions: []*api.Function{
					{
						Name:       "Healthy",
						ThriftName: "healthy",
						Arguments:  []*api.Argument{},
					},
				},
				ModuleID: 1,
			},
		},
		Modules: map[api.ModuleID]*api.Module{
			1: {
				ImportPath: "go.uber.org/thriftrw/plugin/fake",
				Directory:  "fake",
			},
		},
	}

	res := &api.GenerateServiceResponse{
		Files: map[string][]byte{
			"fake/myservice/foo.go":   {1, 2, 3},
			"fake/baseservice/bar.go": {4, 5, 6},
			"fake/baz.go":             {7, 8, 9},
		},
	}

	serviceGenerator.EXPECT().Generate(req).Return(res, nil)
	gotRes, err := sgClient.Generate(req)
	if assert.NoError(t, err) {
		assert.Equal(t, res, gotRes)
	}
}