@@ -425,6 +425,88 @@ func TestPublishParallelMode(t *testing.T) {
425425 }
426426}
427427
428+ func TestRunPublishLoopPublishesWhenBehind (t * testing.T ) {
429+ defer func () { config .Cfg = config.Config {} }()
430+ config .Cfg .Publisher .Mode = "parallel"
431+ config .Cfg .Publisher .Enabled = false
432+
433+ mockRPC := mocks .NewMockIRPCClient (t )
434+ mockMainStorage := mocks .NewMockIMainStorage (t )
435+ mockStagingStorage := mocks .NewMockIStagingStorage (t )
436+ mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
437+ mockStorage := storage.IStorage {
438+ MainStorage : mockMainStorage ,
439+ StagingStorage : mockStagingStorage ,
440+ OrchestratorStorage : mockOrchestratorStorage ,
441+ }
442+ committer := NewCommitter (mockRPC , mockStorage )
443+ committer .workMode = WorkModeLive
444+ committer .setLastCommittedBlock (big .NewInt (102 ))
445+
446+ chainID := big .NewInt (1 )
447+ blockData := []common.BlockData {
448+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (101 )}},
449+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (102 )}},
450+ }
451+
452+ publishDone := make (chan struct {})
453+
454+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
455+ mockStagingStorage .EXPECT ().GetLastPublishedBlockNumber (chainID ).Return (big .NewInt (100 ), nil )
456+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
457+ mockStagingStorage .EXPECT ().GetLastPublishedBlockNumber (chainID ).Return (big .NewInt (100 ), nil )
458+ mockStagingStorage .EXPECT ().GetStagingData (mock .Anything ).Return (blockData , nil )
459+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
460+
461+ ctx , cancel := context .WithCancel (context .Background ())
462+ mockStagingStorage .EXPECT ().SetLastPublishedBlockNumber (chainID , big .NewInt (102 )).RunAndReturn (func (* big.Int , * big.Int ) error {
463+ close (publishDone )
464+ cancel ()
465+ return nil
466+ })
467+
468+ go committer .runPublishLoop (ctx , time .Millisecond )
469+
470+ select {
471+ case <- publishDone :
472+ case <- time .After (2 * time .Second ):
473+ t .Fatal ("publish not triggered" )
474+ }
475+ }
476+
477+ func TestRunPublishLoopSkipsWhenAhead (t * testing.T ) {
478+ defer func () { config .Cfg = config.Config {} }()
479+ config .Cfg .Publisher .Mode = "parallel"
480+ config .Cfg .Publisher .Enabled = false
481+
482+ mockRPC := mocks .NewMockIRPCClient (t )
483+ mockMainStorage := mocks .NewMockIMainStorage (t )
484+ mockStagingStorage := mocks .NewMockIStagingStorage (t )
485+ mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
486+ mockStorage := storage.IStorage {
487+ MainStorage : mockMainStorage ,
488+ StagingStorage : mockStagingStorage ,
489+ OrchestratorStorage : mockOrchestratorStorage ,
490+ }
491+ committer := NewCommitter (mockRPC , mockStorage )
492+ committer .workMode = WorkModeLive
493+ committer .setLastCommittedBlock (big .NewInt (102 ))
494+
495+ chainID := big .NewInt (1 )
496+
497+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
498+ mockStagingStorage .EXPECT ().GetLastPublishedBlockNumber (chainID ).Return (big .NewInt (105 ), nil )
499+
500+ ctx , cancel := context .WithCancel (context .Background ())
501+ go committer .runPublishLoop (ctx , time .Millisecond )
502+ time .Sleep (2 * time .Millisecond )
503+ cancel ()
504+ time .Sleep (10 * time .Millisecond )
505+
506+ mockStagingStorage .AssertNotCalled (t , "GetStagingData" , mock .Anything )
507+ mockStagingStorage .AssertNotCalled (t , "SetLastPublishedBlockNumber" , mock .Anything , mock .Anything )
508+ }
509+
428510func TestInitializeParallelPublisherZero (t * testing.T ) {
429511 defer func () { config .Cfg = config.Config {} }()
430512 config .Cfg .Publisher .Mode = "parallel"
0 commit comments