Skip to content

Commit d157f48

Browse files
committed
Update mock checks
- Construct the mock objects with their `.New___()` method instead of using golang's built-in `new` function, which enables automatic cleanup. - Replace explicit AssertCalled checks at the end of tests with .On() and .Once() which will automatically be checked at the end of the test.
1 parent 9e37f14 commit d157f48

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed

module/finalizer/collection/finalizer_test.go

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestFinalizer(t *testing.T) {
6464
bootstrap()
6565
defer cleanup()
6666

67-
prov := new(collectionmock.GuaranteedCollectionPublisher)
67+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
6868
prov.On("SubmitCollectionGuarantee", mock.Anything)
6969
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
7070

@@ -77,7 +77,7 @@ func TestFinalizer(t *testing.T) {
7777
bootstrap()
7878
defer cleanup()
7979

80-
prov := new(collectionmock.GuaranteedCollectionPublisher)
80+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
8181
prov.On("SubmitCollectionGuarantee", mock.Anything)
8282
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
8383

@@ -103,7 +103,7 @@ func TestFinalizer(t *testing.T) {
103103
bootstrap()
104104
defer cleanup()
105105

106-
prov := new(collectionmock.GuaranteedCollectionPublisher)
106+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
107107
prov.On("SubmitCollectionGuarantee", mock.Anything)
108108
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
109109

@@ -122,7 +122,7 @@ func TestFinalizer(t *testing.T) {
122122
bootstrap()
123123
defer cleanup()
124124

125-
prov := new(collectionmock.GuaranteedCollectionPublisher)
125+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
126126
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
127127

128128
// create a block with empty payload on genesis
@@ -147,8 +147,7 @@ func TestFinalizer(t *testing.T) {
147147
bootstrap()
148148
defer cleanup()
149149

150-
prov := new(collectionmock.GuaranteedCollectionPublisher)
151-
prov.On("SubmitCollectionGuarantee", mock.Anything)
150+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
152151
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
153152

154153
// tx1 is included in the finalized block and mempool
@@ -163,6 +162,15 @@ func TestFinalizer(t *testing.T) {
163162
block.SetPayload(model.PayloadFromTransactions(refBlock.ID(), &tx1))
164163
insert(block)
165164

165+
// block should be passed to provider
166+
prov.On("SubmitCollectionGuarantee", &flow.CollectionGuarantee{
167+
CollectionID: block.Payload.Collection.ID(),
168+
ReferenceBlockID: refBlock.ID(),
169+
ChainID: block.Header.ChainID,
170+
SignerIndices: block.Header.ParentVoterIndices,
171+
Signature: nil,
172+
}).Once()
173+
166174
// finalize the block
167175
err := finalizer.MakeFinal(block.ID())
168176
assert.Nil(t, err)
@@ -177,25 +185,14 @@ func TestFinalizer(t *testing.T) {
177185
assert.Nil(t, err)
178186
assert.Equal(t, block.ID(), final.ID())
179187
assertClusterBlocksIndexedByReferenceHeight(t, db, refBlock.Height, final.ID())
180-
181-
// block should be passed to provider
182-
prov.AssertNumberOfCalls(t, "SubmitCollectionGuarantee", 1)
183-
prov.AssertCalled(t, "SubmitCollectionGuarantee", &flow.CollectionGuarantee{
184-
CollectionID: block.Payload.Collection.ID(),
185-
ReferenceBlockID: refBlock.ID(),
186-
ChainID: block.Header.ChainID,
187-
SignerIndices: block.Header.ParentVoterIndices,
188-
Signature: nil,
189-
})
190188
})
191189

192190
// when finalizing a block with un-finalized ancestors, those ancestors should be finalized as well
193191
t.Run("finalize multiple blocks together", func(t *testing.T) {
194192
bootstrap()
195193
defer cleanup()
196194

197-
prov := new(collectionmock.GuaranteedCollectionPublisher)
198-
prov.On("SubmitCollectionGuarantee", mock.Anything)
195+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
199196
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
200197

201198
// tx1 is included in the first finalized block and mempool
@@ -215,6 +212,22 @@ func TestFinalizer(t *testing.T) {
215212
block2.SetPayload(model.PayloadFromTransactions(refBlock.ID(), &tx2))
216213
insert(block2)
217214

215+
// both blocks should be passed to provider
216+
prov.On("SubmitCollectionGuarantee", &flow.CollectionGuarantee{
217+
CollectionID: block1.Payload.Collection.ID(),
218+
ReferenceBlockID: refBlock.ID(),
219+
ChainID: block1.Header.ChainID,
220+
SignerIndices: block1.Header.ParentVoterIndices,
221+
Signature: nil,
222+
}).Once()
223+
prov.On("SubmitCollectionGuarantee", &flow.CollectionGuarantee{
224+
CollectionID: block2.Payload.Collection.ID(),
225+
ReferenceBlockID: refBlock.ID(),
226+
ChainID: block2.Header.ChainID,
227+
SignerIndices: block2.Header.ParentVoterIndices,
228+
Signature: nil,
229+
}).Once()
230+
218231
// finalize block2 (should indirectly finalize block1 as well)
219232
err := finalizer.MakeFinal(block2.ID())
220233
assert.Nil(t, err)
@@ -228,31 +241,13 @@ func TestFinalizer(t *testing.T) {
228241
assert.Nil(t, err)
229242
assert.Equal(t, block2.ID(), final.ID())
230243
assertClusterBlocksIndexedByReferenceHeight(t, db, refBlock.Height, block1.ID(), block2.ID())
231-
232-
// both blocks should be passed to provider
233-
prov.AssertNumberOfCalls(t, "SubmitCollectionGuarantee", 2)
234-
prov.AssertCalled(t, "SubmitCollectionGuarantee", &flow.CollectionGuarantee{
235-
CollectionID: block1.Payload.Collection.ID(),
236-
ReferenceBlockID: refBlock.ID(),
237-
ChainID: block1.Header.ChainID,
238-
SignerIndices: block1.Header.ParentVoterIndices,
239-
Signature: nil,
240-
})
241-
prov.AssertCalled(t, "SubmitCollectionGuarantee", &flow.CollectionGuarantee{
242-
CollectionID: block2.Payload.Collection.ID(),
243-
ReferenceBlockID: refBlock.ID(),
244-
ChainID: block2.Header.ChainID,
245-
SignerIndices: block2.Header.ParentVoterIndices,
246-
Signature: nil,
247-
})
248244
})
249245

250246
t.Run("finalize with un-finalized child", func(t *testing.T) {
251247
bootstrap()
252248
defer cleanup()
253249

254-
prov := new(collectionmock.GuaranteedCollectionPublisher)
255-
prov.On("SubmitCollectionGuarantee", mock.Anything)
250+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
256251
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
257252

258253
// tx1 is included in the finalized parent block and mempool
@@ -272,6 +267,15 @@ func TestFinalizer(t *testing.T) {
272267
block2.SetPayload(model.PayloadFromTransactions(refBlock.ID(), &tx2))
273268
insert(block2)
274269

270+
// block should be passed to provider
271+
prov.On("SubmitCollectionGuarantee", &flow.CollectionGuarantee{
272+
CollectionID: block1.Payload.Collection.ID(),
273+
ReferenceBlockID: refBlock.ID(),
274+
ChainID: block1.Header.ChainID,
275+
SignerIndices: block1.Header.ParentVoterIndices,
276+
Signature: nil,
277+
}).Once()
278+
275279
// finalize block1 (should NOT finalize block2)
276280
err := finalizer.MakeFinal(block1.ID())
277281
assert.Nil(t, err)
@@ -286,25 +290,14 @@ func TestFinalizer(t *testing.T) {
286290
assert.Nil(t, err)
287291
assert.Equal(t, block1.ID(), final.ID())
288292
assertClusterBlocksIndexedByReferenceHeight(t, db, refBlock.Height, block1.ID())
289-
290-
// block should be passed to provider
291-
prov.AssertNumberOfCalls(t, "SubmitCollectionGuarantee", 1)
292-
prov.AssertCalled(t, "SubmitCollectionGuarantee", &flow.CollectionGuarantee{
293-
CollectionID: block1.Payload.Collection.ID(),
294-
ReferenceBlockID: refBlock.ID(),
295-
ChainID: block1.Header.ChainID,
296-
SignerIndices: block1.Header.ParentVoterIndices,
297-
Signature: nil,
298-
})
299293
})
300294

301295
// when finalizing a block with a conflicting fork, the fork should not be finalized.
302296
t.Run("conflicting fork", func(t *testing.T) {
303297
bootstrap()
304298
defer cleanup()
305299

306-
prov := new(collectionmock.GuaranteedCollectionPublisher)
307-
prov.On("SubmitCollectionGuarantee", mock.Anything)
300+
prov := collectionmock.NewGuaranteedCollectionPublisher(t)
308301
finalizer := collection.NewFinalizer(db, pool, prov, metrics)
309302

310303
// tx1 is included in the finalized block and mempool
@@ -324,6 +317,15 @@ func TestFinalizer(t *testing.T) {
324317
block2.SetPayload(model.PayloadFromTransactions(refBlock.ID(), &tx2))
325318
insert(block2)
326319

320+
// block should be passed to provider
321+
prov.On("SubmitCollectionGuarantee", &flow.CollectionGuarantee{
322+
CollectionID: block1.Payload.Collection.ID(),
323+
ReferenceBlockID: refBlock.ID(),
324+
ChainID: block1.Header.ChainID,
325+
SignerIndices: block1.Header.ParentVoterIndices,
326+
Signature: nil,
327+
}).Once()
328+
327329
// finalize block1
328330
err := finalizer.MakeFinal(block1.ID())
329331
assert.Nil(t, err)
@@ -338,16 +340,6 @@ func TestFinalizer(t *testing.T) {
338340
assert.Nil(t, err)
339341
assert.Equal(t, block1.ID(), final.ID())
340342
assertClusterBlocksIndexedByReferenceHeight(t, db, refBlock.Height, block1.ID())
341-
342-
// block should be passed to provider
343-
prov.AssertNumberOfCalls(t, "SubmitCollectionGuarantee", 1)
344-
prov.AssertCalled(t, "SubmitCollectionGuarantee", &flow.CollectionGuarantee{
345-
CollectionID: block1.Payload.Collection.ID(),
346-
ReferenceBlockID: refBlock.ID(),
347-
ChainID: block1.Header.ChainID,
348-
SignerIndices: block1.Header.ParentVoterIndices,
349-
Signature: nil,
350-
})
351343
})
352344
})
353345
}

0 commit comments

Comments
 (0)