-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
129 lines (104 loc) · 3.04 KB
/
helpers_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
package main
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAlterter(t *testing.T) {
// wrong variable
err := os.Setenv("STARTUP_RETRIES", "d")
assert.Nil(t, err)
// set logging into custom and testable location
logOutput := &bytes.Buffer{}
setLogging("localhost:1234", logOutput)
ctx := signalContext()
// start alerter
errorChannel := make(chan error, 20)
// set context so we can cancel the listner(s)
ctx, cancel := context.WithCancel(context.Background())
go alerter(ctx, errorChannel)
cancel()
c, err := getConfig()
assert.NotNil(t, err)
assert.Equal(t, &config{}, c)
assert.Equal(t, "invalid value passed for STARTUP_RETRIES: strconv.Atoi: parsing \"d\": invalid syntax", err.Error())
// confirm that we got an error logged
assert.Contains(t, string(logOutput.Bytes()), "logging to stderr & graylog2@'localhost:1234'")
}
func TestHandler(t *testing.T) {
w := httptest.NewRecorder()
r := &http.Request{Method: "POST"}
handler(w, r)
assert.Equal(t, "200 OK", w.Result().Status)
}
func TestServerStop(t *testing.T) {
// set logging into custom and testable location
logOutput := &bytes.Buffer{}
setLogging("", logOutput)
os.Setenv("STARTUP_RETRIES", "")
ctx := signalContext()
// start alerter
errorChannel := make(chan error, 20)
// set context so we can cancel the listner(s)
ctx, cancel := context.WithCancel(context.Background())
go alerter(ctx, errorChannel)
cancel()
c, err := getConfig()
assert.Nil(t, err)
// start server...
startServer(ctx, c, errorChannel)
// cancel context - as would happen when signalled
cancel()
// confirm that we got shutdown message in log
messagesShouldBe := []string{
"http listener stopping",
"http listener stopped",
}
for _, message := range messagesShouldBe {
assert.Contains(t, string(logOutput.Bytes()), message)
}
}
func TestGather(t *testing.T) {
getPeersFunc := func(c config) (peers, error) {
return nil, errors.New("this is a bad error")
}
// set logging into custom and testable location
logOutput := &bytes.Buffer{}
config, err := getConfig()
config.StartupDelay = 0
assert.Nil(t, err)
setLogging("", logOutput)
os.Setenv("STARTUP_RETRIES", "")
// start alerter
errorChannel := make(chan error, 20)
ctx, cancel := context.WithCancel(context.Background())
go alerter(ctx, errorChannel)
client := getClient(errorChannel, *config)
wg := sync.WaitGroup{}
go client.gather(*config, getPeersFunc, &wg)
// need a delay to allow previous to actually DO something before killing them with cancel()
time.Sleep(1 * time.Second)
cancel()
c, err := getConfig()
assert.Nil(t, err)
// start server...
startServer(ctx, c, errorChannel)
// cancel context - as would happen when signalled
cancel()
// confirm that we got shutdown message in log
messagesShouldBe := []string{
"http listener stopping",
"http listener stopped",
"this is a bad error",
}
for _, message := range messagesShouldBe {
assert.Contains(t, string(logOutput.Bytes()), message)
}
}