Skip to content

Commit 0b25e4a

Browse files
committed
fix(realtime): stabilize channel tests on ci
1 parent b34f437 commit 0b25e4a

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

Tests/RealtimeTests/RealtimeChannelTests.swift

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Guilherme Souza on 09/09/24.
66
//
77

8+
import Foundation
89
import InlineSnapshotTesting
910
import TestHelpers
1011
import XCTest
@@ -170,12 +171,11 @@ final class RealtimeChannelTests: XCTestCase {
170171
}
171172

172173
// Wait for the join message to be sent
173-
await Task.megaYield()
174-
175-
// Check the sent events to verify presence enabled is set correctly
176-
let joinEvents = server.receivedEvents.compactMap { $0.realtimeMessage }.filter {
177-
$0.event == "phx_join"
178-
}
174+
let joinEvents = await waitForEvents(
175+
in: server,
176+
event: "phx_join",
177+
timeout: 1.0
178+
)
179179

180180
// Should have at least one join event
181181
XCTAssertGreaterThan(joinEvents.count, 0)
@@ -442,10 +442,12 @@ final class RealtimeChannelTests: XCTestCase {
442442
try await channel.httpSend(event: "test", message: ["data": "test"])
443443
XCTFail("Expected httpSend to throw an error on 503 status")
444444
} catch {
445-
// Should fall back to localized status text
445+
// Should fall back to localized status text (case-insensitive)
446+
let description = error.localizedDescription.lowercased()
446447
XCTAssertTrue(
447-
error.localizedDescription.contains("503")
448-
|| error.localizedDescription.contains("unavailable"))
448+
description.contains("503") || description.contains("unavailable"),
449+
"Expected status text fallback, got '\(error.localizedDescription)'"
450+
)
449451
}
450452
}
451453
}
@@ -461,3 +463,29 @@ private struct BroadcastPayload: Decodable {
461463
let `private`: Bool
462464
}
463465
}
466+
467+
extension RealtimeChannelTests {
468+
@MainActor
469+
private func waitForEvents(
470+
in socket: FakeWebSocket,
471+
event: String,
472+
timeout: TimeInterval,
473+
pollInterval: UInt64 = 10_000_000
474+
) async -> [RealtimeMessageV2] {
475+
let deadline = Date().addingTimeInterval(timeout)
476+
477+
while Date() < deadline {
478+
let events = socket.receivedEvents.compactMap { $0.realtimeMessage }.filter {
479+
$0.event == event
480+
}
481+
482+
if !events.isEmpty {
483+
return events
484+
}
485+
486+
try? await Task.sleep(nanoseconds: pollInterval)
487+
}
488+
489+
return []
490+
}
491+
}

0 commit comments

Comments
 (0)