-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(realtime): fix MockWebSocketClient (#461)
* test(realtime): use mainSerialExecutor * fix MockWebSocketClient
- Loading branch information
Showing
5 changed files
with
271 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,126 +37,130 @@ final class RealtimeIntegrationTests: XCTestCase { | |
) | ||
|
||
func testBroadcast() async throws { | ||
let expectation = expectation(description: "receivedBroadcastMessages") | ||
expectation.expectedFulfillmentCount = 3 | ||
try await withMainSerialExecutor { | ||
let expectation = expectation(description: "receivedBroadcastMessages") | ||
expectation.expectedFulfillmentCount = 3 | ||
|
||
let channel = realtime.channel("integration") { | ||
$0.broadcast.receiveOwnBroadcasts = true | ||
} | ||
let channel = realtime.channel("integration") { | ||
$0.broadcast.receiveOwnBroadcasts = true | ||
} | ||
|
||
let receivedMessages = LockIsolated<[JSONObject]>([]) | ||
let receivedMessages = LockIsolated<[JSONObject]>([]) | ||
|
||
Task { | ||
for await message in channel.broadcastStream(event: "test") { | ||
receivedMessages.withValue { | ||
$0.append(message) | ||
Task { | ||
for await message in channel.broadcastStream(event: "test") { | ||
receivedMessages.withValue { | ||
$0.append(message) | ||
} | ||
expectation.fulfill() | ||
} | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
await Task.megaYield() | ||
await Task.yield() | ||
|
||
await channel.subscribe() | ||
await channel.subscribe() | ||
|
||
struct Message: Codable { | ||
var value: Int | ||
} | ||
struct Message: Codable { | ||
var value: Int | ||
} | ||
|
||
try await channel.broadcast(event: "test", message: Message(value: 1)) | ||
try await channel.broadcast(event: "test", message: Message(value: 2)) | ||
try await channel.broadcast(event: "test", message: ["value": 3, "another_value": 42]) | ||
try await channel.broadcast(event: "test", message: Message(value: 1)) | ||
try await channel.broadcast(event: "test", message: Message(value: 2)) | ||
try await channel.broadcast(event: "test", message: ["value": 3, "another_value": 42]) | ||
|
||
await fulfillment(of: [expectation], timeout: 0.5) | ||
await fulfillment(of: [expectation], timeout: 0.5) | ||
|
||
XCTAssertNoDifference( | ||
receivedMessages.value, | ||
[ | ||
XCTAssertNoDifference( | ||
receivedMessages.value, | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 1, | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 1, | ||
], | ||
"type": "broadcast", | ||
], | ||
"type": "broadcast", | ||
], | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 2, | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 2, | ||
], | ||
"type": "broadcast", | ||
], | ||
"type": "broadcast", | ||
], | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 3, | ||
"another_value": 42, | ||
[ | ||
"event": "test", | ||
"payload": [ | ||
"value": 3, | ||
"another_value": 42, | ||
], | ||
"type": "broadcast", | ||
], | ||
"type": "broadcast", | ||
], | ||
] | ||
) | ||
] | ||
) | ||
|
||
await channel.unsubscribe() | ||
await channel.unsubscribe() | ||
} | ||
} | ||
|
||
func testPresence() async throws { | ||
let channel = realtime.channel("integration") { | ||
$0.broadcast.receiveOwnBroadcasts = true | ||
} | ||
try await withMainSerialExecutor { | ||
let channel = realtime.channel("integration") { | ||
$0.broadcast.receiveOwnBroadcasts = true | ||
} | ||
|
||
let expectation = expectation(description: "presenceChange") | ||
expectation.expectedFulfillmentCount = 4 | ||
let expectation = expectation(description: "presenceChange") | ||
expectation.expectedFulfillmentCount = 4 | ||
|
||
let receivedPresenceChanges = LockIsolated<[any PresenceAction]>([]) | ||
let receivedPresenceChanges = LockIsolated<[any PresenceAction]>([]) | ||
|
||
Task { | ||
for await presence in channel.presenceChange() { | ||
receivedPresenceChanges.withValue { | ||
$0.append(presence) | ||
Task { | ||
for await presence in channel.presenceChange() { | ||
receivedPresenceChanges.withValue { | ||
$0.append(presence) | ||
} | ||
expectation.fulfill() | ||
} | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
await Task.megaYield() | ||
|
||
await channel.subscribe() | ||
await Task.yield() | ||
|
||
struct UserState: Codable, Equatable { | ||
let email: String | ||
} | ||
await channel.subscribe() | ||
|
||
try await channel.track(UserState(email: "[email protected]")) | ||
try await channel.track(["email": "[email protected]"]) | ||
|
||
await channel.untrack() | ||
struct UserState: Codable, Equatable { | ||
let email: String | ||
} | ||
|
||
await fulfillment(of: [expectation], timeout: 0.5) | ||
try await channel.track(UserState(email: "[email protected]")) | ||
try await channel.track(["email": "[email protected]"]) | ||
|
||
let joins = try receivedPresenceChanges.value.map { try $0.decodeJoins(as: UserState.self) } | ||
let leaves = try receivedPresenceChanges.value.map { try $0.decodeLeaves(as: UserState.self) } | ||
XCTAssertNoDifference( | ||
joins, | ||
[ | ||
[], // This is the first PRESENCE_STATE event. | ||
[UserState(email: "[email protected]")], | ||
[UserState(email: "[email protected]")], | ||
[], | ||
] | ||
) | ||
await channel.untrack() | ||
|
||
XCTAssertNoDifference( | ||
leaves, | ||
[ | ||
[], // This is the first PRESENCE_STATE event. | ||
[], | ||
[UserState(email: "[email protected]")], | ||
[UserState(email: "[email protected]")], | ||
] | ||
) | ||
await fulfillment(of: [expectation], timeout: 0.5) | ||
|
||
await channel.unsubscribe() | ||
let joins = try receivedPresenceChanges.value.map { try $0.decodeJoins(as: UserState.self) } | ||
let leaves = try receivedPresenceChanges.value.map { try $0.decodeLeaves(as: UserState.self) } | ||
XCTAssertNoDifference( | ||
joins, | ||
[ | ||
[], // This is the first PRESENCE_STATE event. | ||
[UserState(email: "[email protected]")], | ||
[UserState(email: "[email protected]")], | ||
[], | ||
] | ||
) | ||
|
||
XCTAssertNoDifference( | ||
leaves, | ||
[ | ||
[], // This is the first PRESENCE_STATE event. | ||
[], | ||
[UserState(email: "[email protected]")], | ||
[UserState(email: "[email protected]")], | ||
] | ||
) | ||
|
||
await channel.unsubscribe() | ||
} | ||
} | ||
|
||
// FIXME: Test getting stuck | ||
|
@@ -179,7 +183,7 @@ final class RealtimeIntegrationTests: XCTestCase { | |
// await channel.postgresChange(AnyAction.self, schema: "public").prefix(3).collect() | ||
// } | ||
// | ||
// await Task.megaYield() | ||
// await Task.yield() | ||
// await channel.subscribe() | ||
// | ||
// struct Entry: Codable, Equatable { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.