-
Notifications
You must be signed in to change notification settings - Fork 13
/
consumer_test.go
166 lines (148 loc) · 3.97 KB
/
consumer_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
164
165
166
package kafka
import (
"context"
"errors"
"sync"
"testing"
)
func Test_consumer_process(t *testing.T) {
t.Run("When_Processing_Is_Successful", func(t *testing.T) {
// Given
c := consumer{
base: &base{metric: &ConsumerMetric{}},
consumeFn: func(*Message) error {
return nil
},
}
// When
c.process(&Message{})
// Then
if c.metric.TotalProcessedMessagesCounter != 1 {
t.Fatalf("Total Processed Message Counter must equal to 3")
}
if c.metric.TotalUnprocessedMessagesCounter != 0 {
t.Fatalf("Total Unprocessed Message Counter must equal to 0")
}
})
t.Run("When_Re-processing_Is_Successful", func(t *testing.T) {
// Given
gotOnlyOneTimeException := true
c := consumer{
base: &base{metric: &ConsumerMetric{}, logger: NewZapLogger(LogLevelDebug), transactionalRetry: true},
consumeFn: func(*Message) error {
if gotOnlyOneTimeException {
gotOnlyOneTimeException = false
return errors.New("simulate only one time exception")
}
return nil
},
}
// When
c.process(&Message{})
// Then
if c.metric.TotalProcessedMessagesCounter != 1 {
t.Fatalf("Total Processed Message Counter must equal to 1")
}
if c.metric.TotalUnprocessedMessagesCounter != 0 {
t.Fatalf("Total Unprocessed Message Counter must equal to 0")
}
})
t.Run("When_Re-processing_Is_Failed_And_Retry_Disabled", func(t *testing.T) {
// Given
c := consumer{
base: &base{metric: &ConsumerMetric{}, logger: NewZapLogger(LogLevelDebug)},
consumeFn: func(*Message) error {
return errors.New("error case")
},
}
// When
c.process(&Message{})
// Then
if c.metric.TotalProcessedMessagesCounter != 0 {
t.Fatalf("Total Processed Message Counter must equal to 0")
}
if c.metric.TotalUnprocessedMessagesCounter != 1 {
t.Fatalf("Total Unprocessed Message Counter must equal to 1")
}
})
t.Run("When_Re-processing_Is_Failed_And_Retry_Enabled", func(t *testing.T) {
// Given
mc := mockCronsumer{}
c := consumer{
base: &base{metric: &ConsumerMetric{}, logger: NewZapLogger(LogLevelDebug), retryEnabled: true, cronsumer: &mc},
consumeFn: func(*Message) error {
return errors.New("error case")
},
}
// When
c.process(&Message{})
// Then
if c.metric.TotalProcessedMessagesCounter != 0 {
t.Fatalf("Total Processed Message Counter must equal to 0")
}
if c.metric.TotalUnprocessedMessagesCounter != 1 {
t.Fatalf("Total Unprocessed Message Counter must equal to 1")
}
})
t.Run("When_Re-processing_Is_Failed_And_Retry_Failed", func(t *testing.T) {
// Given
mc := mockCronsumer{wantErr: true}
c := consumer{
base: &base{metric: &ConsumerMetric{}, logger: NewZapLogger(LogLevelDebug), retryEnabled: true, cronsumer: &mc},
consumeFn: func(*Message) error {
return errors.New("error case")
},
}
// When
c.process(&Message{})
// Then
if c.metric.TotalProcessedMessagesCounter != 0 {
t.Fatalf("Total Processed Message Counter must equal to 0")
}
if c.metric.TotalUnprocessedMessagesCounter != 1 {
t.Fatalf("Total Unprocessed Message Counter must equal to 1")
}
})
}
func Test_consumer_Pause(t *testing.T) {
// Given
ctx, cancelFn := context.WithCancel(context.Background())
c := consumer{
base: &base{
logger: NewZapLogger(LogLevelDebug),
pause: make(chan struct{}),
context: ctx, cancelFn: cancelFn,
consumerState: stateRunning,
},
}
go func() {
<-c.base.pause
}()
// When
c.Pause()
// Then
if c.base.consumerState != statePaused {
t.Fatal("consumer state must be in paused")
}
}
func Test_consumer_Resume(t *testing.T) {
// Given
mc := mockReader{}
ctx, cancelFn := context.WithCancel(context.Background())
c := consumer{
base: &base{
r: &mc,
logger: NewZapLogger(LogLevelDebug),
pause: make(chan struct{}),
quit: make(chan struct{}),
wg: sync.WaitGroup{},
context: ctx, cancelFn: cancelFn,
},
}
// When
c.Resume()
// Then
if c.base.consumerState != stateRunning {
t.Fatal("consumer state must be in running")
}
}