|
| 1 | +/// Unit tests for [SignalingModuleImpl]. |
| 2 | +/// |
| 3 | +/// Reproduces the disconnect-during-connect race condition: |
| 4 | +/// |
| 5 | +/// When [SignalingModuleImpl.disconnect] is called while [_connectAsync] is |
| 6 | +/// still awaiting the client factory (_client == null, _connectToken != null), |
| 7 | +/// disconnect() must cancel the in-flight attempt by clearing _connectToken. |
| 8 | +/// The next connect() call must then be allowed to start a fresh attempt |
| 9 | +/// instead of being blocked by the stale in-flight operation. |
| 10 | +/// |
| 11 | +/// Tests labelled "BUG:" reproduce the broken behavior on the unfixed code. |
| 12 | +/// Tests labelled "after fix:" verify the correct behavior. |
| 13 | +library; |
| 14 | + |
| 15 | +import 'dart:async'; |
| 16 | + |
| 17 | +import 'package:flutter_test/flutter_test.dart'; |
| 18 | +import 'package:ssl_certificates/ssl_certificates.dart'; |
| 19 | +import 'package:webtrit_signaling/webtrit_signaling.dart'; |
| 20 | +import 'package:webtrit_signaling_service_platform_interface/webtrit_signaling_service_platform_interface.dart'; |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Fake client |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | + |
| 26 | +class _FakeClient extends Fake implements WebtritSignalingClient { |
| 27 | + DisconnectHandler? _onDisconnect; |
| 28 | + |
| 29 | + @override |
| 30 | + void listen({ |
| 31 | + required StateHandshakeHandler onStateHandshake, |
| 32 | + required EventHandler onEvent, |
| 33 | + required ErrorHandler onError, |
| 34 | + required DisconnectHandler onDisconnect, |
| 35 | + }) { |
| 36 | + _onDisconnect = onDisconnect; |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + Future<void> disconnect([int? code, String? reason]) async { |
| 41 | + _onDisconnect?.call(code, reason); |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Future<void> execute(Request request, [Duration? timeout]) async {} |
| 46 | +} |
| 47 | + |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | +// Controlled factory |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | + |
| 52 | +/// A factory where each call gets its own [Completer]. |
| 53 | +/// Tracks how many times the factory was called and allows releasing each |
| 54 | +/// call independently. |
| 55 | +class _ControlledFactory { |
| 56 | + final _calls = <Completer<WebtritSignalingClient>>[]; |
| 57 | + int get callCount => _calls.length; |
| 58 | + |
| 59 | + Completer<WebtritSignalingClient> get lastCall => _calls.last; |
| 60 | + |
| 61 | + SignalingClientFactory get factory => |
| 62 | + ({ |
| 63 | + required Uri url, |
| 64 | + required String tenantId, |
| 65 | + required String token, |
| 66 | + required Duration connectionTimeout, |
| 67 | + required TrustedCertificates certs, |
| 68 | + required bool force, |
| 69 | + }) { |
| 70 | + final c = Completer<WebtritSignalingClient>(); |
| 71 | + _calls.add(c); |
| 72 | + return c.future; |
| 73 | + }; |
| 74 | + |
| 75 | + void release(int callIndex, WebtritSignalingClient client) => _calls[callIndex].complete(client); |
| 76 | +} |
| 77 | + |
| 78 | +// --------------------------------------------------------------------------- |
| 79 | +// Helper |
| 80 | +// --------------------------------------------------------------------------- |
| 81 | + |
| 82 | +SignalingModuleImpl _buildModule(SignalingClientFactory factory) => SignalingModuleImpl( |
| 83 | + coreUrl: 'https://example.com', |
| 84 | + tenantId: 'tenant', |
| 85 | + token: 'token', |
| 86 | + trustedCertificates: TrustedCertificates.empty, |
| 87 | + clientFactory: factory, |
| 88 | +); |
| 89 | + |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | +// Tests |
| 92 | +// --------------------------------------------------------------------------- |
| 93 | + |
| 94 | +void main() { |
| 95 | + group('SignalingModuleImpl — disconnect() during in-flight connect()', () { |
| 96 | + // ----------------------------------------------------------------------- |
| 97 | + // Core bug: disconnect while _client==null leaves _connecting=true |
| 98 | + // ----------------------------------------------------------------------- |
| 99 | + |
| 100 | + test('BUG: second connect() is silently dropped because _connecting stays true after disconnect()', () async { |
| 101 | + // Arrange |
| 102 | + final factory = _ControlledFactory(); |
| 103 | + final module = _buildModule(factory.factory); |
| 104 | + |
| 105 | + // Act: connect #1 → factory called, hangs |
| 106 | + module.connect(); |
| 107 | + await Future<void>.delayed(Duration.zero); |
| 108 | + expect(factory.callCount, 1, reason: 'first connect() must call the factory'); |
| 109 | + |
| 110 | + // disconnect() while _client==null — the bug: does not reset _connecting |
| 111 | + await module.disconnect(); |
| 112 | + |
| 113 | + // connect #2 — must call the factory again; currently it is silently dropped |
| 114 | + module.connect(); |
| 115 | + await Future<void>.delayed(Duration.zero); |
| 116 | + |
| 117 | + // BUG: factory was not called a second time — second connect() was dropped |
| 118 | + expect( |
| 119 | + factory.callCount, |
| 120 | + 2, |
| 121 | + reason: 'second connect() was silently dropped: factory call count did not increase', |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + // ----------------------------------------------------------------------- |
| 126 | + // Consequence: stale connect completes and leaves module in wrong state |
| 127 | + // ----------------------------------------------------------------------- |
| 128 | + |
| 129 | + test('BUG: stale in-flight connect completes after disconnect() and leaves module connected', () async { |
| 130 | + // Arrange |
| 131 | + final factory = _ControlledFactory(); |
| 132 | + final module = _buildModule(factory.factory); |
| 133 | + final events = <SignalingModuleEvent>[]; |
| 134 | + module.events.listen(events.add); |
| 135 | + |
| 136 | + // connect #1 → factory hangs |
| 137 | + module.connect(); |
| 138 | + await Future<void>.delayed(Duration.zero); |
| 139 | + |
| 140 | + // disconnect() — no-op when _client==null (the bug) |
| 141 | + await module.disconnect(); |
| 142 | + |
| 143 | + // Release the stale factory (simulates slow WebSocket that eventually connects) |
| 144 | + factory.release(0, _FakeClient()); |
| 145 | + await Future<void>.delayed(Duration.zero); |
| 146 | + |
| 147 | + // BUG: module is now "connected" even though we explicitly disconnected |
| 148 | + expect( |
| 149 | + module.isConnected, |
| 150 | + isFalse, |
| 151 | + reason: 'stale connect() completed after disconnect() and incorrectly set _client', |
| 152 | + ); |
| 153 | + expect( |
| 154 | + events.whereType<SignalingConnected>(), |
| 155 | + isEmpty, |
| 156 | + reason: 'SignalingConnected must not fire for a connect() superseded by disconnect()', |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + // ----------------------------------------------------------------------- |
| 161 | + // Expected correct behavior after the fix |
| 162 | + // ----------------------------------------------------------------------- |
| 163 | + |
| 164 | + test('after fix: disconnect() cancels in-flight connect so second connect() reaches the factory', () async { |
| 165 | + final factory = _ControlledFactory(); |
| 166 | + final module = _buildModule(factory.factory); |
| 167 | + final events = <SignalingModuleEvent>[]; |
| 168 | + module.events.listen(events.add); |
| 169 | + |
| 170 | + // connect #1 → hangs |
| 171 | + module.connect(); |
| 172 | + await Future<void>.delayed(Duration.zero); |
| 173 | + |
| 174 | + // disconnect() → must cancel connect #1 and reset _connecting |
| 175 | + await module.disconnect(); |
| 176 | + |
| 177 | + // connect #2 → must be accepted (factory called again) |
| 178 | + module.connect(); |
| 179 | + await Future<void>.delayed(Duration.zero); |
| 180 | + expect(factory.callCount, 2, reason: 'second connect() must call the factory'); |
| 181 | + |
| 182 | + // Release connect #2 — module should become connected |
| 183 | + factory.release(1, _FakeClient()); |
| 184 | + await Future<void>.delayed(Duration.zero); |
| 185 | + |
| 186 | + expect(module.isConnected, isTrue); |
| 187 | + expect(events.whereType<SignalingConnected>(), isNotEmpty); |
| 188 | + }); |
| 189 | + |
| 190 | + test('after fix: stale factory result is discarded, SignalingConnected not emitted', () async { |
| 191 | + final factory = _ControlledFactory(); |
| 192 | + final module = _buildModule(factory.factory); |
| 193 | + final events = <SignalingModuleEvent>[]; |
| 194 | + module.events.listen(events.add); |
| 195 | + |
| 196 | + // connect #1 → hangs |
| 197 | + module.connect(); |
| 198 | + await Future<void>.delayed(Duration.zero); |
| 199 | + |
| 200 | + // disconnect() cancels connect #1 |
| 201 | + await module.disconnect(); |
| 202 | + |
| 203 | + // Release the stale factory call AFTER disconnect |
| 204 | + factory.release(0, _FakeClient()); |
| 205 | + await Future<void>.delayed(Duration.zero); |
| 206 | + |
| 207 | + // Stale result must be discarded |
| 208 | + expect(module.isConnected, isFalse); |
| 209 | + expect(events.whereType<SignalingConnected>(), isEmpty); |
| 210 | + }); |
| 211 | + }); |
| 212 | +} |
0 commit comments