-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain_test.go
More file actions
142 lines (135 loc) · 3.17 KB
/
main_test.go
File metadata and controls
142 lines (135 loc) · 3.17 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
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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ErikKalkoken/evebuddy/internal/app/pcache"
"github.com/ErikKalkoken/evebuddy/internal/app/testutil"
"github.com/ErikKalkoken/evebuddy/internal/xgoesi"
"github.com/ErikKalkoken/evebuddy/internal/xslices"
)
func TestCacheAdapter(t *testing.T) {
db, st, _ := testutil.NewDBInMemory()
defer db.Close()
pc := pcache.New(st, 0)
ca := newHTTPCacheAdapter(pc, "prefix", 0)
t.Run("get existing key", func(t *testing.T) {
pc.Clear()
ca.Set("a", []byte("alpha"))
got, ok := ca.Get("a")
if assert.True(t, ok) {
assert.Equal(t, []byte("alpha"), got)
}
})
t.Run("get non existing key", func(t *testing.T) {
pc.Clear()
_, ok := ca.Get("a")
assert.False(t, ok)
})
t.Run("delete existing key", func(t *testing.T) {
pc.Clear()
ca.Set("a", []byte("alpha"))
ca.Delete("a")
_, ok := ca.Get("a")
assert.False(t, ok)
})
}
func TestSetupCrashFile(t *testing.T) {
p := filepath.Join(t.TempDir(), crashFileName)
err := setupCrashFile(p)
if assert.NoError(t, err) {
_, err := os.Stat(p)
assert.NoError(t, err)
}
}
func TestShouldRetryOn420s(t *testing.T) {
responses := []struct {
statusCode int
remain int
reset int
body string
}{
{
http.StatusOK,
100,
60,
"dummy",
},
{
xgoesi.StatusTooManyErrors,
0,
1,
"dummy",
},
}
var callCount int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, ok := xslices.Pop(&responses)
if !ok {
t.Fatal("out of test reponses")
}
w.Header().Set("X-ESI-Error-Limit-Remain", strconv.Itoa(resp.remain))
w.Header().Set("X-ESI-Error-Limit-Reset", strconv.Itoa(resp.reset))
w.WriteHeader(resp.statusCode)
fmt.Fprint(w, resp.body)
callCount++
}))
defer ts.Close()
client := retryablehttp.NewClient()
client.RetryMax = 1
client.CheckRetry = customCheckRetry
client.Backoff = customBackoff
client.HTTPClient.Transport = &xgoesi.RateLimiter{}
resp, err := client.Get(ts.URL)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 2, callCount)
}
func TestShouldRetryOn429s(t *testing.T) {
responses := []struct {
statusCode int
retryAfter int
body string
}{
{
http.StatusOK,
0,
"dummy",
},
{
http.StatusTooManyRequests,
1,
"dummy",
},
}
var callCount int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, ok := xslices.Pop(&responses)
if !ok {
t.Fatal("out of test reponses")
}
if resp.retryAfter > 0 {
w.Header().Set("Retry-After", strconv.Itoa(resp.retryAfter))
}
w.WriteHeader(resp.statusCode)
fmt.Fprint(w, resp.body)
callCount++
}))
defer ts.Close()
client := retryablehttp.NewClient()
client.RetryMax = 1
client.CheckRetry = customCheckRetry
client.Backoff = customBackoff
client.HTTPClient.Transport = &xgoesi.RateLimiter{}
resp, err := client.Get(ts.URL)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 2, callCount)
}