From 45b74c22d4da3bfe6bfe86f923a2711b363fcca7 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 3 Dec 2024 15:46:15 -0300 Subject: [PATCH] Tighten check on `get` reusing room The existing check was not great, since the factory only ever returns a single instance. --- Tests/AblyChatTests/DefaultRoomsTests.swift | 6 ++++-- Tests/AblyChatTests/Mocks/MockRoomFactory.swift | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Tests/AblyChatTests/DefaultRoomsTests.swift b/Tests/AblyChatTests/DefaultRoomsTests.swift index 797bc75a..934ecb1c 100644 --- a/Tests/AblyChatTests/DefaultRoomsTests.swift +++ b/Tests/AblyChatTests/DefaultRoomsTests.swift @@ -40,7 +40,8 @@ struct DefaultRoomsTests { ])) let options = RoomOptions() let roomToReturn = MockRoom(options: options) - let rooms = DefaultRooms(realtime: realtime, clientOptions: .init(), logger: TestLogger(), roomFactory: MockRoomFactory(room: roomToReturn)) + let roomFactory = MockRoomFactory(room: roomToReturn) + let rooms = DefaultRooms(realtime: realtime, clientOptions: .init(), logger: TestLogger(), roomFactory: roomFactory) let roomID = "basketball" let firstRoom = try await rooms.get(roomID: roomID, options: options) @@ -48,7 +49,8 @@ struct DefaultRoomsTests { // When: get(roomID:options:) is called with the same room ID let secondRoom = try await rooms.get(roomID: roomID, options: options) - // Then: It returns the same room object + // Then: It does not create another room, and returns the same room object + #expect(await roomFactory.createRoomCallCount == 1) #expect(secondRoom === firstRoom) } diff --git a/Tests/AblyChatTests/Mocks/MockRoomFactory.swift b/Tests/AblyChatTests/Mocks/MockRoomFactory.swift index f24ad3d1..b7ac6acc 100644 --- a/Tests/AblyChatTests/Mocks/MockRoomFactory.swift +++ b/Tests/AblyChatTests/Mocks/MockRoomFactory.swift @@ -2,6 +2,7 @@ actor MockRoomFactory: RoomFactory { private var room: MockRoom? + private(set) var createRoomCallCount = 0 private(set) var createRoomArguments: (realtime: RealtimeClient, chatAPI: ChatAPI, roomID: String, options: RoomOptions, logger: any InternalLogger)? init(room: MockRoom? = nil) { @@ -13,6 +14,7 @@ actor MockRoomFactory: RoomFactory { } func createRoom(realtime: RealtimeClient, chatAPI: ChatAPI, roomID: String, options: RoomOptions, logger: any InternalLogger) async throws -> MockRoom { + createRoomCallCount += 1 createRoomArguments = (realtime: realtime, chatAPI: chatAPI, roomID: roomID, options: options, logger: logger) guard let room else {