-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathcache_test.go
109 lines (90 loc) · 2.55 KB
/
cache_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley
// https://pb33f.io
package index
import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
)
// NewTestSpecIndex Test helper function to create a SpecIndex with initialised high cache.
func NewTestSpecIndex() *SpecIndex {
index := &SpecIndex{}
index.InitHighCache()
return index
}
// SimpleCache struct and methods are assumed to be imported from the respective package
// TestCreateNewCache tests that a new cache is correctly created.
func TestCreateNewCache(t *testing.T) {
cache := CreateNewCache()
assert.NotNil(t, cache)
assert.NotNil(t, cache.GetStore())
}
// TestSetAndGetStore tests that the store is correctly set and retrieved.
func TestSetAndGetStore(t *testing.T) {
cache := CreateNewCache()
newStore := &sync.Map{}
cache.SetStore(newStore)
assert.Equal(t, newStore, cache.GetStore())
}
// TestLoadAndStore tests that a value can be stored and loaded from the cache.
func TestLoadAndStore(t *testing.T) {
cache := CreateNewCache()
key, value := "key", "value"
cache.Store(key, value)
loadedValue, ok := cache.Load(key)
assert.True(t, ok)
assert.Equal(t, value, loadedValue)
// Test for a key that doesn't exist
_, ok = cache.Load("non-existent")
assert.False(t, ok)
}
// TestAddHit tests that hits are incremented correctly.
func TestAddHit(t *testing.T) {
cache := CreateNewCache()
initialHits := cache.GetHits()
cache.AddHit()
newHits := cache.GetHits()
assert.Equal(t, initialHits+1, newHits)
}
// TestAddMiss tests that misses are incremented correctly.
func TestAddMiss(t *testing.T) {
cache := CreateNewCache()
initialMisses := cache.GetMisses()
cache.AddMiss()
newMisses := cache.GetMisses()
assert.Equal(t, initialMisses+1, newMisses)
}
// TestClear tests that the cache is correctly cleared.
func TestClear(t *testing.T) {
cache := CreateNewCache()
key, value := "key", "value"
cache.Store(key, value)
cache.Clear()
_, ok := cache.Load(key)
assert.False(t, ok)
}
// TestConcurrentAccess tests that the cache supports concurrent access.
func TestConcurrentAccess(t *testing.T) {
cache := CreateNewCache()
var wg sync.WaitGroup
// Run 1000 concurrent Store operations
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
cache.Store(i, i)
}(i)
}
// Run 1000 concurrent Load operations
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
cache.Load(i)
}(i)
}
wg.Wait()
// Check for consistency in hits/misses
assert.True(t, cache.GetHits() >= 0)
assert.True(t, cache.GetMisses() >= 0)
}