Skip to content

Commit 40e025e

Browse files
authored
Async tests (#108)
* Async tests * Add swift 5.6 to CI
1 parent 4e0ccf7 commit 40e025e

File tree

2 files changed

+85
-106
lines changed

2 files changed

+85
-106
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
- swift:5.3
5252
- swift:5.4
5353
- swift:5.5
54+
- swift:5.6
5455
container:
5556
image: ${{ matrix.tag }}
5657
services:

Tests/MQTTNIOTests/MQTTNIOTests+async.swift

Lines changed: 84 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ final class AsyncMQTTNIOTests: XCTestCase {
3434
return logger
3535
}()
3636

37-
func XCTRunAsyncAndBlock(_ closure: @escaping () async throws -> Void) {
38-
let dg = DispatchGroup()
39-
dg.enter()
40-
Task {
41-
do {
42-
try await closure()
43-
} catch {
44-
XCTFail("\(error)")
45-
}
46-
dg.leave()
47-
}
48-
dg.wait()
49-
}
50-
5137
func createClient(identifier: String, version: MQTTClient.Version = .v3_1_1, timeout: TimeAmount? = .seconds(10)) -> MQTTClient {
5238
MQTTClient(
5339
host: Self.hostname,
@@ -59,49 +45,45 @@ final class AsyncMQTTNIOTests: XCTestCase {
5945
)
6046
}
6147

62-
func testConnect() {
48+
func testConnect() async throws {
6349
let client = self.createClient(identifier: "testConnect+async")
64-
self.XCTRunAsyncAndBlock {
65-
try await client.connect()
66-
try await client.disconnect()
67-
try await client.shutdown()
68-
}
50+
try await client.connect()
51+
try await client.disconnect()
52+
try await client.shutdown()
6953
}
7054

71-
func testPublishSubscribe() {
55+
func testPublishSubscribe() async throws {
7256
let expectation = XCTestExpectation(description: "testPublishSubscribe")
7357
expectation.expectedFulfillmentCount = 1
7458

7559
let client = self.createClient(identifier: "testPublish+async")
7660
let client2 = self.createClient(identifier: "testPublish+async2")
7761
let payloadString = "Hello"
78-
self.XCTRunAsyncAndBlock {
79-
try await client.connect()
80-
try await client2.connect()
81-
_ = try await client2.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)])
82-
client2.addPublishListener(named: "test") { result in
83-
switch result {
84-
case .success(let publish):
85-
var buffer = publish.payload
86-
let string = buffer.readString(length: buffer.readableBytes)
87-
XCTAssertEqual(string, payloadString)
88-
expectation.fulfill()
89-
case .failure(let error):
90-
XCTFail("\(error)")
91-
}
62+
try await client.connect()
63+
try await client2.connect()
64+
_ = try await client2.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)])
65+
client2.addPublishListener(named: "test") { result in
66+
switch result {
67+
case .success(let publish):
68+
var buffer = publish.payload
69+
let string = buffer.readString(length: buffer.readableBytes)
70+
XCTAssertEqual(string, payloadString)
71+
expectation.fulfill()
72+
case .failure(let error):
73+
XCTFail("\(error)")
9274
}
93-
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
75+
}
76+
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
9477

95-
self.wait(for: [expectation], timeout: 2)
78+
self.wait(for: [expectation], timeout: 2)
9679

97-
try await client.disconnect()
98-
try await client2.disconnect()
99-
try await client.shutdown()
100-
try await client2.shutdown()
101-
}
80+
try await client.disconnect()
81+
try await client2.disconnect()
82+
try await client.shutdown()
83+
try await client2.shutdown()
10284
}
10385

104-
func testPing() {
86+
func testPing() async throws {
10587
let client = MQTTClient(
10688
host: Self.hostname,
10789
port: 1883,
@@ -111,15 +93,13 @@ final class AsyncMQTTNIOTests: XCTestCase {
11193
configuration: .init(disablePing: true)
11294
)
11395

114-
self.XCTRunAsyncAndBlock {
115-
try await client.connect()
116-
try await client.ping()
117-
try await client.disconnect()
118-
try await client.shutdown()
119-
}
96+
try await client.connect()
97+
try await client.ping()
98+
try await client.disconnect()
99+
try await client.shutdown()
120100
}
121101

122-
func testAsyncSequencePublishListener() {
102+
func testAsyncSequencePublishListener() async throws {
123103
let expectation = XCTestExpectation(description: "testAsyncSequencePublishListener")
124104
expectation.expectedFulfillmentCount = 2
125105
let finishExpectation = XCTestExpectation(description: "testAsyncSequencePublishListener.finish")
@@ -128,43 +108,41 @@ final class AsyncMQTTNIOTests: XCTestCase {
128108
let client = self.createClient(identifier: "testAsyncSequencePublishListener+async", version: .v5_0)
129109
let client2 = self.createClient(identifier: "testAsyncSequencePublishListener+async2", version: .v5_0)
130110

131-
self.XCTRunAsyncAndBlock {
132-
try await client.connect()
133-
try await client2.connect()
134-
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)])
135-
let task = Task {
136-
let publishListener = client2.createPublishListener()
137-
for await result in publishListener {
138-
switch result {
139-
case .success(let publish):
140-
var buffer = publish.payload
141-
let string = buffer.readString(length: buffer.readableBytes)
142-
print("Received: \(string ?? "nothing")")
143-
expectation.fulfill()
144-
145-
case .failure(let error):
146-
XCTFail("\(error)")
147-
}
111+
try await client.connect()
112+
try await client2.connect()
113+
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)])
114+
let task = Task {
115+
let publishListener = client2.createPublishListener()
116+
for await result in publishListener {
117+
switch result {
118+
case .success(let publish):
119+
var buffer = publish.payload
120+
let string = buffer.readString(length: buffer.readableBytes)
121+
print("Received: \(string ?? "nothing")")
122+
expectation.fulfill()
123+
124+
case .failure(let error):
125+
XCTFail("\(error)")
148126
}
149-
finishExpectation.fulfill()
150127
}
151-
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: "Hello"), qos: .atLeastOnce)
152-
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: "Goodbye"), qos: .atLeastOnce)
153-
try await client.disconnect()
128+
finishExpectation.fulfill()
129+
}
130+
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: "Hello"), qos: .atLeastOnce)
131+
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: "Goodbye"), qos: .atLeastOnce)
132+
try await client.disconnect()
154133

