|
| 1 | +// Copyright 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +@Timeout(Duration(seconds: 5)) |
| 16 | +library; |
| 17 | + |
| 18 | +import 'dart:async'; |
| 19 | + |
| 20 | +import 'package:flutter_test/flutter_test.dart'; |
| 21 | + |
| 22 | +import 'package:livekit_client/livekit_client.dart'; |
| 23 | +import 'package:livekit_client/src/core/engine.dart'; |
| 24 | +import 'package:livekit_client/src/internal/events.dart'; |
| 25 | +import 'package:livekit_client/src/types/internal.dart'; |
| 26 | +import '../mock/e2e_container.dart'; |
| 27 | +import '../mock/peerconnection_mock.dart'; |
| 28 | + |
| 29 | +void main() { |
| 30 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 31 | + |
| 32 | + late E2EContainer container; |
| 33 | + late List<EngineEvent> events; |
| 34 | + |
| 35 | + setUp(() async { |
| 36 | + resetMockDataChannels(); |
| 37 | + container = E2EContainer(); |
| 38 | + await container.connectRoom(); |
| 39 | + events = []; |
| 40 | + container.engine.events.listen(events.add); |
| 41 | + }); |
| 42 | + |
| 43 | + tearDown(() async { |
| 44 | + await container.dispose(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('disposing the room announces engine closure', () async { |
| 48 | + await container.room.dispose(); |
| 49 | + |
| 50 | + expect(events.whereType<EngineClosingEvent>(), hasLength(1)); |
| 51 | + expect(container.engine.isClosed, isTrue); |
| 52 | + }); |
| 53 | + |
| 54 | + test('room closure is announced while the first transport teardown is blocked', () async { |
| 55 | + final teardownStarted = Completer<void>(); |
| 56 | + final releaseTeardown = Completer<void>(); |
| 57 | + container.engine.publisher!.onDispose(() async { |
| 58 | + teardownStarted.complete(); |
| 59 | + await releaseTeardown.future; |
| 60 | + }); |
| 61 | + |
| 62 | + final disposing = container.room.dispose(); |
| 63 | + await teardownStarted.future; |
| 64 | + final closedDuringTeardown = container.engine.isClosed; |
| 65 | + final closingEventsDuringTeardown = events.whereType<EngineClosingEvent>().length; |
| 66 | + final emitterDisposedDuringTeardown = container.engine.events.isDisposed; |
| 67 | + releaseTeardown.complete(); |
| 68 | + await disposing; |
| 69 | + |
| 70 | + expect(closedDuringTeardown, isTrue); |
| 71 | + expect(closingEventsDuringTeardown, 1); |
| 72 | + expect(emitterDisposedDuringTeardown, isFalse); |
| 73 | + }); |
| 74 | + |
| 75 | + for (final owner in ['room', 'engine']) { |
| 76 | + test('$owner is marked closed before later disposal callbacks run', () async { |
| 77 | + final disposable = owner == 'room' ? container.room : container.engine; |
| 78 | + bool? closedInCallback; |
| 79 | + disposable.onDispose(() async { |
| 80 | + closedInCallback = container.engine.isClosed; |
| 81 | + }); |
| 82 | + |
| 83 | + if (owner == 'room') { |
| 84 | + await container.room.dispose(); |
| 85 | + } else { |
| 86 | + await container.engine.dispose(); |
| 87 | + } |
| 88 | + |
| 89 | + expect(closedInCallback, isTrue); |
| 90 | + expect(events.whereType<EngineClosingEvent>(), hasLength(1)); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + test('engine closure is delivered while disposing with its emitter still alive', () async { |
| 95 | + bool? engineDisposedWhenDelivered; |
| 96 | + bool? emitterDisposedWhenDelivered; |
| 97 | + container.engine.events.once<EngineClosingEvent>((_) { |
| 98 | + engineDisposedWhenDelivered = container.engine.isDisposed; |
| 99 | + emitterDisposedWhenDelivered = container.engine.events.isDisposed; |
| 100 | + }); |
| 101 | + |
| 102 | + await container.engine.dispose(); |
| 103 | + |
| 104 | + expect(engineDisposedWhenDelivered, isTrue); |
| 105 | + expect(emitterDisposedWhenDelivered, isFalse); |
| 106 | + expect(container.engine.events.isDisposed, isTrue); |
| 107 | + }); |
| 108 | + |
| 109 | + test('an unrecoverable reconnect announces closure before disconnection', () async { |
| 110 | + container.wsConnector.connectError = CertificatePinningException( |
| 111 | + 'Certificate pin mismatch', |
| 112 | + host: 'www.example.com', |
| 113 | + ); |
| 114 | + final disconnected = container.engine.events.waitFor<EngineDisconnectedEvent>( |
| 115 | + duration: const Duration(seconds: 1), |
| 116 | + ); |
| 117 | + |
| 118 | + await container.engine.attemptReconnect(ClientDisconnectReason.reconnectRetry); |
| 119 | + expect((await disconnected).reason, DisconnectReason.signalingConnectionFailure); |
| 120 | + |
| 121 | + expect(container.engine.isClosed, isTrue); |
| 122 | + expect(events.whereType<EngineClosingEvent>(), hasLength(1)); |
| 123 | + expect( |
| 124 | + events.indexWhere((event) => event is EngineClosingEvent), |
| 125 | + lessThan(events.indexWhere((event) => event is EngineDisconnectedEvent)), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + test('exhausting reconnect attempts announces engine closure', () async { |
| 130 | + // A generic resume failure stays recoverable without escalating to a full reconnect. |
| 131 | + container.wsConnector.connectError = Exception('temporary connection failure'); |
| 132 | + final disconnected = container.engine.events.waitFor<EngineDisconnectedEvent>( |
| 133 | + duration: const Duration(seconds: 1), |
| 134 | + ); |
| 135 | + |
| 136 | + // Drive the attempts directly rather than waiting through the retry delays. |
| 137 | + for (var i = 0; i < defaultRetryDelaysInMs.length; i++) { |
| 138 | + await container.engine.attemptReconnect(ClientDisconnectReason.reconnectRetry); |
| 139 | + } |
| 140 | + expect((await disconnected).reason, DisconnectReason.reconnectAttemptsExceeded); |
| 141 | + |
| 142 | + expect(container.engine.isClosed, isTrue); |
| 143 | + expect(events.whereType<EngineClosingEvent>(), hasLength(1)); |
| 144 | + expect( |
| 145 | + events.indexWhere((event) => event is EngineClosingEvent), |
| 146 | + lessThan(events.indexWhere((event) => event is EngineDisconnectedEvent)), |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + test('a recoverable reconnect does not announce engine closure', () async { |
| 151 | + container.wsConnector.connectError = Exception('temporary connection failure'); |
| 152 | + |
| 153 | + await container.engine.attemptReconnect(ClientDisconnectReason.reconnectRetry); |
| 154 | + |
| 155 | + expect(container.engine.isClosed, isFalse); |
| 156 | + expect(events.whereType<EngineClosingEvent>(), isEmpty); |
| 157 | + expect(events.whereType<EngineDisconnectedEvent>(), isEmpty); |
| 158 | + }); |
| 159 | + |
| 160 | + test('a buffer wait started after disposal rejects even when the channel reports low', () async { |
| 161 | + await container.room.dispose(); |
| 162 | + |
| 163 | + await expectLater( |
| 164 | + container.engine.waitForBufferStatusLow(Reliability.reliable), |
| 165 | + throwsA(isA<UnexpectedConnectionState>()), |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + test('disconnect followed by disposal announces closure only once', () async { |
| 170 | + await container.engine.disconnect(); |
| 171 | + await expectLater( |
| 172 | + container.engine.waitForBufferStatusLow(Reliability.reliable), |
| 173 | + throwsA(isA<UnexpectedConnectionState>()), |
| 174 | + ); |
| 175 | + await container.room.dispose(); |
| 176 | + |
| 177 | + expect(events.whereType<EngineClosingEvent>(), hasLength(1)); |
| 178 | + }); |
| 179 | +} |
0 commit comments