@@ -37,126 +37,130 @@ final class RealtimeIntegrationTests: XCTestCase {
37
37
)
38
38
39
39
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
42
43
43
- let channel = realtime. channel ( " integration " ) {
44
- $0. broadcast. receiveOwnBroadcasts = true
45
- }
44
+ let channel = realtime. channel ( " integration " ) {
45
+ $0. broadcast. receiveOwnBroadcasts = true
46
+ }
46
47
47
- let receivedMessages = LockIsolated < [ JSONObject ] > ( [ ] )
48
+ let receivedMessages = LockIsolated < [ JSONObject ] > ( [ ] )
48
49
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 ( )
53
56
}
54
- expectation. fulfill ( )
55
57
}
56
- }
57
58
58
- await Task . megaYield ( )
59
+ await Task . yield ( )
59
60
60
- await channel. subscribe ( )
61
+ await channel. subscribe ( )
61
62
62
- struct Message : Codable {
63
- var value : Int
64
- }
63
+ struct Message : Codable {
64
+ var value : Int
65
+ }
65
66
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 ] )
69
70
70
- await fulfillment ( of: [ expectation] , timeout: 0.5 )
71
+ await fulfillment ( of: [ expectation] , timeout: 0.5 )
71
72
72
- XCTAssertNoDifference (
73
- receivedMessages. value,
74
- [
73
+ XCTAssertNoDifference (
74
+ receivedMessages. value,
75
75
[
76
- " event " : " test " ,
77
- " payload " : [
78
- " value " : 1 ,
76
+ [
77
+ " event " : " test " ,
78
+ " payload " : [
79
+ " value " : 1 ,
80
+ ] ,
81
+ " type " : " broadcast " ,
79
82
] ,
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 " ,
86
89
] ,
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 " ,
94
97
] ,
95
- " type " : " broadcast " ,
96
- ] ,
97
- ]
98
- )
98
+ ]
99
+ )
99
100
100
- await channel. unsubscribe ( )
101
+ await channel. unsubscribe ( )
102
+ }
101
103
}
102
104
103
105
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
+ }
107
110
108
- let expectation = expectation ( description: " presenceChange " )
109
- expectation. expectedFulfillmentCount = 4
111
+ let expectation = expectation ( description: " presenceChange " )
112
+ expectation. expectedFulfillmentCount = 4
110
113
111
- let receivedPresenceChanges = LockIsolated < [ any PresenceAction ] > ( [ ] )
114
+ let receivedPresenceChanges = LockIsolated < [ any PresenceAction ] > ( [ ] )
112
115
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 ( )
117
122
}
118
- expectation. fulfill ( )
119
123
}
120
- }
121
-
122
- await Task . megaYield ( )
123
124
124
- await channel . subscribe ( )
125
+ await Task . yield ( )
125
126
126
- struct UserState : Codable , Equatable {
127
- let email : String
128
- }
127
+ await channel. subscribe ( )
129
128
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
+ }
134
132
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] " ] )
136
135
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 ( )
148
137
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 )
158
139
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
+ }
160
164
}
161
165
162
166
// FIXME: Test getting stuck
@@ -179,7 +183,7 @@ final class RealtimeIntegrationTests: XCTestCase {
179
183
// await channel.postgresChange(AnyAction.self, schema: "public").prefix(3).collect()
180
184
// }
181
185
//
182
- // await Task.megaYield ()
186
+ // await Task.yield ()
183
187
// await channel.subscribe()
184
188
//
185
189
// struct Entry: Codable, Equatable {
0 commit comments