@@ -31,36 +31,38 @@ var (
31
31
)
32
32
33
33
func TestChain (t * testing.T ) {
34
- mockChannel := newChannel ("foo.channel" , defaultPartition )
35
34
36
35
oldestOffset := int64 (0 )
37
36
newestOffset := int64 (5 )
38
37
39
38
message := sarama .StringEncoder ("messageFoo" )
40
39
41
- mockBroker := sarama .NewMockBroker (t , 0 )
42
- defer func () { mockBroker .Close () }()
43
-
44
- mockBroker .SetHandlerByMap (map [string ]sarama.MockResponse {
45
- "MetadataRequest" : sarama .NewMockMetadataResponse (t ).
46
- SetBroker (mockBroker .Addr (), mockBroker .BrokerID ()).
47
- SetLeader (mockChannel .topic (), mockChannel .partition (), mockBroker .BrokerID ()),
48
- "ProduceRequest" : sarama .NewMockProduceResponse (t ).
49
- SetError (mockChannel .topic (), mockChannel .partition (), sarama .ErrNoError ),
50
- "OffsetRequest" : sarama .NewMockOffsetResponse (t ).
51
- SetOffset (mockChannel .topic (), mockChannel .partition (), sarama .OffsetOldest , oldestOffset ).
52
- SetOffset (mockChannel .topic (), mockChannel .partition (), sarama .OffsetNewest , newestOffset ),
53
- "FetchRequest" : sarama .NewMockFetchResponse (t , 1 ).
54
- SetMessage (mockChannel .topic (), mockChannel .partition (), newestOffset , message ),
55
- })
56
-
57
- mockSupport := & mockmultichain.ConsenterSupport {
58
- ChainIDVal : mockChannel .topic (),
59
- HeightVal : uint64 (3 ),
60
- SharedConfigVal : & mockconfig.Orderer {KafkaBrokersVal : []string {mockBroker .Addr ()}},
40
+ newMocks := func (t * testing.T ) (mockChannel channel , mockBroker * sarama.MockBroker , mockSupport * mockmultichain.ConsenterSupport ) {
41
+ mockChannel = newChannel (channelNameForTest (t ), defaultPartition )
42
+ mockBroker = sarama .NewMockBroker (t , 0 )
43
+ mockBroker .SetHandlerByMap (map [string ]sarama.MockResponse {
44
+ "MetadataRequest" : sarama .NewMockMetadataResponse (t ).
45
+ SetBroker (mockBroker .Addr (), mockBroker .BrokerID ()).
46
+ SetLeader (mockChannel .topic (), mockChannel .partition (), mockBroker .BrokerID ()),
47
+ "ProduceRequest" : sarama .NewMockProduceResponse (t ).
48
+ SetError (mockChannel .topic (), mockChannel .partition (), sarama .ErrNoError ),
49
+ "OffsetRequest" : sarama .NewMockOffsetResponse (t ).
50
+ SetOffset (mockChannel .topic (), mockChannel .partition (), sarama .OffsetOldest , oldestOffset ).
51
+ SetOffset (mockChannel .topic (), mockChannel .partition (), sarama .OffsetNewest , newestOffset ),
52
+ "FetchRequest" : sarama .NewMockFetchResponse (t , 1 ).
53
+ SetMessage (mockChannel .topic (), mockChannel .partition (), newestOffset , message ),
54
+ })
55
+ mockSupport = & mockmultichain.ConsenterSupport {
56
+ ChainIDVal : mockChannel .topic (),
57
+ HeightVal : uint64 (3 ),
58
+ SharedConfigVal : & mockconfig.Orderer {KafkaBrokersVal : []string {mockBroker .Addr ()}},
59
+ }
60
+ return
61
61
}
62
62
63
63
t .Run ("New" , func (t * testing.T ) {
64
+ _ , mockBroker , mockSupport := newMocks (t )
65
+ defer func () { mockBroker .Close () }()
64
66
chain , err := newChain (mockConsenter , mockSupport , newestOffset - 1 )
65
67
66
68
assert .NoError (t , err , "Expected newChain to return without errors" )
@@ -87,6 +89,8 @@ func TestChain(t *testing.T) {
87
89
})
88
90
89
91
t .Run ("Start" , func (t * testing.T ) {
92
+ _ , mockBroker , mockSupport := newMocks (t )
93
+ defer func () { mockBroker .Close () }()
90
94
// Set to -1 because we haven't sent the CONNECT message yet
91
95
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
92
96
@@ -103,6 +107,8 @@ func TestChain(t *testing.T) {
103
107
})
104
108
105
109
t .Run ("Halt" , func (t * testing.T ) {
110
+ _ , mockBroker , mockSupport := newMocks (t )
111
+ defer func () { mockBroker .Close () }()
106
112
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
107
113
108
114
chain .Start ()
@@ -132,6 +138,8 @@ func TestChain(t *testing.T) {
132
138
})
133
139
134
140
t .Run ("DoubleHalt" , func (t * testing.T ) {
141
+ _ , mockBroker , mockSupport := newMocks (t )
142
+ defer func () { mockBroker .Close () }()
135
143
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
136
144
137
145
chain .Start ()
@@ -148,6 +156,8 @@ func TestChain(t *testing.T) {
148
156
})
149
157
150
158
t .Run ("StartWithProducerForChannelError" , func (t * testing.T ) {
159
+ _ , mockBroker , mockSupport := newMocks (t )
160
+ defer func () { mockBroker .Close () }()
151
161
// Point to an empty brokers list
152
162
mockSupportCopy := * mockSupport
153
163
mockSupportCopy .SharedConfigVal = & mockconfig.Orderer {KafkaBrokersVal : []string {}}
@@ -164,6 +174,8 @@ func TestChain(t *testing.T) {
164
174
// - Net.ReadTimeout
165
175
// - Consumer.Retry.Backoff
166
176
// - Metadata.Retry.Max
177
+ mockChannel , mockBroker , mockSupport := newMocks (t )
178
+ defer func () { mockBroker .Close () }()
167
179
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
168
180
169
181
// Have the broker return an ErrNotLeaderForPartition error
@@ -184,6 +196,8 @@ func TestChain(t *testing.T) {
184
196
})
185
197
186
198
t .Run ("EnqueueIfNotStarted" , func (t * testing.T ) {
199
+ mockChannel , mockBroker , mockSupport := newMocks (t )
200
+ defer func () { mockBroker .Close () }()
187
201
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
188
202
189
203
// As in StartWithConnectMessageError, have the broker return an
@@ -211,6 +225,9 @@ func TestChain(t *testing.T) {
211
225
// - Consumer.Retry.Backoff
212
226
// - Metadata.Retry.Max
213
227
228
+ mockChannel , mockBroker , mockSupport := newMocks (t )
229
+ defer func () { mockBroker .Close () }()
230
+
214
231
// Provide an out-of-range offset
215
232
chain , _ := newChain (mockConsenter , mockSupport , newestOffset )
216
233
@@ -231,8 +248,8 @@ func TestChain(t *testing.T) {
231
248
})
232
249
233
250
t .Run ("EnqueueProper" , func (t * testing.T ) {
234
- t . Skip ( "Skipping test until FAB-4537 is resolved" )
235
-
251
+ mockChannel , mockBroker , mockSupport := newMocks ( t )
252
+ defer func () { mockBroker . Close () }()
236
253
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
237
254
238
255
mockBroker .SetHandlerByMap (map [string ]sarama.MockResponse {
@@ -264,6 +281,8 @@ func TestChain(t *testing.T) {
264
281
})
265
282
266
283
t .Run ("EnqueueIfHalted" , func (t * testing.T ) {
284
+ mockChannel , mockBroker , mockSupport := newMocks (t )
285
+ defer func () { mockBroker .Close () }()
267
286
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
268
287
269
288
mockBroker .SetHandlerByMap (map [string ]sarama.MockResponse {
@@ -293,6 +312,8 @@ func TestChain(t *testing.T) {
293
312
})
294
313
295
314
t .Run ("EnqueueError" , func (t * testing.T ) {
315
+ mockChannel , mockBroker , mockSupport := newMocks (t )
316
+ defer func () { mockBroker .Close () }()
296
317
chain , _ := newChain (mockConsenter , mockSupport , newestOffset - 1 )
297
318
298
319
// Use the "good" handler map that allows the Stage to complete without
@@ -336,7 +357,7 @@ func TestSetupProducerForChannel(t *testing.T) {
336
357
mockBroker := sarama .NewMockBroker (t , 0 )
337
358
defer mockBroker .Close ()
338
359
339
- mockChannel := newChannel ("foo.channel" , defaultPartition )
360
+ mockChannel := newChannel (channelNameForTest ( t ) , defaultPartition )
340
361
341
362
haltChan := make (chan struct {})
342
363
@@ -361,7 +382,7 @@ func TestSetupConsumerForChannel(t *testing.T) {
361
382
mockBroker := sarama .NewMockBroker (t , 0 )
362
383
defer func () { mockBroker .Close () }()
363
384
364
- mockChannel := newChannel ("foo.channel" , defaultPartition )
385
+ mockChannel := newChannel (channelNameForTest ( t ) , defaultPartition )
365
386
366
387
oldestOffset := int64 (0 )
367
388
newestOffset := int64 (5 )
@@ -412,7 +433,7 @@ func TestSetupConsumerForChannel(t *testing.T) {
412
433
}
413
434
414
435
func TestCloseKafkaObjects (t * testing.T ) {
415
- mockChannel := newChannel ("foo.channel" , defaultPartition )
436
+ mockChannel := newChannel (channelNameForTest ( t ) , defaultPartition )
416
437
417
438
mockSupport := & mockmultichain.ConsenterSupport {
418
439
ChainIDVal : mockChannel .topic (),
@@ -526,7 +547,7 @@ func TestGetLastCutBlockNumber(t *testing.T) {
526
547
}
527
548
528
549
func TestGetLastOffsetPersisted (t * testing.T ) {
529
- mockChannel := newChannel ("foo.channel" , defaultPartition )
550
+ mockChannel := newChannel (channelNameForTest ( t ) , defaultPartition )
530
551
mockMetadata := & cb.Metadata {Value : utils .MarshalOrPanic (& ab.KafkaMetadata {LastOffsetPersisted : int64 (5 )})}
531
552
532
553
testCases := []struct {
0 commit comments