forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
68 lines (48 loc) · 935 Bytes
/
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
package main
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewCache(t *testing.T) {
newCache()
}
func TestCacheLoadOrStore(t *testing.T) {
c := newCache()
x, f := c.LoadOrStore("https://foo.com")
assert.Nil(t, x)
assert.NotNil(t, f)
f(42)
x, f = c.LoadOrStore("https://foo.com")
assert.Equal(t, 42, x)
assert.Nil(t, f)
}
func TestCacheLoadOrStoreConcurrency(t *testing.T) {
c := newCache()
x, f := c.LoadOrStore("key")
assert.Nil(t, x)
assert.NotNil(t, f)
go func() {
x, f := c.LoadOrStore("key")
assert.Equal(t, 42, x)
assert.Nil(t, f)
}()
time.Sleep(time.Millisecond)
f(42)
}
func BenchmarkCacheLoadOrStore(b *testing.B) {
c := newCache()
g := &sync.WaitGroup{}
_, f := c.LoadOrStore("https://foo.com")
f(42)
b.ResetTimer()
for i := 0; i < b.N; i++ {
g.Add(1)
go func() {
c.LoadOrStore("https://foo.com")
g.Done()
}()
}
g.Wait()
}