test: fix data race in snapshots.ChunkReader test (#7299)

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Erik Grinaker 2020-09-14 15:00:08 +02:00 committed by GitHub
parent f640ad6cea
commit 5e16c215ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View File

@ -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)
@ -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)...)
@ -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

View File

@ -83,7 +83,6 @@ func TestChunkWriter(t *testing.T) {
}
func TestChunkReader(t *testing.T) {
ch := makeChunks([][]byte{
{1, 2, 3},
{4},
@ -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)