chore: fix the flaky test in store/snapshots (#19774)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
cool-developer
2024-03-18 16:23:22 +00:00
committed by GitHub
co-authored by Aleksandr Bezobchuk
parent 6f1658e5db
commit 25af567521
3 changed files with 9 additions and 19 deletions
-14
View File
@@ -7,7 +7,6 @@ import (
"crypto/sha256"
"errors"
"io"
"os"
"testing"
"time"
@@ -288,16 +287,3 @@ func (s *extSnapshotter) RestoreExtension(height uint64, format uint32, payloadR
// finalize restoration
return nil
}
// GetTempDir returns a writable temporary director for the test to use.
func GetTempDir(tb testing.TB) string {
tb.Helper()
// os.MkDir() is used instead of testing.T.TempDir()
// see https://github.com/cosmos/cosmos-sdk/pull/8475 and
// https://github.com/cosmos/cosmos-sdk/pull/10341 for
// this change's rationale.
tempdir, err := os.MkdirTemp("", "")
require.NoError(tb, err)
tb.Cleanup(func() { _ = os.RemoveAll(tempdir) })
return tempdir
}
+1 -1
View File
@@ -236,7 +236,7 @@ func TestManager_Restore(t *testing.T) {
func TestManager_TakeError(t *testing.T) {
snapshotter := &mockErrorCommitSnapshotter{}
store, err := snapshots.NewStore(GetTempDir(t))
store, err := snapshots.NewStore(t.TempDir())
require.NoError(t, err)
manager := snapshots.NewManager(store, opts, snapshotter, &mockStorageSnapshotter{}, nil, log.NewNopLogger())
+8 -4
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"io"
"sync"
"testing"
"time"
@@ -16,7 +17,7 @@ import (
func setupStore(t *testing.T) *snapshots.Store {
t.Helper()
store, err := snapshots.NewStore(GetTempDir(t))
store, err := snapshots.NewStore(t.TempDir())
require.NoError(t, err)
_, err = store.Save(1, 1, makeChunks([][]byte{
@@ -40,8 +41,7 @@ func setupStore(t *testing.T) *snapshots.Store {
}
func TestNewStore(t *testing.T) {
tempdir := GetTempDir(t)
_, err := snapshots.NewStore(tempdir)
_, err := snapshots.NewStore(t.TempDir())
require.NoError(t, err)
}
@@ -319,11 +319,15 @@ func TestStore_Save(t *testing.T) {
// Saving a snapshot should error if a snapshot is already in progress for the same height,
// regardless of format. However, a different height should succeed.
ch = make(chan io.ReadCloser)
mtx := sync.Mutex{}
mtx.Lock()
go func() {
mtx.Unlock()
_, err := store.Save(7, 1, ch)
require.NoError(t, err)
}()
time.Sleep(10 * time.Millisecond)
mtx.Lock()
defer mtx.Unlock()
_, err = store.Save(7, 2, makeChunks(nil))
require.Error(t, err)
_, err = store.Save(8, 1, makeChunks(nil))