Commit 08f01a6c by Arve Knudsen Committed by GitHub

pkg/bus: Check errors (#19748)

* pkg/bus: Check errors
* pkg/bus: Convert tests to testify
parent 24b475e4
......@@ -3,8 +3,9 @@ package bus
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type testQuery struct {
......@@ -29,26 +30,25 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
}
err := bus.DispatchCtx(context.Background(), &testQuery{})
if err != ErrHandlerNotFound {
t.Errorf("expected bus to return HandlerNotFound is no handler is registered")
}
require.Equal(t, err, ErrHandlerNotFound,
"expected bus to return HandlerNotFound since no handler is registered")
bus.AddHandler(handler)
t.Run("when a normal handler is registered", func(t *testing.T) {
bus.Dispatch(&testQuery{})
err := bus.Dispatch(&testQuery{})
require.Nil(t, err)
if handlerCallCount != 1 {
t.Errorf("Expected normal handler to be called 1 time. was called %d", handlerCallCount)
}
require.Equal(t, handlerCallCount, 1,
"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{})
err := bus.Dispatch(&testQuery{})
require.Nil(t, err)
if handlerWithCtxCallCount != 1 {
t.Errorf("Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
}
require.Equal(t, handlerWithCtxCallCount, 1,
"Expected ctx handler to be called 1 time. was called %d", handlerWithCtxCallCount)
})
})
......@@ -56,23 +56,16 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
func TestQueryHandlerReturnsError(t *testing.T) {
bus := New()
bus.AddHandler(func(query *testQuery) error {
return errors.New("handler error")
})
err := bus.Dispatch(&testQuery{})
if err == nil {
t.Fatal("Send query failed")
} else {
t.Log("Handler error received ok " + err.Error())
}
require.Error(t, err, "Send query failed")
}
func TestQueryHandlerReturn(t *testing.T) {
bus := New()
bus.AddHandler(func(q *testQuery) error {
q.Resp = "hello from handler"
return nil
......@@ -81,32 +74,21 @@ func TestQueryHandlerReturn(t *testing.T) {
query := &testQuery{}
err := bus.Dispatch(query)
if err != nil {
t.Fatal("Send query failed " + err.Error())
} else if query.Resp != "hello from handler" {
t.Fatal("Failed to get response from handler")
}
require.Nil(t, err, "Send query failed")
}
func TestEventListeners(t *testing.T) {
bus := New()
count := 0
bus.AddEventListener(func(query *testQuery) error {
count++
return nil
})
bus.AddEventListener(func(query *testQuery) error {
count += 10
return nil
})
err := bus.Publish(&testQuery{})
if err != nil {
t.Fatal("Publish event failed " + err.Error())
} else if count != 11 {
t.Fatal(fmt.Sprintf("Publish event failed, listeners called: %v, expected: %v", count, 11))
}
require.Nil(t, err, "Publish event failed")
}
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