Skip to content

Commit 11a29a6

Browse files
authored
Wrap concurrency code in canImport(_Concurrency) test (#70)
* Wrap concurrency code in canImport(_Concurrency) test * swift format * Set correct @available on async tests
1 parent aa3222c commit 11a29a6

File tree

10 files changed

+42
-40
lines changed

10 files changed

+42
-40
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let package = Package(
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
13-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.21.0"),
13+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.3"),
1414
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.14.0"),
1515
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.6.0"),
1616
],

Sources/MQTTNIO/MQTTClient+async.swift renamed to Sources/MQTTNIO/AsyncAwaitSupport/MQTTClient+async.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if compiler(>=5.5)
1+
#if compiler(>=5.5) && canImport(_Concurrency)
22

33
import _NIOConcurrency
44
import NIO
@@ -56,7 +56,7 @@ extension MQTTClient {
5656
/// Completes when UNSUBACK is received
5757
/// - Parameter subscriptions: List of subscriptions to unsubscribe from
5858
public func unsubscribe(from subscriptions: [String]) async throws {
59-
return try await unsubscribe(from: subscriptions).get()
59+
return try await self.unsubscribe(from: subscriptions).get()
6060
}
6161

6262
/// Ping the server to test if it is still alive and to tell it you are alive.
@@ -67,15 +67,13 @@ extension MQTTClient {
6767
/// the connection is still live. If you initialize the client with the configuration `disablePingReq: true` then these
6868
/// are disabled and it is up to you to send the PINGREQ messages yourself
6969
public func ping() async throws {
70-
return try await ping().get()
70+
return try await self.ping().get()
7171
}
7272

7373
/// Disconnect from server
7474
public func disconnect() async throws {
75-
return try await disconnect().get()
75+
return try await self.disconnect().get()
7676
}
77-
7877
}
7978

8079
#endif // compiler(>=5.5)
81-

Sources/MQTTNIO/MQTTClientV5+async.swift renamed to Sources/MQTTNIO/AsyncAwaitSupport/MQTTClientV5+async.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if compiler(>=5.5)
1+
#if compiler(>=5.5) && canImport(_Concurrency)
22

33
import _NIOConcurrency
44
import NIO
@@ -24,7 +24,7 @@ extension MQTTClient.V5 {
2424
will: (topicName: String, payload: ByteBuffer, qos: MQTTQoS, retain: Bool, properties: MQTTProperties)? = nil,
2525
authWorkflow: ((MQTTAuthV5, EventLoop) -> EventLoopFuture<MQTTAuthV5>)? = nil
2626
) async throws -> MQTTConnackV5 {
27-
return try await connect(cleanStart: cleanStart, properties: properties, will: will, authWorkflow: authWorkflow).get()
27+
return try await self.connect(cleanStart: cleanStart, properties: properties, will: will, authWorkflow: authWorkflow).get()
2828
}
2929

3030
/// Publish message to topic
@@ -44,7 +44,7 @@ extension MQTTClient.V5 {
4444
retain: Bool = false,
4545
properties: MQTTProperties = .init()
4646
) async throws -> MQTTAckV5? {
47-
return try await publish(to: topicName, payload: payload, qos: qos, retain: retain, properties: properties).get()
47+
return try await self.publish(to: topicName, payload: payload, qos: qos, retain: retain, properties: properties).get()
4848
}
4949

5050
/// Subscribe to topic
@@ -57,7 +57,7 @@ extension MQTTClient.V5 {
5757
to subscriptions: [MQTTSubscribeInfoV5],
5858
properties: MQTTProperties = .init()
5959
) async throws -> MQTTSubackV5 {
60-
return try await subscribe(to: subscriptions, properties: properties).get()
60+
return try await self.subscribe(to: subscriptions, properties: properties).get()
6161
}
6262

6363
/// Unsubscribe from topic
@@ -70,16 +70,16 @@ extension MQTTClient.V5 {
7070
from subscriptions: [String],
7171
properties: MQTTProperties = .init()
7272
) async throws -> MQTTSubackV5 {
73-
return try await unsubscribe(from: subscriptions, properties: properties).get()
73+
return try await self.unsubscribe(from: subscriptions, properties: properties).get()
7474
}
7575

7676
/// Disconnect from server
7777
/// - Parameter properties: properties to attach to disconnect packet
7878
/// - Returns: Future waiting on disconnect message to be sent
7979
public func disconnect(properties: MQTTProperties = .init()) async throws {
80-
return try await disconnect(properties: properties).get()
80+
return try await self.disconnect(properties: properties).get()
8181
}
82-
82+
8383
/// Re-authenticate with server
8484
///
8585
/// - Parameters:
@@ -90,7 +90,7 @@ extension MQTTClient.V5 {
9090
properties: MQTTProperties,
9191
authWorkflow: ((MQTTAuthV5, EventLoop) -> EventLoopFuture<MQTTAuthV5>)? = nil
9292
) async throws -> MQTTAuthV5 {
93-
return try await auth(properties: properties, authWorkflow: authWorkflow).get()
93+
return try await self.auth(properties: properties, authWorkflow: authWorkflow).get()
9494
}
9595
}
9696

Sources/MQTTNIO/MQTTClientV5.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension MQTTClient {
118118
public func disconnect(properties: MQTTProperties = .init()) -> EventLoopFuture<Void> {
119119
return self.client.disconnect(packet: MQTTDisconnectPacket(reason: .success, properties: properties))
120120
}
121-
121+
122122
/// Re-authenticate with server
123123
///
124124
/// - Parameters:
@@ -130,7 +130,7 @@ extension MQTTClient {
130130
authWorkflow: ((MQTTAuthV5, EventLoop) -> EventLoopFuture<MQTTAuthV5>)? = nil
131131
) -> EventLoopFuture<MQTTAuthV5> {
132132
let authPacket = MQTTAuthPacket(reason: .reAuthenticate, properties: properties)
133-
let authFuture = client.reAuth(packet: authPacket)
133+
let authFuture = self.client.reAuth(packet: authPacket)
134134
let eventLoop = authFuture.eventLoop
135135
return authFuture.flatMap { response -> EventLoopFuture<MQTTPacket> in
136136
guard let auth = response as? MQTTAuthPacket else { return eventLoop.makeFailedFuture(MQTTError.unexpectedMessage) }

Sources/MQTTNIO/MQTTConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ final class MQTTConnection {
118118
bootstrap = NIOClientTCPBootstrap(clientBootstrap, tls: tlsProvider)
119119
return bootstrap.enableTLS()
120120
} else {
121-
bootstrap = NIOClientTCPBootstrap(clientBootstrap,tls: NIOInsecureNoTLS())
121+
bootstrap = NIOClientTCPBootstrap(clientBootstrap, tls: NIOInsecureNoTLS())
122122
}
123123
return bootstrap
124124
}
@@ -181,7 +181,7 @@ final class MQTTConnection {
181181
let task = MQTTTask(on: channel.eventLoop, timeout: self.timeout, checkInbound: checkInbound)
182182
let taskHandler = MQTTTaskHandler(task: task, channel: channel)
183183

184-
channel.pipeline.addHandler(taskHandler, position: .before(unhandledHandler))
184+
channel.pipeline.addHandler(taskHandler, position: .before(self.unhandledHandler))
185185
.flatMap {
186186
self.channel.writeAndFlush(message)
187187
}

Sources/MQTTNIO/MQTTPacket.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ struct MQTTPubAckPacket: MQTTPacket {
373373
writeFixedHeader(packetType: self.type, size: self.packetSize(version: version), to: &byteBuffer)
374374
byteBuffer.writeInteger(self.packetId)
375375
if version == .v5_0,
376-
(self.reason != .success || self.properties.count > 0) {
376+
self.reason != .success || self.properties.count > 0
377+
{
377378
byteBuffer.writeInteger(self.reason.rawValue)
378379
try self.properties.write(to: &byteBuffer)
379380
}
@@ -401,7 +402,8 @@ struct MQTTPubAckPacket: MQTTPacket {
401402

402403
func packetSize(version: MQTTClient.Version) -> Int {
403404
if version == .v5_0,
404-
(self.reason != .success || self.properties.count > 0) {
405+
self.reason != .success || self.properties.count > 0
406+
{
405407
let propertiesPacketSize = self.properties.packetSize
406408
return 3 + MQTTSerializer.variableLengthIntegerPacketSize(propertiesPacketSize) + propertiesPacketSize
407409
}
@@ -500,7 +502,8 @@ struct MQTTDisconnectPacket: MQTTPacket {
500502
func write(version: MQTTClient.Version, to byteBuffer: inout ByteBuffer) throws {
501503
writeFixedHeader(packetType: self.type, size: self.packetSize(version: version), to: &byteBuffer)
502504
if version == .v5_0,
503-
(self.reason != .success || self.properties.count > 0) {
505+
self.reason != .success || self.properties.count > 0
506+
{
504507
byteBuffer.writeInteger(self.reason.rawValue)
505508
try self.properties.write(to: &byteBuffer)
506509
}
@@ -527,7 +530,8 @@ struct MQTTDisconnectPacket: MQTTPacket {
527530

528531
func packetSize(version: MQTTClient.Version) -> Int {
529532
if version == .v5_0,
530-
(self.reason != .success || self.properties.count > 0) {
533+
self.reason != .success || self.properties.count > 0
534+
{
531535
let propertiesPacketSize = self.properties.packetSize
532536
return 1 + MQTTSerializer.variableLengthIntegerPacketSize(propertiesPacketSize) + propertiesPacketSize
533537
}
@@ -596,7 +600,7 @@ struct MQTTAuthPacket: MQTTPacket {
596600
}
597601

598602
var packetSize: Int {
599-
if self.reason == .success && self.properties.count == 0 {
603+
if self.reason == .success, self.properties.count == 0 {
600604
return 0
601605
}
602606
let propertiesPacketSize = self.properties.packetSize

Sources/MQTTNIO/MQTTProperties.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ extension MQTTProperties: ExpressibleByArrayLiteral {
8787

8888
extension MQTTProperties: Collection {
8989
public typealias Index = Array<Property>.Index
90-
public var startIndex: Index { properties.startIndex }
91-
public var endIndex: Index { properties.endIndex }
90+
public var startIndex: Index { self.properties.startIndex }
91+
public var endIndex: Index { self.properties.endIndex }
9292

9393
public subscript(_ index: Index) -> Property {
94-
return properties[index]
94+
return self.properties[index]
9595
}
9696

9797
public func index(after index: Index) -> Index {
98-
return properties.index(after: index)
98+
return self.properties.index(after: index)
9999
}
100100
}
101101

Sources/MQTTNIO/MQTTTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final class MQTTUnhandledPacketHandler: ChannelInboundHandler {
9393

9494
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
9595
// we only send response to v5 server
96-
guard client.configuration.version == .v5_0 else { return }
96+
guard self.client.configuration.version == .v5_0 else { return }
9797
guard let connection = client.connection else { return }
9898
let response = self.unwrapInboundIn(data)
9999
switch response.type {

Tests/MQTTNIOTests/MQTTNIOTests+async.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
#if compiler(>=5.5)
1+
#if compiler(>=5.5) && canImport(_Concurrency)
22

3-
import XCTest
43
import Foundation
54
import Logging
65
import NIO
76
import NIOConcurrencyHelpers
87
import NIOFoundationCompat
98
import NIOHTTP1
9+
import XCTest
1010
#if canImport(NIOSSL)
1111
import NIOSSL
1212
#endif
1313
@testable import MQTTNIO
1414

15-
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
15+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
1616
final class AsyncMQTTNIOTests: XCTestCase {
1717
static let hostname = ProcessInfo.processInfo.environment["MOSQUITTO_SERVER"] ?? "localhost"
1818
static let logger: Logger = {
@@ -47,18 +47,18 @@ final class AsyncMQTTNIOTests: XCTestCase {
4747
}
4848

4949
func testConnect() {
50-
let client = createClient(identifier: "testConnect+async")
51-
XCTRunAsyncAndBlock {
50+
let client = self.createClient(identifier: "testConnect+async")
51+
self.XCTRunAsyncAndBlock {
5252
try await client.connect()
5353
try await client.disconnect()
5454
}
5555
}
5656

5757
func testPublishSubscribe() {
58-
let client = createClient(identifier: "testPublish+async")
59-
let client2 = createClient(identifier: "testPublish+async2")
58+
let client = self.createClient(identifier: "testPublish+async")
59+
let client2 = self.createClient(identifier: "testPublish+async2")
6060
let payloadString = "Hello"
61-
XCTRunAsyncAndBlock {
61+
self.XCTRunAsyncAndBlock {
6262
try await client.connect()
6363
try await client2.connect()
6464
_ = try await client2.subscribe(to: [.init(topicFilter: "TestSubject", qos: .atLeastOnce)])
@@ -89,7 +89,7 @@ final class AsyncMQTTNIOTests: XCTestCase {
8989
configuration: .init(disablePing: true)
9090
)
9191

92-
XCTRunAsyncAndBlock {
92+
self.XCTRunAsyncAndBlock {
9393
try await client.connect()
9494
try await client.ping()
9595
try await client.disconnect()

Tests/MQTTNIOTests/MQTTNIOTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ final class MQTTNIOTests: XCTestCase {
403403
try client.syncShutdownGracefully()
404404
#endif
405405
}
406-
406+
407407
// MARK: Helper variables and functions
408408

409409
func createClient(identifier: String, configuration: MQTTClient.Configuration = .init()) -> MQTTClient {
@@ -474,7 +474,7 @@ final class MQTTNIOTests: XCTestCase {
474474
let rootCertificate = try NIOSSLCertificate.fromPEMFile(MQTTNIOTests.rootPath + "/mosquitto/certs/ca.crt")
475475
let certificate = try NIOSSLCertificate.fromPEMFile(MQTTNIOTests.rootPath + "/mosquitto/certs/client.crt")
476476
let privateKey = try NIOSSLPrivateKey(file: MQTTNIOTests.rootPath + "/mosquitto/certs/client.key", format: .pem)
477-
var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
477+
var tlsConfiguration = TLSConfiguration.makeClientConfiguration()
478478
tlsConfiguration.trustRoots = .certificates(rootCertificate)
479479
tlsConfiguration.certificateChain = certificate.map { .certificate($0) }
480480
tlsConfiguration.privateKey = .privateKey(privateKey)

0 commit comments

Comments
 (0)