-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrentcache_test.go
More file actions
52 lines (44 loc) · 1.1 KB
/
concurrentcache_test.go
File metadata and controls
52 lines (44 loc) · 1.1 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
package concurrentcache
import (
"bytes"
"testing"
"time"
)
func TestConcurrentCache(t *testing.T) {
c := NewConcurrentCache[map[string][]uint8](make(map[string][]uint8), time.Second, func(locker Locker, cache map[string][]uint8) {
locker.Lock()
defer locker.Unlock()
for key, value := range cache {
if key == "key" {
if value[0] == 4 {
delete(cache, key)
return
}
for i := range value {
value[i] += value[i]
}
}
}
})
defer c.Close()
c.AccessWrite(func(cache map[string][]uint8) {
cache["key"] = []uint8{1, 2, 3}
})
c.AccessRead(func(cache map[string][]uint8) {
if value := cache["key"]; !bytes.Equal(value, []uint8{1, 2, 3}) {
t.Error("Expected [1, 2, 3] but got", value)
}
})
time.Sleep(2500 * time.Millisecond)
c.AccessRead(func(cache map[string][]uint8) {
if value := cache["key"]; !bytes.Equal(value, []uint8{4, 8, 12}) {
t.Error("Expected [4, 8, 12] but got", value)
}
})
time.Sleep(time.Second)
c.AccessRead(func(cache map[string][]uint8) {
if value := cache["key"]; value != nil {
t.Error("Expected nil but got", value)
}
})
}