Skip to content

Commit

Permalink
test: fix data race in snapshots.ChunkReader test (cosmos#7299)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessio Treglia <[email protected]>
Co-authored-by: Alexander Bezobchuk <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2020
1 parent f640ad6 commit 5e16c21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
22 changes: 18 additions & 4 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options
}))
}

snapshotInterval := uint64(2)
snapshotTimeout := 1 * time.Minute
snapshotDir, err := ioutil.TempDir("", "baseapp")
require.NoError(t, err)
snapshotStore, err := snapshots.NewStore(dbm.NewMemDB(), snapshotDir)
Expand All @@ -133,7 +135,7 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options

app := setupBaseApp(t, append(options,
SetSnapshotStore(snapshotStore),
SetSnapshotInterval(2),
SetSnapshotInterval(snapshotInterval),
SetPruning(sdk.PruningOptions{KeepEvery: 1}),
routerOpt)...)

Expand Down Expand Up @@ -161,9 +163,21 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options
app.EndBlock(abci.RequestEndBlock{Height: height})
app.Commit()

// Wait for snapshot to be taken, since it happens asynchronously. This
// heuristic is likely to be flaky on low-IO machines.
time.Sleep(time.Duration(int(height)*blockTxs) * 200 * time.Millisecond)
// Wait for snapshot to be taken, since it happens asynchronously.
if uint64(height)%snapshotInterval == 0 {
start := time.Now()
for {
if time.Since(start) > snapshotTimeout {
t.Errorf("timed out waiting for snapshot after %v", snapshotTimeout)
}
snapshot, err := snapshotStore.Get(uint64(height), snapshottypes.CurrentFormat)
require.NoError(t, err)
if snapshot != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
}
}

return app, teardown
Expand Down
7 changes: 3 additions & 4 deletions snapshots/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func TestChunkWriter(t *testing.T) {
}

func TestChunkReader(t *testing.T) {

ch := makeChunks([][]byte{
{1, 2, 3},
{4},
Expand Down Expand Up @@ -150,9 +149,9 @@ func TestChunkReader(t *testing.T) {
close(pch)

go func() {
chunkReader = snapshots.NewChunkReader(pch)
buf = []byte{0, 0, 0, 0}
_, err = chunkReader.Read(buf)
chunkReader := snapshots.NewChunkReader(pch)
buf := []byte{0, 0, 0, 0}
_, err := chunkReader.Read(buf)
require.NoError(t, err)
assert.Equal(t, []byte{1, 2, 3, 0}, buf)

Expand Down

0 comments on commit 5e16c21

Please sign in to comment.