-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle_server_test.go
179 lines (146 loc) · 4.46 KB
/
lifecycle_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
//go:build integration
package goomerang_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.eloylp.dev/goomerang/client"
"go.eloylp.dev/goomerang/internal/test"
"go.eloylp.dev/goomerang/message"
"go.eloylp.dev/goomerang/server"
)
func TestShutdownProcedureServerSideInit(t *testing.T) {
t.Parallel()
serverArbiter := test.NewArbiter(t)
clientArbiter := test.NewArbiter(t)
s, run := Server(t, server.WithOnCloseHook(func() {
serverArbiter.ItsAFactThat("SERVER_PROPERLY_CLOSED")
}),
server.WithOnStatusChangeHook(statusChangesHook(serverArbiter, "server")),
server.WithOnErrorHook(noErrorHook(serverArbiter)),
)
run()
_, connect := Client(t,
client.WithServerAddr(s.Addr()),
client.WithOnCloseHook(func() {
clientArbiter.ItsAFactThat("CLIENT_PROPERLY_CLOSED")
}),
client.WithOnStatusChangeHook(statusChangesHook(clientArbiter, "client")),
client.WithOnErrorHook(noErrorHook(clientArbiter)),
)
connect()
require.NoError(t, s.Shutdown(defaultCtx))
serverArbiter.RequireNoErrors()
clientArbiter.RequireNoErrors()
serverArbiter.RequireHappenedInOrder(
"SERVER_WAS_NEW",
"SERVER_WAS_RUNNING",
"SERVER_WAS_CLOSING",
"SERVER_WAS_CLOSED",
"SERVER_PROPERLY_CLOSED",
)
clientArbiter.RequireHappenedInOrder(
"CLIENT_WAS_NEW",
"CLIENT_WAS_RUNNING",
"CLIENT_WAS_CLOSING",
"CLIENT_WAS_CLOSED",
"CLIENT_PROPERLY_CLOSED",
)
}
func TestServerSendsCloseToAllClients(t *testing.T) {
t.Parallel()
arbiter := test.NewArbiter(t)
errHook := func(err error) {
arbiter.ErrorHappened(err)
}
s, run := Server(t, server.WithOnErrorHook(errHook))
run()
_, connect1 := Client(t,
client.WithServerAddr(s.Addr()),
client.WithOnErrorHook(errHook),
client.WithOnCloseHook(func() {
arbiter.ItsAFactThat("CLIENT1_RECEIVED_CLOSE")
}))
connect1()
_, connect2 := Client(t,
client.WithServerAddr(s.Addr()),
client.WithOnErrorHook(errHook),
client.WithOnCloseHook(func() {
arbiter.ItsAFactThat("CLIENT2_RECEIVED_CLOSE")
}))
connect2()
require.NoError(t, s.Shutdown(defaultCtx))
arbiter.RequireNoErrors()
arbiter.RequireHappened("CLIENT1_RECEIVED_CLOSE")
arbiter.RequireHappened("CLIENT2_RECEIVED_CLOSE")
}
func TestServerCannotSendMessagesIfNotRunning(t *testing.T) {
t.Parallel()
s, run := Server(t)
_, err := s.Broadcast(defaultCtx, defaultMsg())
assert.ErrorIs(t, err, server.ErrNotRunning, "should not broadcast messages if not connected")
run()
err = s.Shutdown(defaultCtx)
require.NoError(t, err)
_, err = s.Broadcast(defaultCtx, defaultMsg())
assert.ErrorIs(t, err, server.ErrNotRunning, "should not broadcast messages on closed status")
}
func TestServerCannotShutdownIfNotRunning(t *testing.T) {
t.Parallel()
s, _ := Server(t)
err := s.Shutdown(defaultCtx)
assert.ErrorIs(t, err, server.ErrNotRunning, "should not shutdown if not running")
}
func TestServerCannotShutdownIfClosed(t *testing.T) {
t.Parallel()
s, run := Server(t)
run()
err := s.Shutdown(defaultCtx)
require.NoError(t, err)
err = s.Shutdown(defaultCtx)
assert.ErrorIs(t, err, server.ErrNotRunning, "should not shutdown if already closed")
}
func TestServerCannotRunIfAlreadyRunning(t *testing.T) {
t.Parallel()
s, run := Server(t)
run()
defer s.Shutdown(defaultCtx)
err := s.Run()
assert.ErrorIs(t, err, server.ErrAlreadyRunning, "should not run if already running")
}
func TestServerCannotRunIfAlreadyClosed(t *testing.T) {
t.Parallel()
s, run := Server(t)
run()
require.NoError(t, s.Shutdown(defaultCtx))
err := s.Run()
assert.ErrorIs(t, err, server.ErrClosed, "should not run if already closed")
}
func TestServerHandlerCannotSendIfClosed(t *testing.T) {
t.Parallel()
arbiter := test.NewArbiter(t)
s, run := Server(t)
s.Handle(defaultMsg().Payload, message.HandlerFunc(func(s message.Sender, msg *message.Message) {
// We wait for the closed state to send,
// as we expect an error.
time.Sleep(50 * time.Millisecond)
if _, err := s.Send(msg); err != nil {
arbiter.ErrorHappened(err)
}
}))
run()
c, connect := Client(t, client.WithServerAddr(s.Addr()))
connect()
defer c.Close(defaultCtx)
// We send the message, in order to invoke the handler.
_, err := c.Send(defaultMsg())
require.NoError(t, err)
// While the handler is sleeping before sending,
// we close the server, so changin its internal
// state.
require.NoError(t, s.Shutdown(defaultCtx))
// The handler should try to send a message,
// getting the expected error.
arbiter.RequireErrorIs(server.ErrNotRunning)
}