@@ -3,14 +3,25 @@ import 'dart:async';
3
3
import 'package:phoenix_socket/phoenix_socket.dart' ;
4
4
import 'package:test/test.dart' ;
5
5
6
- import 'control.dart' ;
6
+ import 'helpers/logging.dart' ;
7
+ import 'helpers/proxy.dart' ;
7
8
8
- void main () {
9
- const addr = 'ws://localhost:4001/socket/websocket' ;
9
+ Set <int > usedPorts = {};
10
10
11
+ void main () {
11
12
group ('PhoenixChannel' , () {
13
+ const addr = 'ws://localhost:4004/socket/websocket' ;
14
+
15
+ setUpAll (() {
16
+ maybeActivateAllLogLevels ();
17
+ });
18
+
12
19
setUp (() async {
13
- await restartBackend ();
20
+ await prepareProxy ();
21
+ });
22
+
23
+ tearDown (() async {
24
+ await destroyProxy ();
14
25
});
15
26
16
27
test ('can join a channel through a socket' , () async {
@@ -28,10 +39,11 @@ void main() {
28
39
29
40
test ('can join a channel through a socket that starts closed then connects' ,
30
41
() async {
42
+ await haltThenResumeProxy ();
43
+
31
44
final socket = PhoenixSocket (addr);
32
45
final completer = Completer <void >();
33
46
34
- await stopThenRestartBackend ();
35
47
await socket.connect ();
36
48
37
49
socket.addChannel (topic: 'channel1' ).join ().onReply ('ok' , (reply) {
@@ -50,10 +62,10 @@ void main() {
50
62
51
63
await socket.connect ();
52
64
53
- await stopBackend ();
65
+ await haltProxy ();
54
66
final joinFuture = socket.addChannel (topic: 'channel1' ).join ();
55
67
Future .delayed (const Duration (milliseconds: 300 ))
56
- .then ((value) => restartBackend ());
68
+ .then ((value) => resumeProxy ());
57
69
58
70
joinFuture.onReply ('ok' , (reply) {
59
71
expect (reply.status, equals ('ok' ));
@@ -63,6 +75,32 @@ void main() {
63
75
await completer.future;
64
76
});
65
77
78
+ test (
79
+ 'can join a channel through a socket that gets a "peer reset" before join but reconnects' ,
80
+ () async {
81
+ final socket = PhoenixSocket (addr);
82
+ final completer = Completer <void >();
83
+
84
+ await socket.connect ();
85
+ addTearDown (() {
86
+ socket.close ();
87
+ });
88
+ await resetPeer ();
89
+
90
+ runZonedGuarded (() {
91
+ final joinFuture = socket.addChannel (topic: 'channel1' ).join ();
92
+ joinFuture.onReply ('ok' , (reply) {
93
+ expect (reply.status, equals ('ok' ));
94
+ completer.complete ();
95
+ });
96
+ }, (error, stack) {});
97
+
98
+ Future .delayed (const Duration (milliseconds: 1000 ))
99
+ .then ((value) => resetPeer (enable: false ));
100
+
101
+ await completer.future;
102
+ });
103
+
66
104
test ('can join a channel through an unawaited socket' , () async {
67
105
final socket = PhoenixSocket (addr);
68
106
final completer = Completer <void >();
@@ -137,22 +175,100 @@ void main() {
137
175
});
138
176
139
177
test (
140
- 'can send messages to channels that got transiently disconnected and receive a reply' ,
141
- () async {
178
+ 'can send messages to channels that got transiently '
179
+ 'disconnected and receive a reply' , () async {
142
180
final socket = PhoenixSocket (addr);
143
181
144
182
await socket.connect ();
145
183
146
184
final channel1 = socket.addChannel (topic: 'channel1' );
147
185
await channel1.join ().future;
148
186
149
- await stopThenRestartBackend ();
187
+ await haltThenResumeProxy ();
188
+ await socket.openStream.first;
150
189
151
190
final reply = await channel1.push ('hello!' , {'foo' : 'bar' }).future;
152
191
expect (reply.status, equals ('ok' ));
153
192
expect (reply.response, equals ({'name' : 'bar' }));
154
193
});
155
194
195
+ test (
196
+ 'can send messages to channels that got "peer reset" '
197
+ 'and receive a reply' , () async {
198
+ final socket = PhoenixSocket (addr);
199
+
200
+ await socket.connect ();
201
+
202
+ final channel1 = socket.addChannel (topic: 'channel1' );
203
+ await channel1.join ().future;
204
+
205
+ await resetPeerThenResumeProxy ();
206
+
207
+ final push = channel1.push ('hello!' , {'foo' : 'bar' });
208
+ final reply = await push.future;
209
+
210
+ expect (reply.status, equals ('ok' ));
211
+ expect (reply.response, equals ({'name' : 'bar' }));
212
+ });
213
+
214
+ test (
215
+ 'throws when sending messages to channels that got "peer reset" '
216
+ 'and that have not recovered yet' , () async {
217
+ final socket = PhoenixSocket (addr);
218
+
219
+ await socket.connect ();
220
+
221
+ final channel1 = socket.addChannel (topic: 'channel1' );
222
+ await channel1.join ().future;
223
+
224
+ await resetPeer ();
225
+
226
+ final Completer <Object > errorCompleter = Completer ();
227
+
228
+ runZonedGuarded (() async {
229
+ final push = channel1.push ('hello!' , {'foo' : 'bar' });
230
+ try {
231
+ await push.future;
232
+ } catch (err) {
233
+ errorCompleter.complete (err);
234
+ }
235
+ }, (error, stack) {});
236
+
237
+ final Object exception;
238
+ expect (exception = await errorCompleter.future, isA <PhoenixException >());
239
+ expect ((exception as PhoenixException ).socketClosed, isNotNull);
240
+ });
241
+
242
+ test (
243
+ 'throws when sending messages to channels that got disconnected '
244
+ 'and that have not recovered yet' ,
245
+ () async {
246
+ final socket = PhoenixSocket (addr);
247
+
248
+ await socket.connect ();
249
+
250
+ final channel1 = socket.addChannel (topic: 'channel1' );
251
+ await channel1.join ().future;
252
+
253
+ await haltProxy ();
254
+
255
+ final Completer <Object > errorCompleter = Completer ();
256
+ runZonedGuarded (() async {
257
+ try {
258
+ final push = channel1.push ('hello!' , {'foo' : 'bar' });
259
+ await push.future;
260
+ } catch (err) {
261
+ errorCompleter.complete (err);
262
+ }
263
+ }, (error, stack) {});
264
+
265
+ expect (await errorCompleter.future, isA <ChannelClosedError >());
266
+ },
267
+ timeout: Timeout (
268
+ Duration (seconds: 5 ),
269
+ ),
270
+ );
271
+
156
272
test ('only emits reply messages that are channel replies' , () async {
157
273
final socket = PhoenixSocket (addr);
158
274
@@ -195,6 +311,11 @@ void main() {
195
311
final channel2 = socket2.addChannel (topic: 'channel3' );
196
312
await channel2.join ().future;
197
313
314
+ addTearDown (() {
315
+ socket1.close ();
316
+ socket2.close ();
317
+ });
318
+
198
319
expect (
199
320
channel1.messages,
200
321
emitsInOrder ([
@@ -249,6 +370,11 @@ void main() {
249
370
final channel2 = socket2.addChannel (topic: 'channel3' );
250
371
await channel2.join ().future;
251
372
373
+ addTearDown (() {
374
+ socket1.close ();
375
+ socket2.close ();
376
+ });
377
+
252
378
channel1.push ('ping' , {'from' : 'socket1' });
253
379
254
380
expect (
@@ -278,6 +404,11 @@ void main() {
278
404
final channel2 = socket2.addChannel (topic: 'channel3' );
279
405
await channel2.join ().future;
280
406
407
+ addTearDown (() {
408
+ socket1.close ();
409
+ socket2.close ();
410
+ });
411
+
281
412
channel1.push ('ping' , {'from' : 'socket1' });
282
413
283
414
expect (
@@ -315,8 +446,8 @@ void main() {
315
446
final socket = PhoenixSocket (addr);
316
447
await socket.connect ();
317
448
final channel = socket.addChannel (topic: 'channel3' );
318
- await channel.join ().future;
319
449
450
+ await channel.join ().future;
320
451
await channel.leave ().future;
321
452
322
453
expect (
0 commit comments