Skip to content

Commit

Permalink
Add test for parallel reads with stats (#199)
Browse files Browse the repository at this point in the history
Regresion test for #198
  • Loading branch information
janisz authored and cristaloleg committed Jan 8, 2020
1 parent 035b28c commit 5780d3f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ func TestCacheDelRandomly(t *testing.T) {
Verbose: false,
Hasher: newDefaultHasher(),
HardMaxCacheSize: 1,
StatsEnabled: true,
Logger: DefaultLogger(),
}

Expand Down Expand Up @@ -463,6 +464,39 @@ func TestCacheDelRandomly(t *testing.T) {
wg.Wait()
}

func TestWriteAndReadParallelSameKeyWithStats(t *testing.T) {
t.Parallel()

c := DefaultConfig(0)
c.StatsEnabled = true

cache, _ := NewBigCache(c)
var wg sync.WaitGroup
ntest := 1000
n := 10
wg.Add(n)
key := "key"
value := blob('a', 1024)
for i := 0; i < ntest; i++ {
assertEqual(t, nil, cache.Set(key, value))
}
for j := 0; j < n; j++ {
go func() {
for i := 0; i < ntest; i++ {
v, err := cache.Get(key)
assertEqual(t, nil, err)
assertEqual(t, value, v)
}
wg.Done()
}()
}

wg.Wait()

assertEqual(t, Stats{Hits: int64(n * ntest)}, cache.Stats())
assertEqual(t, ntest*n, int(cache.KeyMetadata(key).RequestCount))
}

func TestCacheReset(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 5780d3f

Please sign in to comment.