Skip to content

Commit 9b5032e

Browse files
authored
Updating project for strict concurrency and Swift 6 (#207)
1 parent fe9a00c commit 9b5032e

File tree

7 files changed

+54
-40
lines changed

7 files changed

+54
-40
lines changed

.github/workflows/swift.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ on:
44
jobs:
55
focal:
66
container:
7-
image: swift:5.7-focal
7+
image: swiftlang/swift:nightly-6.0-focal
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v1
11-
- run: swift test --enable-test-discovery
11+
- run: swift test
1212
thread:
1313
container:
14-
image: swift:5.7-focal
14+
image: swiftlang/swift:nightly-6.0-focal
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v1
18-
- run: swift test --enable-test-discovery --sanitize=thread
18+
- run: swift test --sanitize=thread
1919
address:
2020
container:
21-
image: swift:5.7-focal
21+
image: swiftlang/swift:nightly-6.0-focal
2222
runs-on: ubuntu-latest
2323
steps:
2424
- uses: actions/checkout@v1
25-
- run: ASAN_OPTIONS=detect_leaks=0 swift test --enable-test-discovery --sanitize=address
25+
- run: ASAN_OPTIONS=detect_leaks=0 swift test --sanitize=address

Package.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.7
1+
// swift-tools-version:6.0
22
import PackageDescription
33

44
let package = Package(
@@ -31,13 +31,16 @@ let package = Package(
3131
dependencies: [
3232
.target(name: "APNSCore"),
3333
.target(name: "APNS"),
34-
]),
34+
.product(name: "Logging", package: "swift-log"),
35+
]
36+
),
3537
.testTarget(
3638
name: "APNSTests",
3739
dependencies: [
3840
.target(name: "APNSCore"),
3941
.target(name: "APNS"),
40-
]),
42+
]
43+
),
4144
.target(
4245
name: "APNSCore",
4346
dependencies: [
@@ -70,5 +73,6 @@ let package = Package(
7073
.target(name: "APNSCore"),
7174
]
7275
),
73-
]
76+
],
77+
swiftLanguageVersions: [.v6]
7478
)

Sources/APNS/APNSClient.swift

+3-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import APNSCore
1616
import AsyncHTTPClient
17-
import Dispatch
1817
import struct Foundation.Date
1918
import struct Foundation.UUID
2019
import NIOConcurrencyHelpers
@@ -114,24 +113,9 @@ public final class APNSClient<Decoder: APNSJSONDecoder, Encoder: APNSJSONEncoder
114113
}
115114
}
116115

117-
/// Shuts down the client and event loop gracefully. This function is clearly an outlier in that it uses a completion
118-
/// callback instead of an EventLoopFuture. The reason for that is that NIO's EventLoopFutures will call back on an event loop.
119-
/// The virtue of this function is to shut the event loop down. To work around that we call back on a DispatchQueue
120-
/// instead.
121-
///
122-
/// - Important: This will only shutdown the event loop if the provider passed to the client was ``createNew``.
123-
/// For shared event loops the owner of the event loop is responsible for handling the lifecycle.
124-
///
125-
/// - Parameters:
126-
/// - queue: The queue on which the callback is invoked on.
127-
/// - callback: The callback that is invoked when everything is shutdown.
128-
@preconcurrency public func shutdown(queue: DispatchQueue = .global(), callback: @Sendable @escaping (Error?) -> Void) {
129-
self.httpClient.shutdown(callback)
130-
}
131-
132-
/// Shuts down the client and `EventLoopGroup` if it was created by the client.
133-
public func syncShutdown() throws {
134-
try self.httpClient.syncShutdown()
116+
/// Shuts down the client gracefully.
117+
public func shutdown() async throws {
118+
try await self.httpClient.shutdown()
135119
}
136120
}
137121

Sources/APNSCore/APNSClient.swift

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
public protocol APNSClientProtocol {
1616
func send(_ request: APNSRequest<some APNSMessage>) async throws -> APNSResponse
17+
func shutdown() async throws
1718
}

Sources/APNSExample/Program.swift

+29-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515
import APNSCore
1616
import APNS
17+
import Logging
1718
import Foundation
1819

