-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
78 lines (60 loc) · 1.65 KB
/
client_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
package gridlock
import (
"context"
"net/http/httptest"
"testing"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/assert"
"github.com/luno/gridlock/api"
"github.com/luno/gridlock/server/handlers"
"github.com/luno/gridlock/server/ops"
)
type state struct {
Log ops.TrafficStats
}
func (s state) TrafficStats() ops.TrafficStats {
return s.Log
}
func TestClientSubmitsMetrics(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
db := ops.NewMemDB()
s := state{Log: ops.NewLoader(ctx, db, db)}
srv := httptest.NewServer(handlers.CreateRouter(ctx, s))
t.Cleanup(srv.Close)
c := NewClient(
WithBaseURL(srv.URL),
WithHTTPClient(srv.Client()),
)
go func() {
err := c.Deliver(ctx)
jtest.Assert(t, context.Canceled, err)
}()
c.Record(Method{
Source: "server1", SourceRegion: "region-a",
Target: "server2", TargetRegion: "region-a",
}, CallGood)
c.Record(Method{
Source: "server1", SourceRegion: "region-a",
Target: "server2", TargetRegion: "region-a",
}, CallGood)
c.Record(Method{
Source: "server1", SourceRegion: "region-a",
Target: "server2", TargetRegion: "region-a",
}, CallBad)
<-c.Record(Method{
Source: "server2", SourceRegion: "region-a",
Target: "server1", TargetRegion: "region-a",
}, CallWarning)
jtest.RequireNil(t, c.Flush(ctx))
traffic, err := c.GetTraffic(ctx)
jtest.RequireNil(t, err)
for i := range traffic {
assert.NotZero(t, traffic[i].Ts)
traffic[i].Ts = 0
}
assert.Equal(t, []api.Traffic{
{Duration: 60, From: "server1", To: "server2", CountGood: 2, CountBad: 1},
{Duration: 60, From: "server2", To: "server1", CountWarning: 1},
}, traffic)
}