-
Notifications
You must be signed in to change notification settings - Fork 23
/
cache_test.go
58 lines (52 loc) · 1.19 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
package dnsr
import (
"sync"
"testing"
"time"
"github.com/nbio/st"
)
func TestCache(t *testing.T) {
c := newCache(100, false)
c.addNX("hello.")
rr := RR{Name: "hello.", Type: "A", Value: "1.2.3.4"}
c.add("hello.", rr)
rrs := c.get("hello.")
st.Expect(t, len(rrs), 1)
}
func TestLiveCacheEntry(t *testing.T) {
c := newCache(100, true)
c.addNX("alive.")
alive := time.Now().Add(time.Minute)
rr := RR{Name: "alive.", Type: "A", Value: "1.2.3.4", Expiry: alive}
c.add("alive.", rr)
rrs := c.get("alive.")
st.Expect(t, len(rrs), 1)
}
func TestExpiredCacheEntry(t *testing.T) {
c := newCache(100, true)
c.addNX("expired.")
expired := time.Now().Add(-time.Minute)
rr := RR{Name: "expired.", Type: "A", Value: "1.2.3.4", Expiry: expired}
c.add("expired.", rr)
rrs := c.get("expired.")
st.Expect(t, len(rrs), 0)
}
func TestCacheContention(t *testing.T) {
k := "expired."
c := newCache(10, true)
var wg sync.WaitGroup
f := func() {
rrs := c.get(k)
st.Expect(t, len(rrs), 0)
c.addNX(k)
expired := time.Now().Add(-time.Minute)
rr := RR{Name: k, Type: "A", Value: "1.2.3.4", Expiry: expired}
c.add(k, rr)
wg.Done()
}
for i := 0; i < 1000; i++ {
wg.Add(1)
go f()
}
wg.Wait()
}