This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcache_test.go
More file actions
152 lines (137 loc) · 4.08 KB
/
cache_test.go
File metadata and controls
152 lines (137 loc) · 4.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package cedar
import (
"context"
"testing"
"time"
"github.com/evergreen-ci/utility"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnvironmentCache(t *testing.T) {
t.Run("PutNewGetAndDelete", func(t *testing.T) {
cache := newEnvironmentCache()
require.True(t, cache.PutNew("key0", "val0"))
assert.False(t, cache.PutNew("key0", "val0"))
require.True(t, cache.PutNew("key1", 1))
assert.False(t, cache.PutNew("key1", 1))
val0, ok := cache.Get("key0")
assert.True(t, ok)
assert.Equal(t, "val0", val0)
val1, ok := cache.Get("key1")
assert.True(t, ok)
assert.Equal(t, 1, val1)
cache.Delete("key0")
val0, ok = cache.Get("key0")
assert.False(t, ok)
assert.Nil(t, val0)
})
t.Run("WithUpdater", func(t *testing.T) {
cache := newEnvironmentCache()
key0 := "key0"
val0 := "value0"
updates0 := make(chan interface{})
ctx0, cancel0 := context.WithCancel(context.Background())
require.True(t, cache.PutNew(key0, val0))
require.True(t, cache.RegisterUpdater(ctx0, cancel0, key0, updates0))
key1 := "key1"
val1 := 5
updates1 := make(chan interface{})
ctx1, cancel1 := context.WithCancel(context.Background())
require.True(t, cache.PutNew(key1, val1))
require.True(t, cache.RegisterUpdater(ctx1, cancel1, key1, updates1))
t.Run("ReturnsInitialValue", func(t *testing.T) {
val, ok := cache.Get(key0)
require.True(t, ok)
assert.Equal(t, val0, val)
val, ok = cache.Get(key1)
require.True(t, ok)
assert.Equal(t, val1, val)
})
t.Run("FailsWhenUpdaterAlreadyExists", func(t *testing.T) {
assert.False(t, cache.RegisterUpdater(ctx0, cancel0, key0, make(chan interface{})))
})
t.Run("FailsWhenKeyDNE", func(t *testing.T) {
assert.False(t, cache.RegisterUpdater(ctx0, cancel0, "DNE", make(chan interface{})))
})
t.Run("ReturnsUpdatedValue", func(t *testing.T) {
var val interface{}
lastVal, ok := cache.Get(key0)
require.True(t, ok)
newVal0 := "new_value0"
updates0 <- newVal0
retryOp := func() (bool, error) {
val, ok = cache.Get(key0)
if !ok {
return false, errors.New("value not found in cache")
}
if lastVal == val {
return true, errors.New("value not updated")
}
return false, nil
}
assert.NoError(t, utility.Retry(context.TODO(), retryOp, utility.RetryOptions{MaxAttempts: 5}))
assert.Equal(t, newVal0, val)
lastVal, ok = cache.Get(key1)
require.True(t, ok)
newVal1 := 10
updates1 <- newVal1
retryOp = func() (bool, error) {
val, ok = cache.Get(key1)
if !ok {
return false, errors.New("value not found in cache")
}
if lastVal == val {
return true, errors.New("value not updated")
}
return false, nil
}
assert.NoError(t, utility.Retry(context.TODO(), retryOp, utility.RetryOptions{MaxAttempts: 5}))
assert.Equal(t, newVal1, val)
})
t.Run("DeletesValueOnUpdateErr", func(t *testing.T) {
err := errors.New("some error")
updates0 <- err
timer := time.NewTimer(time.Second)
defer timer.Stop()
select {
case <-ctx0.Done():
case <-timer.C:
}
assert.Error(t, ctx0.Err())
_, ok := cache.Get(key0)
assert.False(t, ok)
_, ok = cache.updaterClosers[key0]
assert.False(t, ok)
lastVal, ok := cache.Get(key1)
newVal1 := 20
updates1 <- newVal1
var val interface{}
retryOp := func() (bool, error) {
val, ok = cache.Get(key1)
if !ok {
return false, errors.New("value not found in cache")
}
if lastVal == val {
return true, errors.New("value not updated")
}
return false, nil
}
assert.NoError(t, utility.Retry(context.TODO(), retryOp, utility.RetryOptions{MaxAttempts: 5}))
assert.Equal(t, newVal1, val)
})
t.Run("DeletesValueOnCtxErr", func(t *testing.T) {
cancel1()
retryOp := func() (bool, error) {
_, ok := cache.Get(key1)
if ok {
return true, errors.New("value still found in cache")
}
return false, nil
}
assert.NoError(t, utility.Retry(context.TODO(), retryOp, utility.RetryOptions{MaxAttempts: 5}))
_, ok := cache.updaterClosers[key1]
assert.False(t, ok)
})
})
}