Skip to content

Commit 658661b

Browse files
authored
Merge pull request #3 from superlistapp/do-not-throw-when-pushing-to-errored-or-closed-channel
Do not throw when pushing to errored or closed channel
2 parents cd85500 + b8acc73 commit 658661b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/src/channel.dart

+1-5
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,9 @@ class PhoenixChannel {
281281
_logger.finest(() => 'Sending out push ${pushEvent.ref}');
282282
pushEvent.send();
283283
} else {
284-
if (_state == PhoenixChannelState.closed ||
285-
_state == PhoenixChannelState.errored) {
286-
throw ChannelClosedError('Can\'t push event on a $_state channel');
287-
}
288-
289284
_logger.finest(
290285
() => 'Buffering push ${pushEvent.ref} for later send ($_state)');
286+
pushEvent.startTimeout();
291287
pushBuffer.add(pushEvent);
292288
}
293289

test/channel_test.dart

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:mockito/mockito.dart';
2+
import 'package:phoenix_socket/phoenix_socket.dart';
3+
import 'package:test/test.dart';
4+
5+
import 'mocks.dart';
6+
7+
void main() {
8+
group('PhoenixChannel unit tests', () {
9+
test(
10+
'pushing an event into a channel with state PhoenixChannelState.closed does not throw',
11+
() {
12+
final mockSocket = MockPhoenixSocket();
13+
when(mockSocket.defaultTimeout).thenReturn(Duration.zero);
14+
when(mockSocket.isConnected).thenReturn(true);
15+
16+
final channel = PhoenixChannel.fromSocket(mockSocket, topic: 'test');
17+
channel.join();
18+
channel.close();
19+
20+
expect(channel.state, PhoenixChannelState.closed);
21+
expect(() => channel.push('test-event', {}), returnsNormally);
22+
});
23+
24+
test(
25+
'pushing an event into a channel with state PhoenixChannelState.errored does not throw',
26+
() {
27+
final mockSocket = MockPhoenixSocket();
28+
when(mockSocket.defaultTimeout).thenReturn(Duration.zero);
29+
when(mockSocket.isConnected).thenReturn(false);
30+
31+
final channel = PhoenixChannel.fromSocket(mockSocket, topic: 'test');
32+
channel.join();
33+
34+
expect(channel.state, PhoenixChannelState.errored);
35+
expect(() => channel.push('test-event', {}), returnsNormally);
36+
});
37+
});
38+
}

0 commit comments

Comments
 (0)