Skip to content

Commit

Permalink
fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J committed Jan 12, 2025
1 parent ec0a517 commit 75be4be
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions internal/persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ func TestStorePersistence_Resize(t *testing.T) {

}

type DelayWriter struct {
f *os.File
beforeWrite func()
}

func (dw *DelayWriter) Write(p []byte) (n int, err error) {
dw.beforeWrite()
time.Sleep(2 * time.Second)
return dw.f.Write(p)
}

func TestStorePersistence_Readonly(t *testing.T) {
store := NewStore[int, int](1000, false, true, nil, nil, nil, 0, 0, nil)
for i := 0; i < 1000; i++ {
Expand All @@ -204,8 +215,11 @@ func TestStorePersistence_Readonly(t *testing.T) {
require.True(t, ok)
require.Equal(t, 100, v)

startCounter := make(chan bool)
started := false
go func() {
done := false
<-startCounter
for !done {
select {
case <-persistDone:
Expand Down Expand Up @@ -234,8 +248,12 @@ func TestStorePersistence_Readonly(t *testing.T) {
f, err := os.Create("stest")
defer os.Remove("stest")
require.Nil(t, err)
start := counter.Load()
err = store.Persist(0, f)
err = store.Persist(0, &DelayWriter{f: f, beforeWrite: func() {
if !started {
close(startCounter)
started = true
}
}})
require.Nil(t, err)
f.Close()
persistDone <- true
Expand All @@ -247,7 +265,8 @@ func TestStorePersistence_Readonly(t *testing.T) {
require.Nil(t, err)
f.Close()

require.True(t, counter.Load()-start > 10)
// read should not be blocked during persistence
require.Greater(t, counter.Load(), uint64(100))

oldv, ok := store.Get(100)
require.True(t, ok)
Expand Down

0 comments on commit 75be4be

Please sign in to comment.