@@ -100,10 +100,8 @@ final class AsyncMQTTNIOTests: XCTestCase {
100
100
}
101
101
102
102
func testAsyncSequencePublishListener( ) async throws {
103
- let expectation = XCTestExpectation ( description: " testAsyncSequencePublishListener " )
104
- expectation. expectedFulfillmentCount = 2
105
- let finishExpectation = XCTestExpectation ( description: " testAsyncSequencePublishListener.finish " )
106
- finishExpectation. expectedFulfillmentCount = 1
103
+ let expectation = NIOAtomic . makeAtomic ( value: 0 )
104
+ let finishExpectation = NIOAtomic . makeAtomic ( value: 0 )
107
105
108
106
let client = self . createClient ( identifier: " testAsyncSequencePublishListener+async " , version: . v5_0)
109
107
let client2 = self . createClient ( identifier: " testAsyncSequencePublishListener+async2 " , version: . v5_0)
@@ -119,34 +117,33 @@ final class AsyncMQTTNIOTests: XCTestCase {
119
117
var buffer = publish. payload
120
118
let string = buffer. readString ( length: buffer. readableBytes)
121
119
print ( " Received: \( string ?? " nothing " ) " )
122
- expectation. fulfill ( )
120
+ expectation. add ( 1 )
123
121
124
122
case . failure( let error) :
125
123
XCTFail ( " \( error) " )
126
124
}
127
125
}
128
- finishExpectation. fulfill ( )
126
+ finishExpectation. add ( 1 )
129
127
}
130
128
try await client. publish ( to: " TestSubject " , payload: ByteBufferAllocator ( ) . buffer ( string: " Hello " ) , qos: . atLeastOnce)
131
129
try await client. publish ( to: " TestSubject " , payload: ByteBufferAllocator ( ) . buffer ( string: " Goodbye " ) , qos: . atLeastOnce)
132
130
try await client. disconnect ( )
133
131
134
- self . wait ( for : [ expectation ] , timeout : 5.0 )
132
+ _ = try await Task . sleep ( nanoseconds : 500_000_000 )
135
133
136
134
try await client2. disconnect ( )
137
135
try await client. shutdown ( )
138
136
try await client2. shutdown ( )
139
137
140
- self . wait ( for: [ finishExpectation] , timeout: 5.0 )
141
-
142
138
_ = await task. result
139
+
140
+ XCTAssertEqual ( expectation. load ( ) , 2 )
141
+ XCTAssertEqual ( finishExpectation. load ( ) , 1 )
143
142
}
144
143
145
144
func testAsyncSequencePublishSubscriptionIdListener( ) async throws {
146
- let expectation = XCTestExpectation ( description: " publish listener " )
147
- let expectation2 = XCTestExpectation ( description: " publish listener2 " )
148
- expectation. expectedFulfillmentCount = 3
149
- expectation2. expectedFulfillmentCount = 2
145
+ let expectation = NIOAtomic . makeAtomic ( value: 0 )
146
+ let expectation2 = NIOAtomic . makeAtomic ( value: 0 )
150
147
151
148
let client = self . createClient ( identifier: " testAsyncSequencePublishSubscriptionIdListener+async " , version: . v5_0)
152
149
let client2 = self . createClient ( identifier: " testAsyncSequencePublishSubscriptionIdListener+async2 " , version: . v5_0)
@@ -159,31 +156,34 @@ final class AsyncMQTTNIOTests: XCTestCase {
159
156
let task = Task {
160
157
let publishListener = client2. v5. createPublishListener ( subscriptionId: 1 )
161
158
for await _ in publishListener {
162
- expectation. fulfill ( )
159
+ expectation. add ( 1 )
163
160
}
164
- expectation. fulfill ( )
161
+ expectation. add ( 1 )
165
162
}
166
163
let task2 = Task {
167
164
let publishListener = client2. v5. createPublishListener ( subscriptionId: 2 )
168
165
for await _ in publishListener {
169
- expectation2. fulfill ( )
166
+ expectation2. add ( 1 )
170
167
}
171
- expectation2. fulfill ( )
168
+ expectation2. add ( 1 )
172
169
}
173
170
try await client. publish ( to: " TestSubject " , payload: ByteBufferAllocator ( ) . buffer ( string: payloadString) , qos: . atLeastOnce)
174
171
try await client. publish ( to: " TestSubject " , payload: ByteBufferAllocator ( ) . buffer ( string: payloadString) , qos: . atLeastOnce)
175
172
try await client. publish ( to: " TestSubject2 " , payload: ByteBufferAllocator ( ) . buffer ( string: payloadString) , qos: . atLeastOnce)
173
+
176
174
try await client. disconnect ( )
177
- Thread . sleep ( forTimeInterval: 0.5 )
175
+
176
+ _ = try await Task . sleep ( nanoseconds: 500_000_000 )
177
+
178
178
try await client2. disconnect ( )
179
- Thread . sleep ( forTimeInterval: 0.5 )
180
179
try client. syncShutdownGracefully ( )
181
180
try client2. syncShutdownGracefully ( )
182
181
183
182
_ = await task. result
184
183
_ = await task2. result
185
184
186
- wait ( for: [ expectation, expectation2] , timeout: 5.0 )
185
+ XCTAssertEqual ( expectation. load ( ) , 3 )
186
+ XCTAssertEqual ( expectation2. load ( ) , 2 )
187
187
}
188
188
}
189
189
0 commit comments