-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_test.go
158 lines (140 loc) · 3.43 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
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
153
154
155
156
157
158
package poeapi
import (
"container/ring"
"testing"
"time"
)
func TestNewResponseCache(t *testing.T) {
_, err := newResponseCache(1)
if err != nil {
t.Fatalf("failed to create cache: %v", err)
}
}
func TestInvalidCacheSize(t *testing.T) {
_, err := newResponseCache(0)
if err != ErrInvalidCacheSize {
t.Fatal("failed to detect invalid cache size")
}
}
func TestCacheEviction(t *testing.T) {
cache, err := newResponseCache(10)
if err != nil {
t.Fatalf("failed to create cache for operations test: %v", err)
}
cache.Set("1", "A")
cache.Set("2", "B")
cache.Set("3", "C")
cache.Set("4", "D")
cache.Set("5", "E")
cache.Set("6", "F")
cache.Set("7", "G")
cache.Set("8", "H")
cache.Set("9", "I")
val, err := cache.Get("5")
if err != nil {
t.Fatalf("failed to get from cache: %v", err)
}
if val != "E" {
t.Fatalf("unexpected cache result: got %s, expected E", val)
}
cache.Set("foo", "foo")
cache.Set("bar", "bar")
_, err = cache.Get("1")
if err != ErrNotFoundInCache {
t.Fatalf("failed to get from cache: %v", err)
}
}
func TestCacheLatency(t *testing.T) {
c, err := NewAPIClient(ClientOptions{
Host: testHost,
NinjaHost: DefaultNinjaHost,
UseSSL: false,
UseCache: true,
CacheSize: DefaultCacheSize,
RateLimit: UnlimitedRate,
StashRateLimit: UnlimitedRate,
RequestTimeout: testTimeout,
})
if err != nil {
t.Fatalf("failed to create client for latency test: %v", err)
}
_, err = c.GetLeague(GetLeagueOptions{ID: "Standard"})
if err != nil {
t.Fatalf("failed to get all leagues for latency test: %v", err)
}
start2 := time.Now()
_, err = c.GetLeague(GetLeagueOptions{ID: "Standard"})
if err != nil {
t.Fatalf("failed to get all leagues for second latency test: %v", err)
}
duration2 := time.Since(start2)
if duration2 > 2*time.Millisecond {
t.Fatal("cache test took longer than 1ms")
}
}
func TestCacheExistingKey(t *testing.T) {
cache, err := newResponseCache(10)
if err != nil {
t.Fatalf("failed to create cache for existing key test: %v", err)
}
cache.Set("foo", "bar")
cache.Set("foo", "bar")
}
func TestDNSCacheResolve(t *testing.T) {
var (
host = "localhost"
d = newDNSCache()
)
if err := d.resolve(host); err != nil {
t.Fatalf("failed to resolve in dns cache: %v", err)
}
}
func TestDNSCacheGetUnresolved(t *testing.T) {
var (
host = "localhost"
d = newDNSCache()
)
_, err := d.Get(host)
if err != nil {
t.Fatalf("failed to get ip from dns cache: %v", err)
}
}
func TestDNSCacheResolutionFailure(t *testing.T) {
var (
host = "11111"
d = newDNSCache()
)
if err := d.resolve(host); err == nil {
// GitHub Actions Workaround: 11111 resolves to 0.0.43.103.
if d.IPs[host].Next().Value != "0.0.43.103" {
t.Fatal("failed to detect dns resolution failure")
}
}
}
func TestDNSCacheGetResolutionFailure(t *testing.T) {
var (
host = "11111"
d = newDNSCache()
)
if _, err := d.Get(host); err == nil {
// GitHub Actions Workaround: 11111 resolves to 0.0.43.103.
if d.IPs[host].Next().Value != "0.0.43.103" {
t.Fatal("failed to detect dns resolution failure")
}
}
}
func TestDNSCacheInvalidEntry(t *testing.T) {
var (
host = "test"
d = newDNSCache()
entryCount = 3
)
d.IPs[host] = ring.New(entryCount)
for i := 0; i < entryCount; i++ {
d.IPs[host].Value = i
d.IPs[host].Next()
}
if _, err := d.Get(host); err != ErrInvalidAddress {
t.Fatal("failed to detect invalid dns cache entry")
}
}