Skip to content

Commit

Permalink
Tighten check on get reusing room
Browse files Browse the repository at this point in the history
The existing check was not great, since the factory only ever returns a
single instance.
  • Loading branch information
lawrence-forooghian committed Dec 3, 2024
1 parent 363e944 commit 45b74c2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Tests/AblyChatTests/DefaultRoomsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ 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)

// 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)
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/AblyChatTests/Mocks/MockRoomFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 45b74c2

Please sign in to comment.