From 763cf0bfc6d13a3ef5f86a3984931241f08f15cd Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Mon, 18 Dec 2023 21:05:10 +0800 Subject: [PATCH] fix reconnect test. --- lib/src/core/signal_client.dart | 1 + lib/src/support/websocket_utility.dart | 44 ++++++++++++++++---------- test/core/signal_client_test.dart | 15 +++++---- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/src/core/signal_client.dart b/lib/src/core/signal_client.dart index f8ae2349..dc28c0cc 100644 --- a/lib/src/core/signal_client.dart +++ b/lib/src/core/signal_client.dart @@ -43,6 +43,7 @@ class SignalClient extends Disposable with EventsEmittable { ConnectionState get connectionState => _connectionState; final WebSocketUtility _ws; + WebSocketUtility get ws => _ws; final _queue = Queue(); Duration? _pingTimeoutDuration; diff --git a/lib/src/support/websocket_utility.dart b/lib/src/support/websocket_utility.dart index 4a496953..5a3fe3e9 100644 --- a/lib/src/support/websocket_utility.dart +++ b/lib/src/support/websocket_utility.dart @@ -17,6 +17,7 @@ import 'dart:async'; import 'package:connectivity_plus/connectivity_plus.dart'; import '../logger.dart'; +import 'platform.dart'; import 'websocket.dart'; enum SocketStatus { @@ -75,23 +76,25 @@ class WebSocketUtility { _socketStatus = SocketStatus.kSocketStatusNone; onSocketStatusChange?.call(_socketStatus); - if (_connectivitySubscription != null) { - await _connectivitySubscription?.cancel(); - _connectivitySubscription = null; - } + if (!lkPlatformIsTest()) { + if (_connectivitySubscription != null) { + await _connectivitySubscription?.cancel(); + _connectivitySubscription = null; + } - _connectivity = await Connectivity().checkConnectivity(); - _connectivitySubscription = - Connectivity().onConnectivityChanged.listen((result) { - logger.fine('network changed ${result.name}, reconnecting...'); - if ((result != ConnectivityResult.none && _connectivity != result) && - !_isClosed) { - if (_socketStatus != SocketStatus.kSocketStatusNone) { - reconnect(); + _connectivity = await Connectivity().checkConnectivity(); + _connectivitySubscription = + Connectivity().onConnectivityChanged.listen((result) { + logger.fine('network changed ${result.name}, reconnecting...'); + if ((result != ConnectivityResult.none && _connectivity != result) && + !_isClosed) { + if (_socketStatus != SocketStatus.kSocketStatusNone) { + reconnect(); + } } - } - _connectivity = result; - }); + _connectivity = result; + }); + } } void setReconnSid(String sid) { @@ -182,8 +185,10 @@ class WebSocketUtility { } _reconnectTimes = 0; _isClosed = true; - _connectivitySubscription?.cancel(); - _connectivitySubscription = null; + if (!lkPlatformIsTest()) { + _connectivitySubscription?.cancel(); + _connectivitySubscription = null; + } _cleanUp(); } @@ -196,10 +201,15 @@ class WebSocketUtility { } Future reconnect() async { + if (lkPlatformIsTest()) { + _reconnectUrl = Uri.tryParse('ws://example.com'); + } + if (_reconnectUrl == null) { logger.warning('WebSocket reconnect failed, no reconnect url'); return false; } + if (_reconnectTimes < _reconnectCount) { if (_socketStatus != SocketStatus.kSocketStatusReconnecting) { _changeSocketStatus(SocketStatus.kSocketStatusReconnecting); diff --git a/test/core/signal_client_test.dart b/test/core/signal_client_test.dart index c37ef5b7..de417da9 100644 --- a/test/core/signal_client_test.dart +++ b/test/core/signal_client_test.dart @@ -54,21 +54,24 @@ void main() { ); }); test('reconnect', () async { + await client.connect( + exampleUri, + token, + connectOptions: connectOptions, + roomOptions: roomOptions, + ); expect( client.events.streamCtrl.stream, emitsInOrder([ + predicate( + (event) => event.newState == ConnectionState.disconnected), predicate( (event) => event.newState == ConnectionState.reconnecting), predicate((event) => event.newState == ConnectionState.connected && event.didReconnect == true), ])); - await client.connect( - exampleUri, - token, - connectOptions: connectOptions, - roomOptions: roomOptions, - ); + await client.closeSocket(); }); });