Skip to content

Commit

Permalink
Updating project for strict concurrency and Swift 6 (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebrowning authored Aug 21, 2024
1 parent fe9a00c commit 9b5032e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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
Expand Up @@ -14,7 +14,6 @@

import APNSCore
import AsyncHTTPClient
import Dispatch
import struct Foundation.Date
import struct Foundation.UUID
import NIOConcurrencyHelpers
Expand Down Expand Up @@ -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()
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/APNSCore/APNSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -14,8 +14,11 @@

import APNSCore
import APNS
import Logging
import Foundation

let logger = Logger(label: "APNSwiftExample")

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


static func main() async throws {

let client = APNSClient(
configuration: .init(
authenticationMethod: .jwt(
Expand All @@ -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()
}
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -114,6 +127,7 @@ extension Main {
),
deviceToken: self.deviceToken
)
logger.info("successfully sent threaded alert")
}

static func sendCustomCategoryAlert(with client: some APNSClientProtocol) async throws {
Expand All @@ -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 {
Expand All @@ -152,6 +167,7 @@ extension Main {
),
deviceToken: self.deviceToken
)
logger.info("successfully sent mutable content alert")
}
}

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

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

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

Expand All @@ -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
Expand Up @@ -7,6 +7,7 @@ enum APNSUrlSessionClientError: Error {
}

public struct APNSURLSessionClient: APNSClientProtocol {

private let configuration: APNSURLSessionClientConfiguration

let encoder = JSONEncoder()
Expand Down Expand Up @@ -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
Expand Up @@ -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
Expand Down

0 comments on commit 9b5032e

Please sign in to comment.