20+
let logger = Logger(label: "APNSwiftExample")
21+
1922
@available(macOS 11.0, *)
2023
@main
2124
struct Main {
@@ -30,7 +33,9 @@ struct Main {
3033
static let keyIdentifier = ""
3134
static let teamIdentifier = ""
3235

36+
3337
static func main() async throws {
38+
3439
let client = APNSClient(
3540
configuration: .init(
3641
authenticationMethod: .jwt(
@@ -45,15 +50,21 @@ struct Main {
4550
requestEncoder: JSONEncoder()
4651
)
4752

48-
try await Self.sendSimpleAlert(with: client)
49-
try await Self.sendLocalizedAlert(with: client)
50-
try await Self.sendThreadedAlert(with: client)
51-
try await Self.sendCustomCategoryAlert(with: client)
52-
try await Self.sendMutableContentAlert(with: client)
53-
try await Self.sendBackground(with: client)
54-
try await Self.sendVoIP(with: client)
55-
try await Self.sendFileProvider(with: client)
56-
try await Self.sendPushToTalk(with: client)
53+
do {
54+
try await Self.sendSimpleAlert(with: client)
55+
try await Self.sendLocalizedAlert(with: client)
56+
try await Self.sendThreadedAlert(with: client)
57+
try await Self.sendCustomCategoryAlert(with: client)
58+
try await Self.sendMutableContentAlert(with: client)
59+
try await Self.sendBackground(with: client)
60+
try await Self.sendVoIP(with: client)
61+
try await Self.sendFileProvider(with: client)
62+
try await Self.sendPushToTalk(with: client)
63+
} catch {
64+
logger.warning("error sending push: \(error)")
65+
}
66+
67+
try? await client.shutdown()
5768
}
5869
}
5970

@@ -77,6 +88,7 @@ extension Main {
7788
),
7889
deviceToken: self.deviceToken
7990
)
91+
logger.info("successfully sent simple alert notification")
8092
}
8193

8294
static func sendLocalizedAlert(with client: some APNSClientProtocol) async throws {
@@ -95,6 +107,7 @@ extension Main {
95107
),
96108
deviceToken: self.deviceToken
97109
)
110+
logger.info("successfully sent alert localized notification")
98111
}
99112

100113
static func sendThreadedAlert(with client: some APNSClientProtocol) async throws {
@@ -114,6 +127,7 @@ extension Main {
114127
),
115128
deviceToken: self.deviceToken
116129
)
130+
logger.info("successfully sent threaded alert")
117131
}
118132

119133
static func sendCustomCategoryAlert(with client: some APNSClientProtocol) async throws {
@@ -133,6 +147,7 @@ extension Main {
133147
),
134148
deviceToken: self.deviceToken
135149
)
150+
logger.info("successfully sent custom category alert")
136151
}
137152

138153
static func sendMutableContentAlert(with client: some APNSClientProtocol) async throws {
@@ -152,6 +167,7 @@ extension Main {
152167
),
153168
deviceToken: self.deviceToken
154169
)
170+
logger.info("successfully sent mutable content alert")
155171
}
156172
}
157173

@@ -168,6 +184,7 @@ extension Main {
168184
),
169185
deviceToken: self.deviceToken
170186
)
187+
logger.info("successfully sent background notification")
171188
}
172189
}
173190

@@ -185,6 +202,7 @@ extension Main {
185202
),
186203
deviceToken: self.pushKitDeviceToken
187204
)
205+
logger.info("successfully sent VoIP notification")
188206
}
189207
}
190208

@@ -201,6 +219,7 @@ extension Main {
201219
),
202220
deviceToken: self.fileProviderDeviceToken
203221
)
222+
logger.info("successfully sent FileProvider notification")
204223
}
205224
}
206225

@@ -219,5 +238,6 @@ extension Main {
219238
),
220239
deviceToken: self.ephemeralPushToken
221240
)
241+
logger.info("successfully sent Push to Talk notification")
222242
}
223243
}

Sources/APNSURLSession/APNSUrlSessionClient.swift

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum APNSUrlSessionClientError: Error {
77
}
88

99
public struct APNSURLSessionClient: APNSClientProtocol {
10+
1011
private let configuration: APNSURLSessionClientConfiguration
1112

1213
let encoder = JSONEncoder()
@@ -61,6 +62,10 @@ public struct APNSURLSessionClient: APNSClientProtocol {
6162
return APNSResponse(apnsID: apnsID, apnsUniqueID: apnsUniqueID)
6263
}
6364
}
65+
66+
public func shutdown() async throws {
67+
// no op
68+
}
6469
}
6570

6671
#endif

Tests/APNSTests/APNSClientTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import Crypto
1818
import XCTest
1919

2020
final class APNSClientTests: XCTestCase {
21-
func testShutdown() throws {
21+
func testShutdown() async throws {
2222
let client = self.makeClient()
23-
try client.syncShutdown()
23+
try await client.shutdown()
2424
}
2525

2626
// MARK: - Helper methods

0 commit comments

Comments
 (0)