Skip to content

Commit

Permalink
Fixed MockTyping and MockPresence to use mockSubscription for generat…
Browse files Browse the repository at this point in the history
…ing user events.
  • Loading branch information
maratal committed Sep 9, 2024
1 parent 7a70359 commit 89dd2e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
14 changes: 7 additions & 7 deletions Example/AblyChatExample/Mocks/MockSubscriptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ struct MockTypingSubscription: Sendable, AsyncSequence {
private let stream: AsyncStream<Element>
private let continuation: AsyncStream<Element>.Continuation

func emit() {
let typing = TypingEvent(currentlyTyping: [MockStrings.names.randomElement()!, MockStrings.names.randomElement()!])
func emit(names: Set<String>) {
let typing = TypingEvent(currentlyTyping: names)
continuation.yield(typing)
}

func emitTypings() {
Task {
while (true) {
try? await Task.sleep(nanoseconds: 2 * 1_000_000_000)
emit()
emit(names: [MockStrings.names.randomElement()!, MockStrings.names.randomElement()!])
}
}
}
Expand Down Expand Up @@ -137,9 +137,9 @@ struct MockPresenceSubscription: Sendable, AsyncSequence {
private let stream: AsyncStream<Element>
private let continuation: AsyncStream<Element>.Continuation

func emitPresenceEvent() {
let presence = PresenceEvent(action: [.enter, .leave].randomElement()!,
clientID: MockStrings.names.randomElement()!,
func emitPresenceEvent(_ event: PresenceEventType, clientID: String) {
let presence = PresenceEvent(action: event,
clientID: clientID,
timestamp: Date(),
data: nil)
continuation.yield(presence)
Expand All @@ -149,7 +149,7 @@ struct MockPresenceSubscription: Sendable, AsyncSequence {
Task {
while (true) {
try? await Task.sleep(nanoseconds: 5 * 1_000_000_000)
emitPresenceEvent()
emitPresenceEvent([.enter, .leave].randomElement()!, clientID: MockStrings.names.randomElement()!)
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions Example/AblyChatExample/Mocks/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,29 @@ actor MockTyping: Typing {
let roomID: String
let channel: RealtimeChannel

private var mockSubscription: MockTypingSubscription

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.channel = MockRealtimeChannel()
self.mockSubscription = MockTypingSubscription(clientID: clientID, roomID: roomID)
}

func subscribe(bufferingPolicy: BufferingPolicy) -> Subscription<TypingEvent> {
.init(mockAsyncSequence: MockTypingSubscription(clientID: clientID, roomID: roomID))
.init(mockAsyncSequence: mockSubscription)
}

func get() async throws -> Set<String> {
Set(MockStrings.names.prefix(2))
}

func start() async throws {
fatalError("Not yet implemented")
mockSubscription.emit(names: [clientID])
}

func stop() async throws {
fatalError("Not yet implemented")
mockSubscription.emit(names: [clientID])
}

func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {
Expand All @@ -173,9 +176,12 @@ actor MockPresence: Presence {
let clientID: String
let roomID: String

private var mockSubscription: MockPresenceSubscription

init(clientID: String, roomID: String) {
self.clientID = clientID
self.roomID = roomID
self.mockSubscription = MockPresenceSubscription(clientID: clientID, roomID: roomID)
}

func get() async throws -> any PaginatedResult<PresenceMember> {
Expand All @@ -191,11 +197,11 @@ actor MockPresence: Presence {
}

func enter() async throws {
fatalError("Not yet implemented")
mockSubscription.emitPresenceEvent(.enter, clientID: clientID)
}

func enter(data: PresenceData) async throws {
fatalError("Not yet implemented")
mockSubscription.emitPresenceEvent(.enter, clientID: clientID)
}

func update() async throws {
Expand All @@ -207,19 +213,19 @@ actor MockPresence: Presence {
}

func leave() async throws {
fatalError("Not yet implemented")
mockSubscription.emitPresenceEvent(.leave, clientID: clientID)
}

func leave(data: PresenceData) async throws {
fatalError("Not yet implemented")
mockSubscription.emitPresenceEvent(.leave, clientID: clientID)
}

func subscribe(event: PresenceEventType) -> Subscription<PresenceEvent> {
.init(mockAsyncSequence: MockPresenceSubscription(clientID: clientID, roomID: roomID))
.init(mockAsyncSequence: mockSubscription)
}

func subscribe(events: [PresenceEventType]) -> Subscription<PresenceEvent> {
.init(mockAsyncSequence: MockPresenceSubscription(clientID: clientID, roomID: roomID))
.init(mockAsyncSequence: mockSubscription)
}

func subscribeToDiscontinuities() async -> Subscription<ARTErrorInfo> {
Expand Down

0 comments on commit 89dd2e1

Please sign in to comment.