155-
self.wait(for: [expectation], timeout: 5.0)
134+
self.wait(for: [expectation], timeout: 5.0)
156135

157-
try await client2.disconnect()
158-
try await client.shutdown()
159-
try await client2.shutdown()
136+
try await client2.disconnect()
137+
try await client.shutdown()
138+
try await client2.shutdown()
160139

161-
self.wait(for: [finishExpectation], timeout: 5.0)
140+
self.wait(for: [finishExpectation], timeout: 5.0)
162141

163-
_ = await task.result
164-
}
142+
_ = await task.result
165143
}
166144

167-
func testAsyncSequencePublishSubscriptionIdListener() {
145+
func testAsyncSequencePublishSubscriptionIdListener() async throws {
168146
let expectation = XCTestExpectation(description: "publish listener")
169147
let expectation2 = XCTestExpectation(description: "publish listener2")
170148
expectation.expectedFulfillmentCount = 3
@@ -173,38 +151,38 @@ final class AsyncMQTTNIOTests: XCTestCase {
173151
let client = self.createClient(identifier: "testAsyncSequencePublishSubscriptionIdListener+async", version: .v5_0)
174152
let client2 = self.createClient(identifier: "testAsyncSequencePublishSubscriptionIdListener+async2", version: .v5_0)
175153
let payloadString = "Hello"
176-
self.XCTRunAsyncAndBlock {
177-
try await client.connect()
178-
try await client2.connect()
179-
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)], properties: [.subscriptionIdentifier(1)])
180-
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject2", qos: .atLeastOnce)], properties: [.subscriptionIdentifier(2)])
181-
let task = Task {
182-
let publishListener = client2.v5.createPublishListener(subscriptionId: 1)
183-
for await _ in publishListener {
184-
expectation.fulfill()
185-
}
154+
155+
try await client.connect()
156+
try await client2.connect()
157+
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)], properties: [.subscriptionIdentifier(1)])
158+
_ = try await client2.v5.subscribe(to: [.init(topicFilter: "TestSubject2", qos: .atLeastOnce)], properties: [.subscriptionIdentifier(2)])
159+
let task = Task {
160+
let publishListener = client2.v5.createPublishListener(subscriptionId: 1)
161+
for await _ in publishListener {
186162
expectation.fulfill()
187163
}
188-
let task2 = Task {
189-
let publishListener = client2.v5.createPublishListener(subscriptionId: 2)
190-
for await _ in publishListener {
191-
expectation2.fulfill()
192-
}
164+
expectation.fulfill()
165+
}
166+
let task2 = Task {
167+
let publishListener = client2.v5.createPublishListener(subscriptionId: 2)
168+
for await _ in publishListener {
193169
expectation2.fulfill()
194170
}
195-
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
196-
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
197-
try await client.publish(to: "TestSubject2", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
198-
try await client.disconnect()
199-
Thread.sleep(forTimeInterval: 0.5)
200-
try await client2.disconnect()
201-
Thread.sleep(forTimeInterval: 0.5)
202-
try client.syncShutdownGracefully()
203-
try client2.syncShutdownGracefully()
204-
205-
_ = await task.result
206-
_ = await task2.result
171+
expectation2.fulfill()
207172
}
173+
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
174+
try await client.publish(to: "TestSubject", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
175+
try await client.publish(to: "TestSubject2", payload: ByteBufferAllocator().buffer(string: payloadString), qos: .atLeastOnce)
176+
try await client.disconnect()
177+
Thread.sleep(forTimeInterval: 0.5)
178+
try await client2.disconnect()
179+
Thread.sleep(forTimeInterval: 0.5)
180+
try client.syncShutdownGracefully()
181+
try client2.syncShutdownGracefully()
182+
183+
_ = await task.result
184+
_ = await task2.result
185+
208186
wait(for: [expectation, expectation2], timeout: 5.0)
209187
}
210188
}

0 commit comments

Comments
 (0)