|
15 | 15 | package confirmations
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
18 | 19 | "fmt"
|
19 | 20 | "strings"
|
20 | 21 | "sync/atomic"
|
@@ -338,38 +339,39 @@ func TestCBLDispatcherFallsBehindHead(t *testing.T) {
|
338 | 339 | mbiHash := mca.On("BlockInfoByHash", mock.Anything, mock.Anything)
|
339 | 340 | mbiHash.Run(func(args mock.Arguments) { mockBlockHashReturn(mbiHash, args, blocks) })
|
340 | 341 |
|
341 |
| - // We'll fall back to this because we don't keep up |
342 |
| - mbiNum := mca.On("BlockInfoByNumber", mock.Anything, mock.Anything) |
343 |
| - mbiNum.Run(func(args mock.Arguments) { mockBlockNumberReturn(mbiNum, args, blocks) }) |
344 |
| - |
345 | 342 | bcm.requiredConfirmations = 5
|
| 343 | + |
| 344 | + // Start a CBL, but then cancel the dispatcher |
346 | 345 | cbl, err := bcm.startConfirmedBlockListener(bcm.ctx, id, "", nil, esDispatch)
|
347 | 346 | assert.NoError(t, err)
|
| 347 | + cbl.cancelFunc() |
| 348 | + <-cbl.dispatcherDone |
| 349 | + <-cbl.processorDone |
| 350 | + |
| 351 | + // Restart just the processor |
| 352 | + cbl.ctx, cbl.cancelFunc = context.WithCancel(bcm.ctx) |
| 353 | + cbl.processorDone = make(chan struct{}) |
| 354 | + go cbl.notificationProcessor() |
348 | 355 |
|
349 |
| - // Notify all the blocks before we process any |
| 356 | + // Notify all the blocks |
350 | 357 | for i := 0; i < len(blocks); i++ {
|
351 | 358 | bcm.propagateBlockHashToCBLs(&ffcapi.BlockHashEvent{
|
352 | 359 | BlockHashes: []string{blocks[i].BlockHash},
|
353 | 360 | })
|
354 | 361 | }
|
355 | 362 |
|
356 |
| - for i := 0; i < len(blocks)-bcm.requiredConfirmations; i++ { |
357 |
| - // The dispatches should have been added, until it got too far ahead |
358 |
| - // and then set to nil. |
359 |
| - for cbl.newHeadToAdd != nil { |
360 |
| - time.Sleep(1 * time.Millisecond) |
361 |
| - } |
362 |
| - b := <-esDispatch |
363 |
| - assert.Equal(t, b.BlockEvent.BlockNumber.Uint64(), blocks[i].BlockNumber.Uint64()) |
364 |
| - assert.Equal(t, b.BlockEvent.BlockInfo, blocks[i].BlockInfo) |
365 |
| - } |
| 363 | + // Wait until there's a first tap to the dispatcher, meaning |
| 364 | + // dispatches should have been added to the newHeadToAdd list |
| 365 | + <-cbl.dispatcherTap |
366 | 366 |
|
367 |
| - time.Sleep(1 * time.Millisecond) |
368 |
| - assert.Len(t, cbl.blocksSinceCheckpoint, bcm.requiredConfirmations) |
369 |
| - select { |
370 |
| - case <-esDispatch: |
371 |
| - assert.Fail(t, "should not have received block in confirmation window") |
372 |
| - default: // good - we should have the confirmations sat there, but no dispatch |
| 367 | + // ... until it got too far ahead and then set to nil. |
| 368 | + checkHeadLen := func() int { |
| 369 | + cbl.stateLock.Lock() |
| 370 | + defer cbl.stateLock.Unlock() |
| 371 | + return len(cbl.newHeadToAdd) |
| 372 | + } |
| 373 | + for checkHeadLen() < 10 { |
| 374 | + time.Sleep(1 * time.Millisecond) |
373 | 375 | }
|
374 | 376 |
|
375 | 377 | bcm.Stop()
|
|
0 commit comments