@@ -37,126 +37,130 @@ final class RealtimeIntegrationTests: XCTestCase {
3737 )
3838
3939 func testBroadcast( ) async throws {
40- let expectation = expectation ( description: " receivedBroadcastMessages " )
41- expectation. expectedFulfillmentCount = 3
40+ try await withMainSerialExecutor {
41+ let expectation = expectation ( description: " receivedBroadcastMessages " )
42+ expectation. expectedFulfillmentCount = 3
4243
43- let channel = realtime. channel ( " integration " ) {
44- $0. broadcast. receiveOwnBroadcasts = true
45- }
44+ let channel = realtime. channel ( " integration " ) {
45+ $0. broadcast. receiveOwnBroadcasts = true
46+ }
4647
47- let receivedMessages = LockIsolated < [ JSONObject ] > ( [ ] )
48+ let receivedMessages = LockIsolated < [ JSONObject ] > ( [ ] )
4849
49- Task {
50- for await message in channel. broadcastStream ( event: " test " ) {
51- receivedMessages. withValue {
52- $0. append ( message)
50+ Task {
51+ for await message in channel. broadcastStream ( event: " test " ) {
52+ receivedMessages. withValue {
53+ $0. append ( message)
54+ }
55+ expectation. fulfill ( )
5356 }
54- expectation. fulfill ( )
5557 }
56- }
5758
58- await Task . megaYield ( )
59+ await Task . yield ( )
5960
60- await channel. subscribe ( )
61+ await channel. subscribe ( )
6162
62- struct Message : Codable {
63- var value : Int
64- }
63+ struct Message : Codable {
64+ var value : Int
65+ }
6566
66- try await channel. broadcast ( event: " test " , message: Message ( value: 1 ) )
67- try await channel. broadcast ( event: " test " , message: Message ( value: 2 ) )
68- try await channel. broadcast ( event: " test " , message: [ " value " : 3 , " another_value " : 42 ] )
67+ try await channel. broadcast ( event: " test " , message: Message ( value: 1 ) )
68+ try await channel. broadcast ( event: " test " , message: Message ( value: 2 ) )
69+ try await channel. broadcast ( event: " test " , message: [ " value " : 3 , " another_value " : 42 ] )
6970
70- await fulfillment ( of: [ expectation] , timeout: 0.5 )
71+ await fulfillment ( of: [ expectation] , timeout: 0.5 )
7172
72- XCTAssertNoDifference (
73- receivedMessages. value,
74- [
73+ XCTAssertNoDifference (
74+ receivedMessages. value,
7575 [
76- " event " : " test " ,
77- " payload " : [
78- " value " : 1 ,
76+ [
77+ " event " : " test " ,
78+ " payload " : [
79+ " value " : 1 ,
80+ ] ,
81+ " type " : " broadcast " ,
7982 ] ,
80- " type " : " broadcast " ,
81- ] ,
82- [
83- " event " : " test " ,
84- " payload " : [
85- " value " : 2 ,
83+ [
84+ " event " : " test " ,
85+ " payload " : [
86+ " value " : 2 ,
87+ ] ,
88+ " type " : " broadcast " ,
8689 ] ,
87- " type " : " broadcast " ,
88- ] ,
89- [
90- " event " : " test " ,
91- " payload " : [
92- " value " : 3 ,
93- " another_value " : 42 ,
90+ [
91+ " event " : " test " ,
92+ " payload " : [
93+ " value " : 3 ,
94+ " another_value " : 42 ,
95+ ] ,
96+ " type " : " broadcast " ,
9497 ] ,
95- " type " : " broadcast " ,
96- ] ,
97- ]
98- )
98+ ]
99+ )
99100
100- await channel. unsubscribe ( )
101+ await channel. unsubscribe ( )
102+ }
101103 }
102104
103105 func testPresence( ) async throws {
104- let channel = realtime. channel ( " integration " ) {
105- $0. broadcast. receiveOwnBroadcasts = true
106- }
106+ try await withMainSerialExecutor {
107+ let channel = realtime. channel ( " integration " ) {
108+ $0. broadcast. receiveOwnBroadcasts = true
109+ }
107110
108- let expectation = expectation ( description: " presenceChange " )
109- expectation. expectedFulfillmentCount = 4
111+ let expectation = expectation ( description: " presenceChange " )
112+ expectation. expectedFulfillmentCount = 4
110113
111- let receivedPresenceChanges = LockIsolated < [ any PresenceAction ] > ( [ ] )
114+ let receivedPresenceChanges = LockIsolated < [ any PresenceAction ] > ( [ ] )
112115
113- Task {
114- for await presence in channel. presenceChange ( ) {
115- receivedPresenceChanges. withValue {
116- $0. append ( presence)
116+ Task {
117+ for await presence in channel. presenceChange ( ) {
118+ receivedPresenceChanges. withValue {
119+ $0. append ( presence)
120+ }
121+ expectation. fulfill ( )
117122 }
118- expectation. fulfill ( )
119123 }
120- }
121-
122- await Task . megaYield ( )
123124
124- await channel . subscribe ( )
125+ await Task . yield ( )
125126
126- struct UserState : Codable , Equatable {
127- let email : String
128- }
127+ await channel. subscribe ( )
129128
130- try await channel
. track ( UserState ( email
: " [email protected] " ) ) 131- try await channel
. track ( [ " email " : " [email protected] " ] ) 132-
133- await channel. untrack ( )
129+ struct UserState : Codable , Equatable {
130+ let email : String
131+ }
134132
135- await fulfillment ( of: [ expectation] , timeout: 0.5 )
133+ try await channel
. track ( UserState ( email
: " [email protected] " ) ) 134+ try await channel
. track ( [ " email " : " [email protected] " ] ) 136135
137- let joins = try receivedPresenceChanges. value. map { try $0. decodeJoins ( as: UserState . self) }
138- let leaves = try receivedPresenceChanges. value. map { try $0. decodeLeaves ( as: UserState . self) }
139- XCTAssertNoDifference (
140- joins,
141- [
142- [ ] , // This is the first PRESENCE_STATE event.
143- [ UserState ( email
: " [email protected] " ) ] , 144- [ UserState ( email
: " [email protected] " ) ] , 145- [ ] ,
146- ]
147- )
136+ await channel. untrack ( )
148137
149- XCTAssertNoDifference (
150- leaves,
151- [
152- [ ] , // This is the first PRESENCE_STATE event.
153- [ ] ,
154- [ UserState ( email
: " [email protected] " ) ] , 155- [ UserState ( email
: " [email protected] " ) ] , 156- ]
157- )
138+ await fulfillment ( of: [ expectation] , timeout: 0.5 )
158139
159- await channel. unsubscribe ( )
140+ let joins = try receivedPresenceChanges. value. map { try $0. decodeJoins ( as: UserState . self) }
141+ let leaves = try receivedPresenceChanges. value. map { try $0. decodeLeaves ( as: UserState . self) }
142+ XCTAssertNoDifference (
143+ joins,
144+ [
145+ [ ] , // This is the first PRESENCE_STATE event.
146+ [ UserState ( email
: " [email protected] " ) ] , 147+ [ UserState ( email
: " [email protected] " ) ] , 148+ [ ] ,
149+ ]
150+ )
151+
152+ XCTAssertNoDifference (
153+ leaves,
154+ [
155+ [ ] , // This is the first PRESENCE_STATE event.
156+ [ ] ,
157+ [ UserState ( email
: " [email protected] " ) ] , 158+ [ UserState ( email
: " [email protected] " ) ] , 159+ ]
160+ )
161+
162+ await channel. unsubscribe ( )
163+ }
160164 }
161165
162166 // FIXME: Test getting stuck
@@ -179,7 +183,7 @@ final class RealtimeIntegrationTests: XCTestCase {
179183// await channel.postgresChange(AnyAction.self, schema: "public").prefix(3).collect()
180184// }
181185//
182- // await Task.megaYield ()
186+ // await Task.yield ()
183187// await channel.subscribe()
184188//
185189// struct Entry: Codable, Equatable {
0 commit comments