diff --git a/mobile/lib/shared/relay/relay_socket.dart b/mobile/lib/shared/relay/relay_socket.dart index ca619a2862..3667681c91 100644 --- a/mobile/lib/shared/relay/relay_socket.dart +++ b/mobile/lib/shared/relay/relay_socket.dart @@ -16,6 +16,10 @@ import 'nostr_models.dart'; /// Does NOT handle reconnection — that is [RelaySessionNotifier]'s job. enum SocketState { disconnected, connecting, authenticating, connected } +class _RelayDisconnectCancelled implements Exception { + const _RelayDisconnectCancelled(); +} + class RelayAuthRejectedException implements Exception { final String message; @@ -129,7 +133,7 @@ class RelaySocket { } catch (e) { _authTimeout?.cancel(); await disconnect(); - _onDisconnected(e); + if (e is! _RelayDisconnectCancelled) _onDisconnected(e); } } @@ -140,7 +144,7 @@ class RelaySocket { /// Gracefully close the connection. Future disconnect() async { - _failAuth(Exception('Relay disconnected during authentication')); + _failAuth(const _RelayDisconnectCancelled()); _resetConnection(); final channel = _channel; _channel = null; diff --git a/mobile/test/shared/relay/relay_socket_liveness_test.dart b/mobile/test/shared/relay/relay_socket_liveness_test.dart index 51a80673c0..81183b7c59 100644 --- a/mobile/test/shared/relay/relay_socket_liveness_test.dart +++ b/mobile/test/shared/relay/relay_socket_liveness_test.dart @@ -56,6 +56,7 @@ void main() { RelaySocket.debugPingInterval = RelaySocket.pingInterval; final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); final authReceived = Completer(); + final disconnectErrors = []; WebSocket? peer; server.listen((request) async { final connection = await WebSocketTransformer.upgrade(request); @@ -73,7 +74,7 @@ void main() { nsec: nostr.Keys.generate().nsec, onMessage: (_) {}, onConnected: () => fail('The peer never acknowledges authentication'), - onDisconnected: (_) {}, + onDisconnected: disconnectErrors.add, ); final connecting = socket.connect(); try { @@ -82,6 +83,7 @@ void main() { await socket.disconnect(); await connecting.timeout(const Duration(seconds: 1)); expect(socket.state, SocketState.disconnected); + expect(disconnectErrors, isEmpty); } finally { socket.dispose(); await peer?.close();