-
Notifications
You must be signed in to change notification settings - Fork 4
/
grid_test.go
99 lines (82 loc) · 2.14 KB
/
grid_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
package grid_test
import (
"context"
"net/http"
"testing"
"github.com/boson-project/grid"
)
func StartGrid(t *testing.T) (g *grid.Grid, err error) {
t.Helper()
listening := make(chan bool)
errCh := make(chan error)
g = grid.New(
grid.WithAddress("127.0.0.1:"), // OS-chosen port
grid.WithOnListen(func() { listening <- true }), // signal start
)
go func() {
if err := g.Serve(context.Background()); err != nil {
errCh <- err
}
}()
select {
case err = <-errCh:
case <-listening:
}
return g, err
}
// TestCancel ensures the service starts and stops without error with all defaults using
// a cancelable context.
func TestStart(t *testing.T) {
// A context which, when canceled, triggers a graceful shutdown of the server.
ctx, cancel := context.WithCancel(context.Background())
// Grid instance which immediately triggers a shutdown when listening.
g := grid.New(grid.WithOnListen(cancel))
// Serve, which should return without error on graceful shutdown.
if err := g.Serve(ctx); err != nil {
t.Fatal(err)
}
}
// TestVersion ensures that the /v1/version endpoint returns the version structure.
func TestVersion(t *testing.T) {
g, err := StartGrid(t)
if err != nil {
t.Fatal(err)
}
res, err := http.Get("http://" + g.Addr().String() + "/v1/version")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("Expected HTTP 200, got %v", res.StatusCode)
}
}
func TestEventsEndpointAvailable(t *testing.T) {
g, err := StartGrid(t)
if err != nil {
t.Fatal(err)
}
res, err := http.Get("http://" + g.Addr().String() + "/v1/events")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("Expected HTTP 200, got %v", res.StatusCode)
}
}
func TestSubscriptionsEndpointAvailable(t *testing.T) {
g, err := StartGrid(t)
if err != nil {
t.Fatal(err)
}
res, err := http.Get("http://" + g.Addr().String() + "/v1/subscriptions")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("Expected HTTP 200, got %v", res.StatusCode)
}
}
// See handlers_test.go for individual endpoint unit tests.