-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcache_test.go
More file actions
94 lines (82 loc) · 2.04 KB
/
cache_test.go
File metadata and controls
94 lines (82 loc) · 2.04 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
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 range 1000 {
wg.Add(1)
go f()
}
wg.Wait()
}
func TestDeleteNX(t *testing.T) {
c := newCache(100, false)
// Add NXDOMAIN entry
c.addNX("nonexistent.")
rrs := c.get("nonexistent.")
st.Expect(t, len(rrs), 0) // NXDOMAIN returns empty slice
// Delete the NXDOMAIN entry
c.deleteNX("nonexistent.")
rrs = c.get("nonexistent.")
st.Expect(t, rrs, RRs(nil)) // Entry should be completely gone
// Verify deleteNX doesn't affect non-NXDOMAIN entries
rr := RR{Name: "exists.", Type: "A", Value: "1.2.3.4"}
c.add("exists.", rr)
c.deleteNX("exists.") // Should not delete because it's not an NX entry
rrs = c.get("exists.")
st.Expect(t, len(rrs), 1) // Entry should still exist
}
func TestDeleteNXConcurrent(t *testing.T) {
c := newCache(100, false)
var wg sync.WaitGroup
for range 100 {
wg.Add(1)
go func() {
defer wg.Done()
c.addNX("concurrent.")
c.deleteNX("concurrent.")
}()
}
wg.Wait()
}