From 5bea19ecf1b10d6facb4cafd80840e0015881433 Mon Sep 17 00:00:00 2001 From: Rene Nochebuena Date: Sun, 13 Apr 2025 01:19:06 -0600 Subject: [PATCH] Add error handling test cases for dispatcher Introduce error-producing handlers to test command, query, and event dispatch processes. Ensure proper error propagation when handlers encounter failures. Validate that expected errors are correctly returned in all scenarios. --- dispatcher_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/dispatcher_test.go b/dispatcher_test.go index 54efd40..b266481 100644 --- a/dispatcher_test.go +++ b/dispatcher_test.go @@ -2,6 +2,7 @@ package stonecqrs import ( "context" + "errors" "fmt" "testing" ) @@ -49,6 +50,30 @@ func (handler *testEventHandler) Handle( return nil } +type testErrorHandler struct{} + +func (handler *testErrorHandler) Handle( + ctx context.Context, cmd Command, +) ([]Event, error) { + return nil, errors.New("test error") +} + +type testErrorQueryHandler struct{} + +func (handler *testErrorQueryHandler) Handle( + ctx context.Context, query Query, +) (interface{}, error) { + return nil, errors.New("test error") +} + +type testErrorEventHandler struct{} + +func (handler *testErrorEventHandler) Handle( + ctx context.Context, event Event, +) error { + return errors.New("test error") +} + func Test_NewDispatcher(t *testing.T) { d := NewDispatcher() if d == nil { @@ -98,7 +123,7 @@ func Test_UnknownEvent(t *testing.T) { t.Fatal("expected error") } - err = d.DispatchEvent(context.Background(), unknownEvent{}) + _, err = d.DispatchQuery(context.Background(), unknownEvent{}) if err == nil { t.Fatal("expected error") @@ -110,3 +135,25 @@ func Test_UnknownEvent(t *testing.T) { t.Fatal("expected error") } } + +func Test_Error(t *testing.T) { + d := NewDispatcher() + d.RegisterCommandHandler(testCommand{}, &testErrorHandler{}) + d.RegisterQueryHandler(testQuery{}, &testErrorQueryHandler{}) + d.RegisterEventHandler(testEvent{}, &testErrorEventHandler{}) + + _, err := d.DispatchCommand(context.Background(), testCommand{}) + if err == nil { + t.Fatal("expected error") + } + + _, err = d.DispatchQuery(context.Background(), testQuery{}) + if err == nil { + t.Fatal("expected error") + } + + err = d.DispatchEvent(context.Background(), testEvent{}) + if err == nil { + t.Fatal("expected error") + } +}