Commit 17724e9f by Marcus Olsson Committed by GitHub

Bus: Remove unused wildcard handlers and clean up tests (#20327)

* Refactor bus tests

* Remove wildcard listeners

* Fix review comments
parent adc84c6a
......@@ -38,7 +38,6 @@ type Bus interface {
AddHandler(handler HandlerFunc)
AddHandlerCtx(handler HandlerFunc)
AddEventListener(handler HandlerFunc)
AddWildcardListener(handler HandlerFunc)
// SetTransactionManager allows the user to replace the internal
// noop TransactionManager that is responsible for manageing
......@@ -53,11 +52,10 @@ func (b *InProcBus) InTransaction(ctx context.Context, fn func(ctx context.Conte
// InProcBus defines the bus structure
type InProcBus struct {
handlers map[string]HandlerFunc
handlersWithCtx map[string]HandlerFunc
listeners map[string][]HandlerFunc
wildcardListeners []HandlerFunc
txMng TransactionManager
handlers map[string]HandlerFunc
handlersWithCtx map[string]HandlerFunc
listeners map[string][]HandlerFunc
txMng TransactionManager
}
// temp stuff, not sure how to handle bus instance, and init yet
......@@ -69,7 +67,6 @@ func New() Bus {
bus.handlers = make(map[string]HandlerFunc)
bus.handlersWithCtx = make(map[string]HandlerFunc)
bus.listeners = make(map[string][]HandlerFunc)
bus.wildcardListeners = make([]HandlerFunc, 0)
bus.txMng = &noopTransactionManager{}
return bus
......@@ -152,21 +149,9 @@ func (b *InProcBus) Publish(msg Msg) error {
}
}
for _, listenerHandler := range b.wildcardListeners {
ret := reflect.ValueOf(listenerHandler).Call(params)
err := ret[0].Interface()
if err != nil {
return err.(error)
}
}
return nil
}
func (b *InProcBus) AddWildcardListener(handler HandlerFunc) {
b.wildcardListeners = append(b.wildcardListeners, handler)
}
func (b *InProcBus) AddHandler(handler HandlerFunc) {
handlerType := reflect.TypeOf(handler)
queryTypeName := handlerType.In(0).Elem().Name()
......@@ -207,11 +192,6 @@ func AddEventListener(handler HandlerFunc) {
globalBus.AddEventListener(handler)
}
// AddWildcardListener attach a handler function to the wildcard listener
func AddWildcardListener(handler HandlerFunc) {
globalBus.AddWildcardListener(handler)
}
func Dispatch(msg Msg) error {
return globalBus.Dispatch(msg)
}
......
......@@ -13,82 +13,118 @@ type testQuery struct {
Resp string
}
func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
func TestDispatch(t *testing.T) {
bus := New()
handlerWithCtxCallCount := 0
handlerCallCount := 0
var invoked bool
handlerWithCtx := func(ctx context.Context, query *testQuery) error {
handlerWithCtxCallCount++
bus.AddHandler(func(query *testQuery) error {
invoked = true
return nil
}
})
handler := func(query *testQuery) error {
handlerCallCount++
return nil
}
err := bus.Dispatch(&testQuery{})
require.NoError(t, err)
err := bus.DispatchCtx(context.Background(), &testQuery{})
require.True(t, invoked, "expected handler to be called")
}
func TestDispatch_NoRegisteredHandler(t *testing.T) {
bus := New()
err := bus.Dispatch(&testQuery{})
require.Equal(t, err, ErrHandlerNotFound,
"expected bus to return HandlerNotFound since no handler is registered")
}
func TestDispatch_ContextHandler(t *testing.T) {
bus := New()
bus.AddHandler(handler)
var invoked bool
t.Run("when a normal handler is registered", func(t *testing.T) {
err := bus.Dispatch(&testQuery{})
require.Nil(t, err)
bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
invoked = true
return nil
})
err := bus.Dispatch(&testQuery{})
require.NoError(t, err)
require.Equal(t, handlerCallCount, 1,
"Expected normal handler to be called 1 time. was called %d", handlerCallCount)
require.True(t, invoked, "expected handler to be called")
}
func TestDispatchCtx(t *testing.T) {
bus := New()
t.Run("when a ctx handler is registered", func(t *testing.T) {
bus.AddHandlerCtx(handlerWithCtx)
err := bus.Dispatch(&testQuery{})
require.Nil(t, err)
var invoked bool
require.Equal(t, handlerWithCtxCallCount, 1,
"Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
})
bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
invoked = true
return nil
})
err := bus.DispatchCtx(context.Background(), &testQuery{})
require.NoError(t, err)
require.True(t, invoked, "expected handler to be called")
}
func TestQueryHandlerReturnsError(t *testing.T) {
func TestDispatchCtx_NoRegisteredHandler(t *testing.T) {
bus := New()
bus.AddHandler(func(query *testQuery) error {
return errors.New("handler error")
})
err := bus.Dispatch(&testQuery{})
require.Error(t, err, "Send query failed")
err := bus.DispatchCtx(context.Background(), &testQuery{})
require.Equal(t, err, ErrHandlerNotFound,
"expected bus to return HandlerNotFound since no handler is registered")
}
func TestQueryHandlerReturn(t *testing.T) {
func TestQuery(t *testing.T) {
bus := New()
want := "hello from handler"
bus.AddHandler(func(q *testQuery) error {
q.Resp = "hello from handler"
q.Resp = want
return nil
})
query := &testQuery{}
err := bus.Dispatch(query)
q := &testQuery{}
err := bus.Dispatch(q)
require.NoError(t, err, "unable to dispatch query")
require.Nil(t, err, "Send query failed")
require.Equal(t, want, q.Resp)
}
func TestEventListeners(t *testing.T) {
func TestQuery_HandlerReturnsError(t *testing.T) {
bus := New()
count := 0
bus.AddEventListener(func(query *testQuery) error {
count++
return nil
bus.AddHandler(func(query *testQuery) error {
return errors.New("handler error")
})
err := bus.Dispatch(&testQuery{})
require.Error(t, err, "expected error but got none")
}
func TestEvent(t *testing.T) {
bus := New()
var invoked bool
bus.AddEventListener(func(query *testQuery) error {
count += 10
invoked = true
return nil
})
err := bus.Publish(&testQuery{})
require.Nil(t, err, "Publish event failed")
require.NoError(t, err, "unable to publish event")
require.True(t, invoked)
}
func TestEvent_NoRegisteredListener(t *testing.T) {
bus := New()
err := bus.Publish(&testQuery{})
require.NoError(t, err, "unable to publish event")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment