Add error handling test cases for dispatcher
Some checks failed
Go CI/CD / go-ci (push) Has been cancelled

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.
This commit is contained in:
Rene Nochebuena 2025-04-13 01:19:06 -06:00
parent 3d2e5ec027
commit 5bea19ecf1
Signed by: Rene Nochebuena
GPG Key ID: A9FD83117EA538D8

View File

@ -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")
}
}