-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_strategy_synchronous_test.go
76 lines (64 loc) · 1.79 KB
/
publish_strategy_synchronous_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
package mediator_test
import (
"context"
"github.com/Oleexo/mediator-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"testing"
)
type synchronousHandler interface {
Execute() error
Executed() bool
}
type synchronousHandlerImpl struct {
executed bool
err error
}
func newSynchronousHandler(err error) synchronousHandler {
return &synchronousHandlerImpl{
executed: false,
err: err,
}
}
func (s *synchronousHandlerImpl) Execute() error {
s.executed = true
return s.err
}
func (s *synchronousHandlerImpl) Executed() bool {
return s.executed
}
func TestSynchronousPublishStrategy(t *testing.T) {
t.Run("no error will run all tests", func(t *testing.T) {
strategy := mediator.NewSynchronousPublishStrategy()
handlers := []interface{}{
newSynchronousHandler(nil),
newSynchronousHandler(nil),
}
result := strategy.Execute(context.Background(),
handlers,
func(ctx context.Context, handler interface{}) error {
return handler.(synchronousHandler).Execute()
})
assert.NoError(t, result)
for _, handler := range handlers {
assert.True(t, handler.(synchronousHandler).Executed())
}
})
t.Run("error will stop the execution", func(t *testing.T) {
strategy := mediator.NewSynchronousPublishStrategy()
handlers := []interface{}{
newSynchronousHandler(nil),
newSynchronousHandler(errors.New("an error happened")),
newSynchronousHandler(nil),
}
result := strategy.Execute(context.Background(),
handlers,
func(ctx context.Context, handler interface{}) error {
return handler.(synchronousHandler).Execute()
})
assert.Error(t, result)
assert.True(t, handlers[0].(synchronousHandler).Executed())
assert.True(t, handlers[1].(synchronousHandler).Executed())
assert.False(t, handlers[2].(synchronousHandler).Executed())
})
}