forked from elastic/go-freelru
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncedlru_test.go
54 lines (46 loc) · 1.23 KB
/
syncedlru_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// nolint: dupl
package freelru
import (
"sync"
"testing"
)
// TestSyncedRaceCondition tests that the synced LRU is safe to use concurrently.
// Test with 'go test . -race'.
func TestSyncedRaceCondition(t *testing.T) {
const CAP = 4
lru, err := NewSynced[uint64, int](CAP, hashUint64)
if err != nil {
t.Fatalf("err: %v", err)
}
wg := sync.WaitGroup{}
call := func(fn func()) {
wg.Add(1)
go func() {
fn()
wg.Done()
}()
}
call(func() { lru.SetLifetime(1) })
call(func() { lru.SetOnEvict(nil) })
call(func() { _ = lru.Len() })
call(func() { _ = lru.AddWithLifetime(1, 1, 0) })
call(func() { _ = lru.Add(1, 1) })
call(func() { _, _ = lru.Get(1) })
call(func() { _, _ = lru.GetAndRefresh(1) })
call(func() { _, _ = lru.Peek(1) })
call(func() { _ = lru.Contains(1) })
call(func() { _ = lru.Remove(1) })
call(func() { _, _, _ = lru.RemoveOldest() })
call(func() { _ = lru.Keys() })
call(func() { lru.Purge() })
call(func() { lru.PurgeExpired() })
call(func() { lru.Metrics() })
call(func() { _ = lru.ResetMetrics() })
call(func() { lru.dump() })
call(func() { lru.PrintStats() })
wg.Wait()
}
func TestSyncedLRUMetrics(t *testing.T) {
cache, _ := NewSynced[uint64, uint64](1, hashUint64)
testMetrics(t, cache)
}