-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
179 lines (146 loc) · 6.24 KB
/
server_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package shimgo
import (
"errors"
"fmt"
"os"
"sync"
"testing"
)
func TestServersHaveCorrectInitialValues(t *testing.T) {
assert(t, serverCache != nil, "global instance is initialized")
assert(t, len(serverCache.backends) == 3, "servers are less than expected")
for format, server := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
assert(t, len(server.errors) == 0, "there are no errors")
assert(t, len(server.workingDirectory) != 0, "working is defined", server.workingDirectory)
_, err := os.Stat(server.workingDirectory)
assert(t, !os.IsNotExist(err), "working directory exists:", server.workingDirectory)
assert(t, server.pid == 0, "pid is zeroed")
cleanup(t, server)
})
}
}
func TestAddError(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
assert(t, len(s.errors) == 0, "there should be no errors and there are:", len(s.errors))
assert(t, !s.hasError(), "has error should not report error but does")
assert(t, s.getError() == nil, "get error should be nil")
s.addError(nil)
assert(t, len(s.errors) == 0, "there should be no errors and there are:", len(s.errors))
assert(t, !s.hasError(), "has error should not report error but does")
assert(t, s.getError() == nil, "get error should be nil")
s.addError(errors.New("foo"))
assert(t, len(s.errors) == 1, "there should be one error and there are:", len(s.errors))
assert(t, s.hasError(), "has error should report error but does not")
assert(t, s.getError() != nil, "get error should not be nil")
wg := &sync.WaitGroup{}
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for n := 0; n < 10; n++ {
s.addError(errors.New("bar"))
}
}()
}
wg.Wait()
assert(t, len(s.errors) == 201, "there are many errors (201), and are actually:", len(s.errors))
assert(t, s.hasError(), "has error should report error but does not")
assert(t, s.getError() != nil, "get error should not be nil")
cleanup(t, s)
})
}
}
func TestStartingService(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
require(t, !s.running, "server shouldn't be running at start, but is", fmt.Sprintf("%+v", s))
assert(t, !s.isRunning(), "isRunning method should reflect that server is not yet running")
s.start()
require(t, s.running, "server should be running after starting, but isn't", fmt.Sprintf("%+v", s))
assert(t, s.isRunning(), "isRunning method should reflect that server is running")
assert(t, !s.hasTerminated(), "server should not report terminated before it has")
cleanup(t, s)
})
}
}
func TestStartIfNeeded(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
require(t, !s.running, "server shouldn't be running at start, but is", fmt.Sprintf("%+v", s))
assert(t, !s.isRunning(), "isRunning method should reflect that server is not yet running")
err := s.startIfNeeded()
require(t, err == nil, "server should not error when starting", fmt.Sprintf("%+v", err))
require(t, s.running, "server should be running after starting, but isn't", fmt.Sprintf("%+v", s))
assert(t, s.isRunning(), "isRunning method should reflect that server is running")
assert(t, s.pid != 0, "pid is set because server is running")
cleanup(t, s)
s = newServer(s.backend)
s.addError(errors.New("blocker"))
assert(t, s.hasError(), "error should be here")
assert(t, !s.running, "server shouldn't start if it has errors")
assert(t, !s.isRunning(), "server isn't running and shouldn't report that")
err = s.startIfNeeded()
require(t, err != nil, "server should have mocked error")
assert(t, !s.running, "server shouldn't start if it has errors")
assert(t, !s.isRunning(), "server isn't running and shouldn't report that")
assert(t, s.pid == 0, "pid isnt set because server is running")
cleanup(t, s)
s = newServer(s.backend)
s.running = true
assert(t, s.isRunning(), "test faked running attribute and the method should reflect that")
assert(t, !s.hasError(), "no errrors")
err = s.startIfNeeded()
require(t, err == nil, "server should not error when starting", fmt.Sprintf("%+v", err))
assert(t, s.pid == 0, "pid isnt set because server is running")
assert(t, !s.hasError(), "no errrors")
s.setup()
})
}
}
func TestErrorConditionsWhenStarting(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
wd := s.workingDirectory
_, err := os.Stat(wd)
assert(t, !os.IsNotExist(err), "working directory should be created by constructor:", wd)
s.workingDirectory = s.workingDirectory + "-DOES-NOT-EXSIT"
assert(t, s.workingDirectory != wd, "post modification wd shouldn't be correct")
_, err = os.Stat(s.workingDirectory)
assert(t, os.IsNotExist(err), "invalid working directory shouldn't exist:", s.workingDirectory)
s.start()
assert(t, s.hasError(), "server should have error after starting with a broken working directory", s.getError())
assert(t, !s.isRunning(), "server shouldn't think it's running when it fails to start")
s.workingDirectory = wd // set things right so that cleanup happens
cleanup(t, s)
})
}
}
func TestStartStop(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
s.start()
assert(t, s.isRunning(), "server should run after its started")
s.stop()
assert(t, s.hasTerminated(), "server should report as terminated after it has")
assert(t, !s.isRunning(), "server shouldn't be running after its started")
cleanup(t, s)
})
}
}
func TestServersStopIsSafeToRunAfterCleanup(t *testing.T) {
for format, s := range serverCache.backends {
t.Run(fmt.Sprint(format), func(t *testing.T) {
s.start()
assert(t, s.isRunning(), "server should run after its started")
s.stop()
assert(t, s.hasTerminated(), "server knows it's terminated (0)")
assert(t, !s.isRunning(), "server shouldn't be running after its started (0)")
s.stop()
assert(t, s.hasTerminated(), "server knows it's terminated (1)")
assert(t, !s.isRunning(), "server shouldn't be running after its started (1)")
cleanup(t, s)
})
}
}