Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw when pushing to errored or closed channel #3

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/src/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,9 @@ class PhoenixChannel {
_logger.finest(() => 'Sending out push ${pushEvent.ref}');
pushEvent.send();
} else {
if (_state == PhoenixChannelState.closed ||
_state == PhoenixChannelState.errored) {
throw ChannelClosedError('Can\'t push event on a $_state channel');
}

_logger.finest(
() => 'Buffering push ${pushEvent.ref} for later send ($_state)');
pushEvent.startTimeout();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wish there was an easy way to test this

pushBuffer.add(pushEvent);
}

Expand Down
38 changes: 38 additions & 0 deletions test/channel_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:mockito/mockito.dart';
import 'package:phoenix_socket/phoenix_socket.dart';
import 'package:test/test.dart';

import 'mocks.dart';

void main() {
group('PhoenixChannel unit tests', () {
test(
'pushing an event into a channel with state PhoenixChannelState.closed does not throw',
() {
final mockSocket = MockPhoenixSocket();
when(mockSocket.defaultTimeout).thenReturn(Duration.zero);
when(mockSocket.isConnected).thenReturn(true);

final channel = PhoenixChannel.fromSocket(mockSocket, topic: 'test');
channel.join();
channel.close();

expect(channel.state, PhoenixChannelState.closed);
expect(() => channel.push('test-event', {}), returnsNormally);
});

test(
'pushing an event into a channel with state PhoenixChannelState.errored does not throw',
() {
final mockSocket = MockPhoenixSocket();
when(mockSocket.defaultTimeout).thenReturn(Duration.zero);
when(mockSocket.isConnected).thenReturn(false);

final channel = PhoenixChannel.fromSocket(mockSocket, topic: 'test');
channel.join();

expect(channel.state, PhoenixChannelState.errored);
expect(() => channel.push('test-event', {}), returnsNormally);
});
});
}
Loading