mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
bus: Dispatch now passes empty ctx if handler require it
This commit is contained in:
parent
9ca9a7c302
commit
9c1758b593
@ -96,13 +96,23 @@ func (b *InProcBus) DispatchCtx(ctx context.Context, msg Msg) error {
|
||||
func (b *InProcBus) Dispatch(msg Msg) error {
|
||||
var msgName = reflect.TypeOf(msg).Elem().Name()
|
||||
|
||||
var handler = b.handlers[msgName]
|
||||
var handler = b.handlersWithCtx[msgName]
|
||||
withCtx := true
|
||||
|
||||
if handler == nil {
|
||||
withCtx = false
|
||||
handler = b.handlers[msgName]
|
||||
}
|
||||
|
||||
if handler == nil {
|
||||
return ErrHandlerNotFound
|
||||
}
|
||||
|
||||
var params = make([]reflect.Value, 1)
|
||||
params[0] = reflect.ValueOf(msg)
|
||||
var params = []reflect.Value{}
|
||||
if withCtx {
|
||||
params = append(params, reflect.ValueOf(context.Background()))
|
||||
}
|
||||
params = append(params, reflect.ValueOf(msg))
|
||||
|
||||
ret := reflect.ValueOf(handler).Call(params)
|
||||
err := ret[0].Interface()
|
||||
|
@ -34,7 +34,6 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
|
||||
}
|
||||
|
||||
bus.AddHandler(handler)
|
||||
bus.AddHandlerCtx(handlerWithCtx)
|
||||
|
||||
t.Run("when a normal handler is registered", func(t *testing.T) {
|
||||
bus.Dispatch(&testQuery{})
|
||||
@ -42,15 +41,17 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
|
||||
if handlerCallCount != 1 {
|
||||
t.Errorf("Expected normal handler to be called 1 time. was called %d", handlerCallCount)
|
||||
}
|
||||
|
||||
t.Run("when a ctx handler is registered", func(t *testing.T) {
|
||||
bus.AddHandlerCtx(handlerWithCtx)
|
||||
bus.Dispatch(&testQuery{})
|
||||
|
||||
if handlerWithCtxCallCount != 1 {
|
||||
t.Errorf("Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("when a ctx handler is registered", func(t *testing.T) {
|
||||
bus.DispatchCtx(context.Background(), &testQuery{})
|
||||
|
||||
if handlerWithCtxCallCount != 1 {
|
||||
t.Errorf("Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueryHandlerReturnsError(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user