4
4
"context"
5
5
"fmt"
6
6
"testing"
7
+ "time"
7
8
8
9
"github.com/didil/inhooks/pkg/models"
9
10
"github.com/didil/inhooks/pkg/testsupport"
@@ -13,13 +14,14 @@ import (
13
14
"go.uber.org/zap"
14
15
)
15
16
16
- func TestSupervisorFetchAndProcess_OK (t * testing.T ) {
17
+ func TestSupervisorHandleReadyQueue_OK (t * testing.T ) {
17
18
ctx := context .Background ()
18
19
19
20
appConf , err := testsupport .InitAppConfig (ctx )
20
21
assert .NoError (t , err )
21
22
22
23
appConf .Supervisor .ErrSleepTime = 0
24
+ appConf .Supervisor .ReadyQueueConcurrency = 1
23
25
24
26
ctrl := gomock .NewController (t )
25
27
defer ctrl .Finish ()
@@ -46,10 +48,6 @@ func TestSupervisorFetchAndProcess_OK(t *testing.T) {
46
48
messageProcessor := mocks .NewMockMessageProcessor (ctrl )
47
49
processingResultsService := mocks .NewMockProcessingResultsService (ctrl )
48
50
49
- messageFetcher .EXPECT ().GetMessageForProcessing (ctx , appConf .Supervisor .ReadyWaitTime , flowId1 , sinkID1 ).Return (m , nil )
50
- messageProcessor .EXPECT ().Process (ctx , sink1 , m ).Return (nil )
51
- processingResultsService .EXPECT ().HandleOK (ctx , m ).Return (nil )
52
-
53
51
logger , err := zap .NewDevelopment ()
54
52
assert .NoError (t , err )
55
53
@@ -61,17 +59,42 @@ func TestSupervisorFetchAndProcess_OK(t *testing.T) {
61
59
WithLogger (logger ),
62
60
)
63
61
64
- err = s .FetchAndProcess (ctx , flow1 , sink1 )
65
- assert .NoError (t , err )
62
+ fetcherCallCount := 0
63
+
64
+ messageFetcher .EXPECT ().
65
+ GetMessageForProcessing (gomock .Any (), appConf .Supervisor .ReadyWaitTime , flowId1 , sinkID1 ).AnyTimes ().
66
+ DoAndReturn (func (ctx context.Context , timeout time.Duration , flowID string , sinkID string ) (* models.Message , error ) {
67
+ fetcherCallCount ++
68
+
69
+ if fetcherCallCount == 1 {
70
+ return m , nil
71
+ }
72
+
73
+ return nil , nil
74
+ })
75
+
76
+ messageProcessor .EXPECT ().
77
+ Process (gomock .Any (), sink1 , m ).
78
+ DoAndReturn (func (ctx context.Context , sink * models.Sink , m * models.Message ) error {
79
+ return nil
80
+ })
81
+
82
+ processingResultsService .EXPECT ().HandleOK (gomock .Any (), m ).DoAndReturn (func (ctx context.Context , m * models.Message ) error {
83
+ s .Shutdown ()
84
+ return nil
85
+ })
86
+
87
+ s .HandleReadyQueue (flow1 , sink1 )
66
88
}
67
89
68
- func TestSupervisorFetchAndProcess_Failed (t * testing.T ) {
90
+ func TestSupervisorHandleReadyQueue_Failed (t * testing.T ) {
69
91
ctx := context .Background ()
70
92
71
93
appConf , err := testsupport .InitAppConfig (ctx )
72
94
assert .NoError (t , err )
73
95
74
96
appConf .Supervisor .ErrSleepTime = 0
97
+ appConf .Supervisor .ReadyQueueConcurrency = 1
75
98
76
99
ctrl := gomock .NewController (t )
77
100
defer ctrl .Finish ()
@@ -94,16 +117,10 @@ func TestSupervisorFetchAndProcess_Failed(t *testing.T) {
94
117
ID : mID1 ,
95
118
}
96
119
97
- processingErr := fmt .Errorf ("processing error" )
98
-
99
120
messageFetcher := mocks .NewMockMessageFetcher (ctrl )
100
121
messageProcessor := mocks .NewMockMessageProcessor (ctrl )
101
122
processingResultsService := mocks .NewMockProcessingResultsService (ctrl )
102
123
103
- messageFetcher .EXPECT ().GetMessageForProcessing (ctx , appConf .Supervisor .ReadyWaitTime , flowId1 , sinkID1 ).Return (m , nil )
104
- messageProcessor .EXPECT ().Process (ctx , sink1 , m ).Return (processingErr )
105
- processingResultsService .EXPECT ().HandleFailed (ctx , sink1 , m , processingErr ).Return (& models.QueuedInfo {QueueStatus : models .QueueStatusReady }, nil )
106
-
107
124
logger , err := zap .NewDevelopment ()
108
125
assert .NoError (t , err )
109
126
@@ -115,6 +132,34 @@ func TestSupervisorFetchAndProcess_Failed(t *testing.T) {
115
132
WithLogger (logger ),
116
133
)
117
134
118
- err = s .FetchAndProcess (ctx , flow1 , sink1 )
119
- assert .NoError (t , err )
135
+ processingErr := fmt .Errorf ("processing error" )
136
+
137
+ fetcherCallCount := 0
138
+ messageFetcher .EXPECT ().
139
+ GetMessageForProcessing (gomock .Any (), appConf .Supervisor .ReadyWaitTime , flowId1 , sinkID1 ).AnyTimes ().
140
+ DoAndReturn (func (ctx context.Context , timeout time.Duration , flowID string , sinkID string ) (* models.Message , error ) {
141
+ fetcherCallCount ++
142
+
143
+ if fetcherCallCount == 1 {
144
+ return m , nil
145
+ }
146
+
147
+ return nil , nil
148
+ })
149
+
150
+ messageProcessor .EXPECT ().
151
+ Process (gomock .Any (), sink1 , m ).
152
+ DoAndReturn (func (ctx context.Context , sink * models.Sink , m * models.Message ) error {
153
+ return processingErr
154
+ })
155
+
156
+ processingResultsService .EXPECT ().
157
+ HandleFailed (gomock .Any (), sink1 , m , processingErr ).
158
+ DoAndReturn (func (ctx context.Context , sink * models.Sink , m * models.Message , processingErr error ) (* models.QueuedInfo , error ) {
159
+ s .Shutdown ()
160
+
161
+ return & models.QueuedInfo {QueueStatus : models .QueueStatusReady }, nil
162
+ })
163
+
164
+ s .HandleReadyQueue (flow1 , sink1 )
120
165
}
0 commit comments