Skip to content

Commit

Permalink
[fixup] refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jul 31, 2023
1 parent c0c631e commit d5809ee
Showing 1 changed file with 61 additions and 58 deletions.
119 changes: 61 additions & 58 deletions persistence/trees/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,79 +106,82 @@ func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) {
}

func TestTreeStore_SaveAndLoad(t *testing.T) {
ctrl := gomock.NewController(t)
tmpDir := t.TempDir()

mockTxIndexer := mock_types.NewMockTxIndexer(ctrl)
mockBus := mockModules.NewMockBus(ctrl)
mockPersistenceMod := mockModules.NewMockPersistenceModule(ctrl)

mockBus.EXPECT().GetPersistenceModule().AnyTimes().Return(mockPersistenceMod)
mockPersistenceMod.EXPECT().GetTxIndexer().AnyTimes().Return(mockTxIndexer)

ts := &treeStore{
logger: &zerolog.Logger{},
treeStoreDir: tmpDir,
}
require.NoError(t, ts.Start())
require.NotNil(t, ts.rootTree.tree)

for _, treeName := range stateTreeNames {
err := ts.merkleTrees[treeName].tree.Update([]byte("foo"), []byte("bar"))
t.Parallel()
t.Run("should save a backup in a directory", func(t *testing.T) {
ts := newTestTreeStore(t)
tmpdir := t.TempDir()
// assert that the directory is empty before backup
ok, err := isEmpty(tmpdir)
require.NoError(t, err)
}
require.True(t, ok)

err := ts.Commit()
require.NoError(t, err)

hash1 := ts.getStateHash()
require.NotEmpty(t, hash1)
// Trigger a backup
require.NoError(t, ts.Backup(tmpdir))

w, err := ts.save()
require.NoError(t, err)
require.NotNil(t, w)
require.NotNil(t, w.rootHash)
require.NotNil(t, w.merkleRoots)
// assert that the directory is not empty after Backup has returned
ok, err = isEmpty(tmpdir)
require.NoError(t, err)
require.False(t, ok)
})
t.Run("should load a backup and maintain TreeStore hash integrity", func(t *testing.T) {
ctrl := gomock.NewController(t)
tmpDir := t.TempDir()

mockTxIndexer := mock_types.NewMockTxIndexer(ctrl)
mockBus := mockModules.NewMockBus(ctrl)
mockPersistenceMod := mockModules.NewMockPersistenceModule(ctrl)

mockBus.EXPECT().GetPersistenceModule().AnyTimes().Return(mockPersistenceMod)
mockPersistenceMod.EXPECT().GetTxIndexer().AnyTimes().Return(mockTxIndexer)

ts := &treeStore{
logger: logger.Global.CreateLoggerForModule(modules.TreeStoreSubmoduleName),
treeStoreDir: tmpDir,
}
require.NoError(t, ts.Start())
require.NotNil(t, ts.rootTree.tree)

for _, treeName := range stateTreeNames {
err := ts.merkleTrees[treeName].tree.Update([]byte("foo"), []byte("bar"))
require.NoError(t, err)
}

err := ts.Commit()
require.NoError(t, err)

// Stop the first tree store so that it's databases are no longer used
require.NoError(t, ts.Stop())
hash1 := ts.getStateHash()
require.NotEmpty(t, hash1)

// declare a second TreeStore with no trees then load the first worldstate into it
ts2 := &treeStore{
logger: logger.Global.CreateLoggerForModule(modules.TreeStoreSubmoduleName),
treeStoreDir: tmpDir,
}
w, err := ts.save()
require.NoError(t, err)
require.NotNil(t, w)
require.NotNil(t, w.rootHash)
require.NotNil(t, w.merkleRoots)

// Load sets a tree store to the provided worldstate
err = ts2.Load(w)
require.NoError(t, err)
// Stop the first tree store so that it's databases are no longer used
require.NoError(t, ts.Stop())

hash2 := ts2.getStateHash()
// declare a second TreeStore with no trees then load the first worldstate into it
ts2 := &treeStore{
logger: logger.Global.CreateLoggerForModule(modules.TreeStoreSubmoduleName),
treeStoreDir: tmpDir,
}

// Assert that hash is unchanged from save and load
require.Equal(t, hash1, hash2)
}

func TestTreeStore_SaveBackup(t *testing.T) {
ts := newTestTreeStore(t)
tmpdir := t.TempDir()
// assert that the directory is empty before backup
ok, err := isEmpty(tmpdir)
require.NoError(t, err)
require.True(t, ok)
// Load sets a tree store to the provided worldstate
err = ts2.Load(w)
require.NoError(t, err)

// Trigger a backup
require.NoError(t, ts.Backup(tmpdir))
hash2 := ts2.getStateHash()

// assert that the directory is not empty after Backup has returned
ok, err = isEmpty(tmpdir)
require.NoError(t, err)
require.False(t, ok)
// Assert that hash is unchanged from save and load
require.Equal(t, hash1, hash2)
})
}

// creates a new tree store with a tmp directory for nodestore persistence
// and then starts the tree store and returns its pointer.
func newTestTreeStore(t *testing.T) *treeStore {
t.Helper()
ctrl := gomock.NewController(t)
tmpDir := t.TempDir()

Expand Down

0 comments on commit d5809ee

Please sign in to comment.