2014-11-28 15:16:49 -06:00
|
|
|
package bus
|
|
|
|
|
|
|
|
import (
|
2018-06-08 03:51:27 -05:00
|
|
|
"context"
|
2014-11-28 15:16:49 -06:00
|
|
|
"errors"
|
|
|
|
"testing"
|
2019-10-14 10:23:31 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-11-28 15:16:49 -06:00
|
|
|
)
|
|
|
|
|
2018-06-08 03:51:27 -05:00
|
|
|
type testQuery struct {
|
2019-06-04 15:00:05 -05:00
|
|
|
ID int64
|
2014-11-28 15:16:49 -06:00
|
|
|
Resp string
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
func TestDispatch(t *testing.T) {
|
2018-06-08 03:51:27 -05:00
|
|
|
bus := New()
|
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
var invoked bool
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
bus.AddHandler(func(query *testQuery) error {
|
|
|
|
invoked = true
|
2018-06-08 03:51:27 -05:00
|
|
|
return nil
|
2019-11-13 06:02:44 -06:00
|
|
|
})
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
err := bus.Dispatch(&testQuery{})
|
|
|
|
require.NoError(t, err)
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispatch_NoRegisteredHandler(t *testing.T) {
|
|
|
|
bus := New()
|
|
|
|
|
|
|
|
err := bus.Dispatch(&testQuery{})
|
2019-10-14 10:23:31 -05:00
|
|
|
require.Equal(t, err, ErrHandlerNotFound,
|
|
|
|
"expected bus to return HandlerNotFound since no handler is registered")
|
2019-11-13 06:02:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispatch_ContextHandler(t *testing.T) {
|
|
|
|
bus := New()
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
var invoked bool
|
2018-06-10 16:17:18 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
|
|
|
|
invoked = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err := bus.Dispatch(&testQuery{})
|
|
|
|
require.NoError(t, err)
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
require.True(t, invoked, "expected handler to be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDispatchCtx(t *testing.T) {
|
|
|
|
bus := New()
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
var invoked bool
|
2018-06-08 03:51:27 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
|
|
|
|
invoked = true
|
|
|
|
return nil
|
2018-06-08 03:51:27 -05:00
|
|
|
})
|
2018-06-13 02:18:53 -05:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
err := bus.DispatchCtx(context.Background(), &testQuery{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, invoked, "expected handler to be called")
|
2018-06-08 03:51:27 -05:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
func TestDispatchCtx_NoRegisteredHandler(t *testing.T) {
|
2014-12-01 10:56:03 -06:00
|
|
|
bus := New()
|
2014-11-28 15:16:49 -06:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
err := bus.DispatchCtx(context.Background(), &testQuery{})
|
|
|
|
require.Equal(t, err, ErrHandlerNotFound,
|
|
|
|
"expected bus to return HandlerNotFound since no handler is registered")
|
2014-11-28 15:16:49 -06:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
func TestQuery(t *testing.T) {
|
2014-12-01 10:56:03 -06:00
|
|
|
bus := New()
|
2019-11-13 06:02:44 -06:00
|
|
|
|
|
|
|
want := "hello from handler"
|
|
|
|
|
2018-06-08 03:51:27 -05:00
|
|
|
bus.AddHandler(func(q *testQuery) error {
|
2019-11-13 06:02:44 -06:00
|
|
|
q.Resp = want
|
2014-11-28 15:16:49 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
q := &testQuery{}
|
|
|
|
|
|
|
|
err := bus.Dispatch(q)
|
|
|
|
require.NoError(t, err, "unable to dispatch query")
|
2014-11-28 15:16:49 -06:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
require.Equal(t, want, q.Resp)
|
2014-11-28 15:16:49 -06:00
|
|
|
}
|
2015-01-09 09:36:23 -06:00
|
|
|
|
2019-11-13 06:02:44 -06:00
|
|
|
func TestQuery_HandlerReturnsError(t *testing.T) {
|
2015-01-09 09:36:23 -06:00
|
|
|
bus := New()
|
2019-11-13 06:02:44 -06:00
|
|
|
|
|
|
|
bus.AddHandler(func(query *testQuery) error {
|
|
|
|
return errors.New("handler error")
|
2015-01-09 09:36:23 -06:00
|
|
|
})
|
2019-11-13 06:02:44 -06:00
|
|
|
|
|
|
|
err := bus.Dispatch(&testQuery{})
|
|
|
|
require.Error(t, err, "expected error but got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvent(t *testing.T) {
|
|
|
|
bus := New()
|
|
|
|
|
|
|
|
var invoked bool
|
|
|
|
|
2018-06-08 03:51:27 -05:00
|
|
|
bus.AddEventListener(func(query *testQuery) error {
|
2019-11-13 06:02:44 -06:00
|
|
|
invoked = true
|
2015-01-09 09:36:23 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-06-08 03:51:27 -05:00
|
|
|
err := bus.Publish(&testQuery{})
|
2019-11-13 06:02:44 -06:00
|
|
|
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")
|
2015-01-09 09:36:23 -06:00
|
|
|
}
|