-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzigg_test.go
142 lines (119 loc) · 3.58 KB
/
zigg_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
package ziggurat
import (
"context"
"errors"
"github.com/gojekfarm/ziggurat/v2/logger"
"github.com/stretchr/testify/mock"
"sync/atomic"
"testing"
"time"
)
type MockConsumer struct {
mock.Mock
PollInterval time.Duration
}
func (m *MockConsumer) Consume(ctx context.Context, handler Handler) error {
args := m.Called(ctx, handler)
if m.PollInterval == 0 {
m.PollInterval = 200 * time.Millisecond
}
keepAlive := true
for keepAlive {
select {
case <-ctx.Done():
keepAlive = false
default:
time.Sleep(m.PollInterval)
handler.Handle(ctx, &Event{})
}
}
return args.Error(0)
}
func TestZiggurat_Run(t *testing.T) {
t.Run("clean shutdown error", func(t *testing.T) {
var zig Ziggurat
var msgCount int32
ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
defer cancel()
mc1 := MockConsumer{}
mc2 := MockConsumer{}
mc3 := MockConsumer{}
handler := HandlerFunc(func(ctx context.Context, event *Event) {
atomic.AddInt32(&msgCount, 1)
})
mc1.On("Consume", mock.Anything, mock.Anything).Return(nil)
mc2.On("Consume", mock.Anything, mock.Anything).Return(nil)
mc3.On("Consume", mock.Anything, mock.Anything).Return(nil)
err := zig.Run(ctx, handler, &mc1, &mc2, &mc3)
if !errors.Is(err, ErrCleanShutdown) {
t.Errorf("expected %s got %s", ErrCleanShutdown.Error(), err.Error())
return
}
if atomic.LoadInt32(&msgCount) < 1 {
t.Error("handler not invoked")
}
t.Logf("message count:%d", msgCount)
})
t.Run("one of the consumers error out", func(t *testing.T) {
var errorHandlerCalled atomic.Bool
var zig Ziggurat
zig.ErrorHandler = func(err error) {
t.Logf("error handler invoked with error:%s", err.Error())
errorHandlerCalled.Store(true)
}
var msgCount int32
ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
defer cancel()
mc1 := MockConsumer{}
mc2 := MockConsumer{}
mc3 := MockConsumer{}
handler := HandlerFunc(func(ctx context.Context, event *Event) {
atomic.AddInt32(&msgCount, 1)
})
mc1.On("Consume", mock.Anything, mock.Anything).Return(nil)
mc2.On("Consume", mock.Anything, mock.Anything).Return(nil)
mc3.On("Consume", mock.Anything, mock.Anything).Return(errors.New("mc3 errored out"))
err := zig.Run(ctx, handler, &mc1, &mc2, &mc3)
if err == nil {
t.Error("expected error got nil")
return
}
if !errorHandlerCalled.Load() {
t.Error("error handler was never called")
}
if atomic.LoadInt32(&msgCount) < 1 {
t.Error("handler not invoked")
}
t.Logf("message count:%d", msgCount)
})
t.Run("test shutdown timeout", func(t *testing.T) {
var zig Ziggurat
zig.ShutdownTimeout = 250 * time.Millisecond
zig.Logger = logger.NewLogger(logger.LevelInfo)
errCount := 1
zig.ErrorHandler = func(err error) {
errCount++
}
ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
defer cancel()
mc1 := MockConsumer{PollInterval: 10000 * time.Second}
mc2 := MockConsumer{}
mc3 := MockConsumer{}
handler := HandlerFunc(func(ctx context.Context, event *Event) {})
mc1.On("Consume", mock.Anything, mock.Anything).Return(nil)
mc2.On("Consume", mock.Anything, mock.Anything).Return(errors.New("mc2 errored out"))
mc3.On("Consume", mock.Anything, mock.Anything).Return(nil)
err := zig.Run(ctx, handler, &mc1, &mc2, &mc3)
if err == nil {
t.Error("expected error got nil")
return
}
if err.Error() != "shutdown timeout" {
t.Errorf("expected error:%s got %s\n", "shutdown timeout", err.Error())
return
}
if errCount < 1 {
t.Errorf("expected an error count of 1 got %d\n", errCount)
}
})
}