Skip to content

Commit

Permalink
test(store/snapshot): fix flaky test (cosmos#20078)
Browse files Browse the repository at this point in the history
Co-authored-by: cool-developer <[email protected]>
  • Loading branch information
EmilGeorgiev and cool-develope authored Apr 26, 2024
1 parent e856c74 commit 560e6fb
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions store/snapshots/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func TestStore_Prune(t *testing.T) {
}

func TestStore_Save(t *testing.T) {
t.Parallel()
store := setupStore(t)
// Saving a snapshot should work
snapshot, err := store.Save(4, 1, makeChunks([][]byte{{1}, {2}}))
Expand Down Expand Up @@ -318,19 +319,40 @@ 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)
}()
mtx.Lock()
defer mtx.Unlock()
_, err = store.Save(7, 2, makeChunks(nil))
require.Error(t, err)
_, err = store.Save(8, 1, makeChunks(nil))
require.NoError(t, err)
close(ch)
var (
wgStart, wgDone sync.WaitGroup
mu sync.Mutex
gotErrHeights []uint64
)
srcHeights := []uint64{7, 7, 7, 8, 9}
wgStart.Add(len(srcHeights))
wgDone.Add(len(srcHeights))
for _, h := range srcHeights {
ch = make(chan io.ReadCloser, 1)
ch <- &ReadCloserMock{} // does not block on a buffered channel
close(ch)
go func(height uint64) {
wgStart.Done()
wgStart.Wait() // wait for all routines started
if _, err = store.Save(height, 1, ch); err != nil {
mu.Lock()
gotErrHeights = append(gotErrHeights, height)
mu.Unlock()
}
wgDone.Done()
}(h)
}
wgDone.Wait() // wait for all routines completed
assert.Equal(t, []uint64{7, 7}, gotErrHeights)
}

type ReadCloserMock struct {
}

func (r ReadCloserMock) Read(p []byte) (n int, err error) {
return len(p), io.EOF
}

func (r ReadCloserMock) Close() error {
return nil
}

0 comments on commit 560e6fb

Please sign in to comment.