Skip to content

Commit

Permalink
Test: ttl cache multi-threading (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 3, 2024
1 parent d3dc094 commit 01b70a2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/api/cache/ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package cache

import (
"crypto/rand"
"fmt"
"math/big"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -118,6 +120,43 @@ func TestTTLCache_SetGet(t *testing.T) {
require.Len(t, c.queue, 4)
require.Len(t, c.m, 3)
})

t.Run("multithread", func(t *testing.T) {
c := NewTTLCache(Config{MaxEntitiesCount: 10}, time.Millisecond)

var wg sync.WaitGroup
set := func(wg *sync.WaitGroup) {
wg.Done()

for i := 0; i < 100; i++ {
val, err := rand.Int(rand.Reader, big.NewInt(255))
require.NoError(t, err)
c.Set(val.String(), []byte{byte(i)})
}
}
get := func(wg *sync.WaitGroup) {
wg.Done()

for i := 0; i < 100; i++ {
c.Get(fmt.Sprintf("%d", i))
}
}

for i := 0; i < 100; i++ {
wg.Add(2)
set(&wg)
get(&wg)
}

wg.Wait()

require.Len(t, c.queue, 10)
require.Len(t, c.m, 10)

for key := range c.m {
require.Contains(t, c.queue, key)
}
})
}

func TestTTLCache_Clear(t *testing.T) {
Expand Down

0 comments on commit 01b70a2

Please sign in to comment.