Exemple #1
0
func TestEmptyProcedureRegistration(t *testing.T) {
	m := transport.NewMapRegistry("test-service-name")

	registrants := []transport.Registrant{
		{
			Service:   "test",
			Procedure: "",
		},
	}

	assert.Panics(t,
		func() { m.Register(registrants) },
		"expected registry panic")
}
Exemple #2
0
// NewDispatcher builds a new Dispatcher using the specified Config.
func NewDispatcher(cfg Config) Dispatcher {
	if cfg.Name == "" {
		panic("a service name is required")
	}

	return dispatcher{
		Name:        cfg.Name,
		Registrar:   transport.NewMapRegistry(cfg.Name),
		inbounds:    cfg.Inbounds,
		outbounds:   convertOutbounds(cfg.Outbounds, cfg.Filter),
		Interceptor: cfg.Interceptor,
		deps:        transport.NoDeps.WithTracer(cfg.Tracer),
	}
}
Exemple #3
0
func TestMapRegistry_ServiceProcedures(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	m := transport.NewMapRegistry("myservice")

	bar := transporttest.NewMockUnaryHandler(mockCtrl)
	foo := transporttest.NewMockUnaryHandler(mockCtrl)
	aww := transporttest.NewMockUnaryHandler(mockCtrl)
	m.Register([]transport.Registrant{
		{
			Service:     "anotherservice",
			Procedure:   "bar",
			HandlerSpec: transport.NewUnaryHandlerSpec(bar),
		},
		{
			Procedure:   "foo",
			HandlerSpec: transport.NewUnaryHandlerSpec(foo),
		},
		{
			Service:     "anotherservice",
			Procedure:   "aww",
			HandlerSpec: transport.NewUnaryHandlerSpec(aww),
		},
	})

	expectedOrderedServiceProcedures := []transport.ServiceProcedure{
		{
			Service:   "anotherservice",
			Procedure: "aww",
		},
		{
			Service:   "anotherservice",
			Procedure: "bar",
		},
		{
			Service:   "myservice",
			Procedure: "foo",
		},
	}

	serviceProcedures := m.ServiceProcedures()

	assert.Equal(t, expectedOrderedServiceProcedures, serviceProcedures)
}
Exemple #4
0
func TestMapRegistry(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	m := transport.NewMapRegistry("myservice")

	foo := transporttest.NewMockUnaryHandler(mockCtrl)
	bar := transporttest.NewMockUnaryHandler(mockCtrl)
	m.Register([]transport.Registrant{
		{
			Procedure:   "foo",
			HandlerSpec: transport.NewUnaryHandlerSpec(foo),
		},
		{
			Service:     "anotherservice",
			Procedure:   "bar",
			HandlerSpec: transport.NewUnaryHandlerSpec(bar),
		},
	})

	tests := []struct {
		service, procedure string
		want               transport.UnaryHandler
	}{
		{"myservice", "foo", foo},
		{"", "foo", foo},
		{"anotherservice", "foo", nil},
		{"", "bar", nil},
		{"myservice", "bar", nil},
		{"anotherservice", "bar", bar},
	}

	for _, tt := range tests {
		got, err := m.GetHandlerSpec(tt.service, tt.procedure)
		if tt.want != nil {
			assert.NoError(t, err,
				"GetHandlerSpec(%q, %q) failed", tt.service, tt.procedure)
			assert.True(t, tt.want == got.Unary(), // want == match, not deep equals
				"GetHandlerSpec(%q, %q) did not match", tt.service, tt.procedure)
		} else {
			assert.Error(t, err)
		}
	}
}