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

[part of DEV-11757] Makes failed heartbeat not attempt to reconnect after ConnectionManager is closed or disposed #10

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions lib/src/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@ class PhoenixSocket {
return false;
} catch (error, stackTrace) {
_logger.warning('Heartbeat message failed', error, stackTrace);
_reconnect(heartbeatTimedOut, reason: 'Heartbeat timed out');
if (!_disposed && _connectionManager != null) {
_reconnect(heartbeatTimedOut, reason: 'Heartbeat timed out');
}
return false;
}
}
Expand Down Expand Up @@ -569,7 +571,6 @@ class PhoenixSocket {
if (_isOpen) {
_triggerChannelExceptions(PhoenixException(socketError: errorEvent));
}
// _connectionManager?.reconnect(internalServerError, reason: 'Error on socket: ${errorEvent.error}');
}

void _onSocketClosed(PhoenixSocketCloseEvent closeEvent) {
Expand Down
31 changes: 31 additions & 0 deletions test/socket_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import 'dart:async';
import 'package:phoenix_socket/phoenix_socket.dart';
import 'package:test/test.dart';

import 'helpers/proxy.dart';

void main() {
const addr = 'ws://localhost:4001/socket/websocket';

group('PhoenixSocket', () {
setUp(() async {
await prepareProxy();
});

tearDown(() async {
await destroyProxy();
});

test('can connect to a running Phoenix server', () async {
final socket = PhoenixSocket(addr);

Expand Down Expand Up @@ -121,5 +131,26 @@ void main() {

expect(errCount, 3);
});

test('heartbeat failure does not reconnect after disposal', () async {
final socket = PhoenixSocket(
addr,
socketOptions: PhoenixSocketOptions(
heartbeat: Duration(milliseconds: 1),
),
);

await socket.connect();

await socket.openStream.first;

// Prevent next heartbeat from getting a reply.
await haltProxy();

socket.dispose();

await Future.delayed(Duration(milliseconds: 1));
// The test will fail with unhandled exception if reconnection attempt is made
});
});
}
Loading