Skip to content
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

Updating project for strict concurrency and Swift 6 #207

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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 and event loop gracefully.
kylebrowning marked this conversation as resolved.
Show resolved Hide resolved
public func shutdown() async throws {
try await self.httpClient.shutdown()
kylebrowning marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
}
39 changes: 30 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")
kylebrowning marked this conversation as resolved.
Show resolved Hide resolved

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

private static let logger = Logger(label: "APNSwiftExample")

static func main() async throws {

let client = APNSClient(
configuration: .init(
authenticationMethod: .jwt(
Expand All @@ -45,15 +51,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 +89,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 +108,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 +128,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 +148,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 +168,7 @@ extension Main {
),
deviceToken: self.deviceToken
)
logger.info("successfully sent mutable content alert")
}
}

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

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

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

Expand All @@ -219,5 +239,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