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 5a71d8c commit 08e2696
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions internal/persistence_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"math/rand"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -195,7 +196,7 @@ type DelayWriter struct {

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

Expand Down Expand Up @@ -225,26 +226,14 @@ func TestStorePersistence_Readonly(t *testing.T) {
case <-persistDone:
done = true
default:
store.Get(int(counter.Load()) % 1000)
shard := store.shards[rand.Intn(len(store.shards))]
tk := shard.mu.RLock()
shard.mu.RUnlock(tk)
counter.Add(1)
}
}
}()

go func() {
done := false
i := 0
for !done {
select {
case <-persistDone:
done = true
default:
store.Set(100, i, 1, 0)
i++
}
}
}()

f, err := os.Create("stest")
defer os.Remove("stest")
require.Nil(t, err)
Expand All @@ -268,13 +257,7 @@ func TestStorePersistence_Readonly(t *testing.T) {
// read should not be blocked during persistence
require.Greater(t, counter.Load(), uint64(100))

oldv, ok := store.Get(100)
require.True(t, ok)
newv, ok := new.Get(100)
require.True(t, ok)
require.NotEqual(t, oldv, newv)

for i := 0; i < 5000; i++ {
for i := 0; i < 1000; i++ {
new.Get(i)
new.Set(i, 123, 1, 0)
}
Expand Down

0 comments on commit 08e2696

Please sign in to comment.