github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/epoch_test.go (about)

     1  package operation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  	"github.com/koko1123/flow-go-1/utils/unittest"
    11  )
    12  
    13  func TestEpochEmergencyFallback(t *testing.T) {
    14  
    15  	// the block ID where EECC was triggered
    16  	blockID := unittest.IdentifierFixture()
    17  
    18  	t.Run("reading when unset should return false", func(t *testing.T) {
    19  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    20  			var triggered bool
    21  			err := db.View(CheckEpochEmergencyFallbackTriggered(&triggered))
    22  			assert.NoError(t, err)
    23  			assert.False(t, triggered)
    24  		})
    25  	})
    26  	t.Run("should be able to set flag to true", func(t *testing.T) {
    27  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    28  			// set the flag, ensure no error
    29  			err := db.Update(SetEpochEmergencyFallbackTriggered(blockID))
    30  			assert.NoError(t, err)
    31  
    32  			// read the flag, should be true now
    33  			var triggered bool
    34  			err = db.View(CheckEpochEmergencyFallbackTriggered(&triggered))
    35  			assert.NoError(t, err)
    36  			assert.True(t, triggered)
    37  
    38  			// read the value of the block ID, should match
    39  			var storedBlockID flow.Identifier
    40  			err = db.View(RetrieveEpochEmergencyFallbackTriggeredBlockID(&storedBlockID))
    41  			assert.NoError(t, err)
    42  			assert.Equal(t, blockID, storedBlockID)
    43  		})
    44  	})
    45  	t.Run("setting flag multiple time should have no additional effect", func(t *testing.T) {
    46  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    47  			// set the flag, ensure no error
    48  			err := db.Update(SetEpochEmergencyFallbackTriggered(blockID))
    49  			assert.NoError(t, err)
    50  
    51  			// set the flag, should have no error and no effect on state
    52  			err = db.Update(SetEpochEmergencyFallbackTriggered(unittest.IdentifierFixture()))
    53  			assert.NoError(t, err)
    54  
    55  			// read the flag, should be true
    56  			var triggered bool
    57  			err = db.View(CheckEpochEmergencyFallbackTriggered(&triggered))
    58  			assert.NoError(t, err)
    59  			assert.True(t, triggered)
    60  
    61  			// read the value of block ID, should equal the FIRST set ID
    62  			var storedBlockID flow.Identifier
    63  			err = db.View(RetrieveEpochEmergencyFallbackTriggeredBlockID(&storedBlockID))
    64  			assert.NoError(t, err)
    65  			assert.Equal(t, blockID, storedBlockID)
    66  		})
    67  	})
    68  }