This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
paralleldispatcher_test.go
163 lines (146 loc) · 5.87 KB
/
paralleldispatcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package command_test
import (
"errors"
"sync"
"testing"
"time"
"github.com/nproc/errorgroup-go"
. "github.com/smartystreets/goconvey/convey"
"github.com/txgruppi/command"
)
func TestParallelDispatcher(t *testing.T) {
Convey("ParallelDispatcher", t, func() {
handlerA := &TestHandler{
CanHandleCallback: NewCanHandleCallbackForCommand(CommandA),
}
handlerB := &TestHandler{
CanHandleCallback: NewCanHandleCallbackForCommand(CommandB),
}
handlers := []command.Handler{handlerA, handlerB}
dispatcher := command.NewParallelDispatcher(handlers)
Convey("New", func() {
Convey("it should create a dispatcher with the given handlers", func() {
err := dispatcher.Dispatch(CommandA)
So(err, ShouldBeNil)
So(handlerA.CanHandleCallCount, ShouldEqual, 1)
So(handlerB.CanHandleCallCount, ShouldEqual, 1)
So(handlerA.HandleCallCount, ShouldEqual, 1)
So(handlerB.HandleCallCount, ShouldEqual, 0)
err = dispatcher.Dispatch(CommandB)
So(err, ShouldBeNil)
So(handlerA.CanHandleCallCount, ShouldEqual, 2)
So(handlerB.CanHandleCallCount, ShouldEqual, 2)
So(handlerA.HandleCallCount, ShouldEqual, 1)
So(handlerB.HandleCallCount, ShouldEqual, 1)
})
})
Convey("AppendHandlers", func() {
Convey("it should append the given handler to the existing handlers", func() {
handlerC := &TestHandler{
CanHandleCallback: NewCanHandleCallbackForCommand(CommandC),
}
dispatcher.AppendHandlers(handlerC)
err := dispatcher.Dispatch(CommandA)
So(err, ShouldBeNil)
So(handlerA.CanHandleCallCount, ShouldEqual, 1)
So(handlerB.CanHandleCallCount, ShouldEqual, 1)
So(handlerC.CanHandleCallCount, ShouldEqual, 1)
So(handlerA.HandleCallCount, ShouldEqual, 1)
So(handlerB.HandleCallCount, ShouldEqual, 0)
So(handlerC.HandleCallCount, ShouldEqual, 0)
err = dispatcher.Dispatch(CommandB)
So(err, ShouldBeNil)
So(handlerA.CanHandleCallCount, ShouldEqual, 2)
So(handlerB.CanHandleCallCount, ShouldEqual, 2)
So(handlerC.CanHandleCallCount, ShouldEqual, 2)
So(handlerA.HandleCallCount, ShouldEqual, 1)
So(handlerB.HandleCallCount, ShouldEqual, 1)
So(handlerC.HandleCallCount, ShouldEqual, 0)
err = dispatcher.Dispatch(CommandC)
So(err, ShouldBeNil)
So(handlerA.CanHandleCallCount, ShouldEqual, 3)
So(handlerB.CanHandleCallCount, ShouldEqual, 3)
So(handlerC.CanHandleCallCount, ShouldEqual, 3)
So(handlerA.HandleCallCount, ShouldEqual, 1)
So(handlerB.HandleCallCount, ShouldEqual, 1)
So(handlerC.HandleCallCount, ShouldEqual, 1)
})
})
Convey("Dispatch", func() {
Convey("it should dispatch the command and return the handler's return value", func() {
expected := errors.New("This is the expected error")
handlerA.HandleCallback = func(interface{}, command.Dispatcher) error { return expected }
err := dispatcher.Dispatch(CommandA)
So(err.Error(), ShouldEqual, expected.Error())
})
Convey("it should dispatch the command and return the handler's return value only if the command can be handled", func() {
notExpected := errors.New("This error should not be returned")
handlerA.HandleCallback = func(interface{}, command.Dispatcher) error { return notExpected }
err := dispatcher.Dispatch(CommandB)
So(err, ShouldBeNil)
})
Convey("it should return an error if no handler can handle the given command", func() {
err := dispatcher.Dispatch(CommandC)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "No handler can handle command_test.Command")
})
Convey("it should recover from panic", func() {
canHandleErr := errors.New("CanHandle panic")
handleErr := errors.New("Handle panic")
handlerA.CanHandleCallback = func(interface{}) bool { panic(canHandleErr) }
err := dispatcher.Dispatch(CommandB)
So(err.Error(), ShouldEqual, canHandleErr.Error())
handlerA.CanHandleCallback = nil
handlerB.HandleCallback = func(interface{}, command.Dispatcher) error { panic(handleErr) }
err = dispatcher.Dispatch(CommandB)
So(err.Error(), ShouldEqual, handleErr.Error())
})
Convey("it should wait a handler to finish before continue (parallel dispatch)", func() {
callOrder := []int{}
lock := sync.Mutex{}
handlerA.CanHandleCallback = func(interface{}) bool {
time.Sleep(10 * time.Millisecond)
lock.Lock()
defer lock.Unlock()
callOrder = append(callOrder, 1)
return false
}
handlerB.CanHandleCallback = func(interface{}) bool {
lock.Lock()
defer lock.Unlock()
callOrder = append(callOrder, 2)
return true
}
err := dispatcher.Dispatch(CommandB)
So(err, ShouldBeNil)
So(callOrder, ShouldResemble, []int{2, 1})
})
Convey("it should group errors", func() {
errA := errors.New("Error A")
errB := errors.New("Error B")
handlerA.HandleCallback = func(interface{}, command.Dispatcher) error { panic(errA) }
handlerB.HandleCallback = func(interface{}, command.Dispatcher) error { return errB }
handlerB.CanHandleCallback = NewCanHandleCallbackForCommand(CommandA)
err := dispatcher.Dispatch(CommandA)
So(err, ShouldNotBeNil)
So(err, ShouldHaveSameTypeAs, &errorgroup.ErrorGroup{})
errGroup := err.(*errorgroup.ErrorGroup)
So(len(errGroup.Errors), ShouldEqual, 2)
So(errGroup.Errors, ShouldContain, errA)
So(errGroup.Errors, ShouldContain, errB)
})
})
Convey("DispatchOptional", func() {
Convey("it should dispatch the command and return the handler's return value", func() {
expected := errors.New("This is the expected error")
handlerA.HandleCallback = func(interface{}, command.Dispatcher) error { return expected }
err := dispatcher.DispatchOptional(CommandA)
So(err.Error(), ShouldEqual, expected.Error())
})
Convey("it should return nil if no handler can handle the given command", func() {
err := dispatcher.DispatchOptional(CommandC)
So(err, ShouldBeNil)
})
})
})
}