Skip to content

Updating project for strict concurrency and Swift 6 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -4,22 +4,22 @@ on:
jobs:
focal:
container:
image: swift:5.7-focal
image: swiftlang/swift:nightly-6.0-focal
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery
- run: swift test
thread:
container:
image: swift:5.7-focal
image: swiftlang/swift:nightly-6.0-focal
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery --sanitize=thread
- run: swift test --sanitize=thread
address:
container:
image: swift:5.7-focal
image: swiftlang/swift:nightly-6.0-focal
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: ASAN_OPTIONS=detect_leaks=0 swift test --enable-test-discovery --sanitize=address
- run: ASAN_OPTIONS=detect_leaks=0 swift test --sanitize=address
12 changes: 8 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:6.0
import PackageDescription

let package = Package(
@@ -31,13 +31,16 @@ let package = Package(
dependencies: [
.target(name: "APNSCore"),
.target(name: "APNS"),
]),
.product(name: "Logging", package: "swift-log"),
]
),
.testTarget(
name: "APNSTests",
dependencies: [
.target(name: "APNSCore"),
.target(name: "APNS"),
]),
]
),
.target(
name: "APNSCore",
dependencies: [
@@ -70,5 +73,6 @@ let package = Package(
.target(name: "APNSCore"),
]
),
]
],
swiftLanguageVersions: [.v6]
)
22 changes: 3 additions & 19 deletions Sources/APNS/APNSClient.swift
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@

import APNSCore
import AsyncHTTPClient
import Dispatch
import struct Foundation.Date
import struct Foundation.UUID
import NIOConcurrencyHelpers
@@ -114,24 +113,9 @@ public final class APNSClient<Decoder: APNSJSONDecoder, Encoder: APNSJSONEncoder
}
}

/// Shuts down the client and event loop gracefully. This function is clearly an outlier in that it uses a completion
/// callback instead of an EventLoopFuture. The reason for that is that NIO's EventLoopFutures will call back on an event loop.
/// The virtue of this function is to shut the event loop down. To work around that we call back on a DispatchQueue
/// instead.
///
/// - Important: This will only shutdown the event loop if the provider passed to the client was ``createNew``.
/// For shared event loops the owner of the event loop is responsible for handling the lifecycle.
///
/// - Parameters:
/// - queue: The queue on which the callback is invoked on.
/// - callback: The callback that is invoked when everything is shutdown.
@preconcurrency public func shutdown(queue: DispatchQueue = .global(), callback: @Sendable @escaping (Error?) -> Void) {
self.httpClient.shutdown(callback)
}

/// Shuts down the client and `EventLoopGroup` if it was created by the client.
public func syncShutdown() throws {
try self.httpClient.syncShutdown()
/// Shuts down the client gracefully.
public func shutdown() async throws {
try await self.httpClient.shutdown()
}
}

1 change: 1 addition & 0 deletions Sources/APNSCore/APNSClient.swift
Original file line number Diff line number Diff line change
@@ -14,4 +14,5 @@

public protocol APNSClientProtocol {
func send(_ request: APNSRequest<some APNSMessage>) async throws -> APNSResponse
func shutdown() async throws
}
38 changes: 29 additions & 9 deletions Sources/APNSExample/Program.swift
Original file line number Diff line number Diff line change
@@ -14,8 +14,11 @@

import APNSCore
import APNS
import Logging
import Foundation

let logger = Logger(label: "APNSwiftExample")

@available(macOS 11.0, *)
@main
struct Main {
@@ -30,7 +33,9 @@ struct Main {
static let keyIdentifier = ""
static let teamIdentifier = ""


static func main() async throws {

let client = APNSClient(
configuration: .init(
authenticationMethod: .jwt(
@@ -45,15 +50,21 @@ struct Main {
requestEncoder: JSONEncoder()
)

try await Self.sendSimpleAlert(with: client)
try await Self.sendLocalizedAlert(with: client)
try await Self.sendThreadedAlert(with: client)
try await Self.sendCustomCategoryAlert(with: client)
try await Self.sendMutableContentAlert(with: client)
try await Self.sendBackground(with: client)
try await Self.sendVoIP(with: client)
try await Self.sendFileProvider(with: client)
try await Self.sendPushToTalk(with: client)
do {
try await Self.sendSimpleAlert(with: client)
try await Self.sendLocalizedAlert(with: client)
try await Self.sendThreadedAlert(with: client)
try await Self.sendCustomCategoryAlert(with: client)
try await Self.sendMutableContentAlert(with: client)
try await Self.sendBackground(with: client)
try await Self.sendVoIP(with: client)
try await Self.sendFileProvider(with: client)
try await Self.sendPushToTalk(with: client)
} catch {
logger.warning("error sending push: \(error)")
}

try? await client.shutdown()
}
}

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

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

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

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

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

@@ -168,6 +184,7 @@ extension Main {
),
deviceToken: self.deviceToken
)
logger.info("successfully sent background notification")
}
}

@@ -185,6 +202,7 @@ extension Main {
),
deviceToken: self.pushKitDeviceToken
)
logger.info("successfully sent VoIP notification")
}
}

@@ -201,6 +219,7 @@ extension Main {
),
deviceToken: self.fileProviderDeviceToken
)
logger.info("successfully sent FileProvider notification")
}
}

@@ -219,5 +238,6 @@ extension Main {
),
deviceToken: self.ephemeralPushToken
)
logger.info("successfully sent Push to Talk notification")
}
}
5 changes: 5 additions & 0 deletions Sources/APNSURLSession/APNSUrlSessionClient.swift
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ enum APNSUrlSessionClientError: Error {
}

public struct APNSURLSessionClient: APNSClientProtocol {

private let configuration: APNSURLSessionClientConfiguration

let encoder = JSONEncoder()
@@ -61,6 +62,10 @@ public struct APNSURLSessionClient: APNSClientProtocol {
return APNSResponse(apnsID: apnsID, apnsUniqueID: apnsUniqueID)
}
}

public func shutdown() async throws {
// no op
}
}

#endif
4 changes: 2 additions & 2 deletions Tests/APNSTests/APNSClientTests.swift
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ import Crypto
import XCTest

final class APNSClientTests: XCTestCase {
func testShutdown() throws {
func testShutdown() async throws {
let client = self.makeClient()
try client.syncShutdown()
try await client.shutdown()
}

// MARK: - Helper methods