-
Notifications
You must be signed in to change notification settings - Fork 13
/
autocache_test.go
92 lines (88 loc) · 2.24 KB
/
autocache_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
package autocache
import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/groupcache"
"github.com/hashicorp/memberlist"
)
// todo(bdd): test coverage could be improved by using memberlist's mock
// todo(bdd): by design, groupcache's http pool panics if initialized twice
func TestNew(t *testing.T) {
tests := []struct {
name string
o *Options
seed []string
path string
wantErr bool
wantStatus int
wantBody string
}{
{"complete config",
&Options{
PoolContext: func(_ *http.Request) context.Context { return context.TODO() },
PoolTransportFn: func(_ context.Context) http.RoundTripper { return http.DefaultTransport },
PoolOptions: &groupcache.HTTPPoolOptions{BasePath: "/"},
},
[]string{"localhost"},
"/no_such_group/2/",
false,
404,
"no such group: no_such_group\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := New(tt.o)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
_, err = s.Join(nil)
if err != nil {
t.Fatal(err)
}
_, err = s.Join(tt.seed)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", tt.path, nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
if status := w.Code; status != tt.wantStatus {
t.Errorf("status code: got %v want %v", status, tt.wantStatus)
}
if tt.wantBody != "" {
body := w.Body.String()
if body != tt.wantBody {
t.Errorf("wrong body\n%s \n %s", body, tt.wantBody)
}
}
ip, _, err := net.ParseCIDR("192.0.2.1/24")
if err != nil {
t.Fatal(err)
}
s.NotifyJoin(&memberlist.Node{Addr: ip})
if len(s.peers) != 2 {
t.Errorf("NotifyJoin failed")
}
s.NotifyLeave(&memberlist.Node{Addr: ip})
if len(s.peers) != 1 {
t.Errorf("NotifyLeave failed")
}
s.NotifyUpdate(&memberlist.Node{Addr: ip})
if len(s.peers) != 1 {
t.Errorf("NotifyUpdate failed")
}
// check nill conditions
s.GroupcachePool = nil
r = httptest.NewRequest("GET", tt.path, nil)
w = httptest.NewRecorder()
s.ServeHTTP(w, r)
if status := w.Code; status != 500 {
t.Errorf("status code: got %v want %v", status, 500)
}
})
}
}