|
| 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 'package:flutter_test/flutter_test.dart'; |
| 19 | + |
| 20 | +import 'package:livekit_client/livekit_client.dart'; |
| 21 | +import 'package:livekit_client/src/proto/livekit_models.pb.dart' as lk_models; |
| 22 | +import 'package:livekit_client/src/proto/livekit_rtc.pb.dart' as lk_rtc; |
| 23 | +import '../mock/e2e_container.dart'; |
| 24 | +import '../mock/test_data.dart'; |
| 25 | +import 'signal_client_test.dart'; |
| 26 | + |
| 27 | +/// Room sids are assigned asynchronously by the server: the JoinResponse can |
| 28 | +/// carry an empty `room.sid`, with the real sid following in a RoomUpdate. |
| 29 | +/// `getSid()` must resolve when that update arrives — it used to wait for |
| 30 | +/// `SignalRoomUpdateEvent` on the Room's own emitter, where that (signal) |
| 31 | +/// event is never emitted, so the returned future never completed. |
| 32 | +void main() { |
| 33 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 34 | + |
| 35 | + final lk_rtc.SignalResponse emptySidJoinResponse = lk_rtc.SignalResponse( |
| 36 | + join: lk_rtc.JoinResponse( |
| 37 | + room: lk_models.Room( |
| 38 | + name: 'room_name', |
| 39 | + // sid deliberately unset — issued later via RoomUpdate. |
| 40 | + ), |
| 41 | + participant: localParticipantData, |
| 42 | + subscriberPrimary: true, |
| 43 | + serverVersion: '99.999', |
| 44 | + serverInfo: lk_models.ServerInfo( |
| 45 | + version: '1.8.0', |
| 46 | + ), |
| 47 | + ), |
| 48 | + ); |
| 49 | + |
| 50 | + final lk_rtc.SignalResponse sidRoomUpdateResponse = lk_rtc.SignalResponse( |
| 51 | + roomUpdate: lk_rtc.RoomUpdate( |
| 52 | + room: lk_models.Room( |
| 53 | + name: 'room_name', |
| 54 | + sid: 'RM_issued_later', |
| 55 | + ), |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + /// Connects the container's room, answering with [joinResp] instead of the |
| 60 | + /// default join response. |
| 61 | + Future<void> connectWith(E2EContainer container, lk_rtc.SignalResponse joinResp) async { |
| 62 | + final connectFuture = container.room.connect(exampleUri, token); |
| 63 | + Future.delayed(const Duration(milliseconds: 1), () { |
| 64 | + container.wsConnector.onData(joinResp.writeToBuffer()); |
| 65 | + container.wsConnector.onData(offerResponse.writeToBuffer()); |
| 66 | + }); |
| 67 | + await connectFuture; |
| 68 | + } |
| 69 | + |
| 70 | + late E2EContainer container; |
| 71 | + |
| 72 | + setUp(() async { |
| 73 | + container = E2EContainer(); |
| 74 | + }); |
| 75 | + |
| 76 | + tearDown(() async { |
| 77 | + await container.dispose(); |
| 78 | + }); |
| 79 | + |
| 80 | + group('Room.getSid', () { |
| 81 | + test('returns immediately when the join response carried the sid', () async { |
| 82 | + await connectWith(container, joinResponse); |
| 83 | + |
| 84 | + expect(await container.room.getSid(), 'room_sid'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('resolves when the sid arrives via a later RoomUpdate', () async { |
| 88 | + await connectWith(container, emptySidJoinResponse); |
| 89 | + |
| 90 | + final sidFuture = container.room.getSid(); |
| 91 | + container.wsConnector.onData(sidRoomUpdateResponse.writeToBuffer()); |
| 92 | + |
| 93 | + expect(await sidFuture, 'RM_issued_later'); |
| 94 | + }); |
| 95 | + |
| 96 | + test('completes with an empty sid when the room is disposed while waiting', () async { |
| 97 | + await connectWith(container, emptySidJoinResponse); |
| 98 | + |
| 99 | + final sidFuture = container.room.getSid(); |
| 100 | + await container.room.dispose(); |
| 101 | + |
| 102 | + expect(await sidFuture, ''); |
| 103 | + }); |
| 104 | + |
| 105 | + test('resolves with the join-response sid for callers waiting during connect', () async { |
| 106 | + final connectFuture = container.room.connect(exampleUri, token); |
| 107 | + Future.delayed(const Duration(milliseconds: 5), () { |
| 108 | + container.wsConnector.onData(joinResponse.writeToBuffer()); |
| 109 | + container.wsConnector.onData(offerResponse.writeToBuffer()); |
| 110 | + }); |
| 111 | + // Ask while the signal connection is still being established. |
| 112 | + await Future<void>.delayed(const Duration(milliseconds: 1)); |
| 113 | + final sidFuture = container.room.getSid(); |
| 114 | + |
| 115 | + await connectFuture; |
| 116 | + expect(await sidFuture, 'room_sid'); |
| 117 | + }); |
| 118 | + |
| 119 | + test('repeated calls do not accumulate dispose hooks', () async { |
| 120 | + await connectWith(container, emptySidJoinResponse); |
| 121 | + final hooksBefore = container.room.disposeFuncCount; |
| 122 | + |
| 123 | + for (var i = 0; i < 3; i++) { |
| 124 | + final sidFuture = container.room.getSid(); |
| 125 | + container.wsConnector.onData(sidRoomUpdateResponse.writeToBuffer()); |
| 126 | + await sidFuture; |
| 127 | + } |
| 128 | + |
| 129 | + expect(container.room.disposeFuncCount, hooksBefore); |
| 130 | + }); |
| 131 | + |
| 132 | + test('resolves for a caller arriving after the RoomUpdate landed', () async { |
| 133 | + await connectWith(container, emptySidJoinResponse); |
| 134 | + |
| 135 | + container.wsConnector.onData(sidRoomUpdateResponse.writeToBuffer()); |
| 136 | + // Let the signal event propagate to _applyRoomUpdate. |
| 137 | + await Future<void>.delayed(const Duration(milliseconds: 10)); |
| 138 | + |
| 139 | + expect(await container.room.getSid(), 'RM_issued_later'); |
| 140 | + }); |
| 141 | + }); |
| 142 | +} |
0 commit comments