|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
| 4 | +import 'package:fake_async/fake_async.dart'; |
| 5 | +import 'package:mocktail/mocktail.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | +import 'package:web_socket_channel/web_socket_channel.dart'; |
| 8 | + |
| 9 | +import 'package:webtrit_signaling/webtrit_signaling.dart'; |
| 10 | + |
| 11 | +class MockWebSocketChannel extends Mock implements WebSocketChannel {} |
| 12 | + |
| 13 | +class MockWebSocketSink extends Mock implements WebSocketSink {} |
| 14 | + |
| 15 | +void main() { |
| 16 | + group('WebtritSignalingClient Keepalive Race Condition', () { |
| 17 | + late MockWebSocketChannel mockChannel; |
| 18 | + late MockWebSocketSink mockSink; |
| 19 | + late StreamController<dynamic> streamController; |
| 20 | + late WebtritSignalingClient client; |
| 21 | + |
| 22 | + setUp(() { |
| 23 | + mockChannel = MockWebSocketChannel(); |
| 24 | + mockSink = MockWebSocketSink(); |
| 25 | + streamController = StreamController<dynamic>(); |
| 26 | + |
| 27 | + when(() => mockChannel.sink).thenReturn(mockSink); |
| 28 | + when(() => mockChannel.stream).thenAnswer((_) => streamController.stream); |
| 29 | + when(() => mockChannel.closeCode).thenReturn(null); |
| 30 | + when(() => mockSink.close(any(), any())).thenAnswer((_) => Future.value()); |
| 31 | + }); |
| 32 | + |
| 33 | + tearDown(() { |
| 34 | + streamController.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('should gracefully terminate keepalive loop without error when timer fires on a closed socket', () { |
| 38 | + fakeAsync((async) { |
| 39 | + client = WebtritSignalingClient.inner(mockChannel); |
| 40 | + |
| 41 | + var errorReported = false; |
| 42 | + Object? reportedError; |
| 43 | + |
| 44 | + client.listen( |
| 45 | + onStateHandshake: (_) {}, |
| 46 | + onEvent: (_) {}, |
| 47 | + onError: (e, s) { |
| 48 | + errorReported = true; |
| 49 | + reportedError = e; |
| 50 | + }, |
| 51 | + onDisconnect: (_, _) {}, |
| 52 | + ); |
| 53 | + |
| 54 | + var isPhysicalSocketClosed = false; |
| 55 | + |
| 56 | + // Throws StateError to simulate writing to a closed sink. |
| 57 | + when(() => mockSink.add(any())).thenAnswer((invocation) { |
| 58 | + if (isPhysicalSocketClosed) { |
| 59 | + throw StateError('Bad state: Cannot add event after closing.'); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + // Initiates the Keepalive timer (100ms interval). |
| 64 | + final handshakeData = jsonEncode({ |
| 65 | + 'handshake': 'state', |
| 66 | + 'timestamp': 1705322000000, |
| 67 | + 'keepalive_interval': 100, |
| 68 | + 'registration': {'status': 'registered'}, |
| 69 | + 'lines': [], |
| 70 | + 'user_active_calls': [], |
| 71 | + 'presence_contacts_info': {}, |
| 72 | + 'guest_line': null, |
| 73 | + }); |
| 74 | + |
| 75 | + streamController.add(handshakeData); |
| 76 | + async.flushMicrotasks(); |
| 77 | + |
| 78 | + // Simulates a physical connection drop without explicit client disconnection. |
| 79 | + // The client remains unaware of the transport failure, allowing the timer to persist. |
| 80 | + isPhysicalSocketClosed = true; |
| 81 | + |
| 82 | + // Advances time to trigger the Keepalive timer (100ms + buffer). |
| 83 | + async.elapse(const Duration(milliseconds: 200)); |
| 84 | + |
| 85 | + // Confirms the write was attempted (validating the race condition logic executed). |
| 86 | + verify(() => mockSink.add(any())).called(greaterThan(0)); |
| 87 | + |
| 88 | + // Ensures the internal exception was handled silently and not reported. |
| 89 | + expect(errorReported, isFalse, reason: 'Race condition caused an exception to leak to onError: $reportedError'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
0 commit comments