@@ -2,6 +2,7 @@ package orchestrator
22
33import (
44 "context"
5+ "errors"
56 "math/big"
67 "testing"
78 "time"
@@ -324,9 +325,10 @@ func TestCommit(t *testing.T) {
324325 committer := NewCommitter (mockRPC , mockStorage )
325326 committer .workMode = WorkModeBackfill
326327
328+ chainID := big .NewInt (1 )
327329 blockData := []common.BlockData {
328- {Block : common.Block {Number : big .NewInt (101 )}},
329- {Block : common.Block {Number : big .NewInt (102 )}},
330+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (101 )}},
331+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (102 )}},
330332 }
331333
332334 // Create a channel to signal when DeleteStagingData is called
@@ -350,6 +352,81 @@ func TestCommit(t *testing.T) {
350352 }
351353}
352354
355+ func TestCommitPreCommitPublisherMode (t * testing.T ) {
356+ defer func () { config .Cfg = config.Config {} }()
357+ config .Cfg .Publisher .Mode = "pre-commit"
358+
359+ mockRPC := mocks .NewMockIRPCClient (t )
360+ mockMainStorage := mocks .NewMockIMainStorage (t )
361+ mockStagingStorage := mocks .NewMockIStagingStorage (t )
362+ mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
363+ mockStorage := storage.IStorage {
364+ MainStorage : mockMainStorage ,
365+ StagingStorage : mockStagingStorage ,
366+ OrchestratorStorage : mockOrchestratorStorage ,
367+ }
368+ committer := NewCommitter (mockRPC , mockStorage )
369+ committer .workMode = WorkModeLive
370+
371+ chainID := big .NewInt (1 )
372+ blockData := []common.BlockData {
373+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (101 )}},
374+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (102 )}},
375+ }
376+
377+ publishDone := make (chan struct {})
378+
379+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
380+ mockStagingStorage .EXPECT ().GetLastPublishedBlockNumber (chainID ).Return (big .NewInt (100 ), nil )
381+ mockStagingStorage .EXPECT ().SetLastPublishedBlockNumber (chainID , big .NewInt (102 )).RunAndReturn (func (* big.Int , * big.Int ) error {
382+ close (publishDone )
383+ return nil
384+ })
385+ mockMainStorage .EXPECT ().InsertBlockData (blockData ).Return (nil )
386+
387+ err := committer .commit (context .Background (), blockData )
388+ assert .NoError (t , err )
389+
390+ select {
391+ case <- publishDone :
392+ case <- time .After (2 * time .Second ):
393+ t .Fatal ("SetLastPublishedBlockNumber was not called" )
394+ }
395+ }
396+
397+ func TestCommitPreCommitPublisherModeFallback (t * testing.T ) {
398+ defer func () { config .Cfg = config.Config {} }()
399+ config .Cfg .Publisher .Mode = "pre-commit"
400+
401+ mockRPC := mocks .NewMockIRPCClient (t )
402+ mockMainStorage := mocks .NewMockIMainStorage (t )
403+ mockStagingStorage := mocks .NewMockIStagingStorage (t )
404+ mockOrchestratorStorage := mocks .NewMockIOrchestratorStorage (t )
405+ mockStorage := storage.IStorage {
406+ MainStorage : mockMainStorage ,
407+ StagingStorage : mockStagingStorage ,
408+ OrchestratorStorage : mockOrchestratorStorage ,
409+ }
410+ committer := NewCommitter (mockRPC , mockStorage )
411+ committer .workMode = WorkModeLive
412+
413+ chainID := big .NewInt (1 )
414+ blockData := []common.BlockData {
415+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (101 )}},
416+ {Block : common.Block {ChainId : chainID , Number : big .NewInt (102 )}},
417+ }
418+
419+ mockRPC .EXPECT ().GetChainID ().Return (chainID )
420+ mockStagingStorage .EXPECT ().GetLastPublishedBlockNumber (chainID ).Return (nil , errors .New ("boom" ))
421+ mockMainStorage .EXPECT ().InsertBlockData (blockData ).Return (nil )
422+
423+ err := committer .commit (context .Background (), blockData )
424+ assert .NoError (t , err )
425+
426+ time .Sleep (100 * time .Millisecond )
427+ mockStagingStorage .AssertNotCalled (t , "SetLastPublishedBlockNumber" , mock .Anything , mock .Anything )
428+ }
429+
353430func TestHandleGap (t * testing.T ) {
354431 mockRPC := mocks .NewMockIRPCClient (t )
355432 mockMainStorage := mocks .NewMockIMainStorage (t )
0 commit comments