Skip to content

Commit

Permalink
tech: Resolve issues supporting Linux with async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
surpher committed May 13, 2024
1 parent cae0832 commit 173cf71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build

on:
pull_request:
push:
branches:
- 'main'
Expand All @@ -14,6 +15,7 @@ on:
- 'style/**'
- 'test/**'
- 'tech/**'
- 'for-posterity/**'

env:
RUST_TARGET_PATH: pact-reference
Expand All @@ -22,7 +24,7 @@ jobs:
test:
name: Run ${{ matrix.platform }} on ${{ matrix.host }}
runs-on: ${{ matrix.host }}

strategy:
fail-fast: true
matrix:
Expand Down
21 changes: 14 additions & 7 deletions Sources/Extensions/MockServer+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@
import Foundation

#if os(Linux)
import PactSwiftMockServerLinux
import PactSwiftMockServerLinux
#elseif compiler(>=5.5)
@_implementationOnly import PactSwiftMockServer
@_implementationOnly import PactSwiftMockServer
#else
import PactSwiftMockServer
import PactSwiftMockServer
#endif

extension MockServer {


// Handling platform specifics
#if os(Linux)
typealias TransferProtocolScheme = PactSwiftMockServerLinux.TransferProtocol
#else
private var TransferProtocolScheme = PactSwiftMockServer.TransferProtocol
#endif

/// Spins up a mock server with expected interactions defined in the provided Pact
///
/// - Parameters:
Expand All @@ -35,14 +42,14 @@ extension MockServer {
///
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
@discardableResult
func setup(pact: Data, protocol: PactSwiftMockServer.TransferProtocol = .standard) async throws -> Int {
func setup(pact: Data, protocol: TransferProtocolScheme = .standard) async throws -> Int {
try await withCheckedThrowingContinuation { continuation in
self.setup(pact: pact, protocol: `protocol`) { result in
continuation.resume(with: result)
}
}
}

/// Verifies all interactions passed to mock server
///
/// By default pact files are written to `/tmp/pacts`.
Expand All @@ -56,7 +63,7 @@ extension MockServer {
}
}
}

/// Finalises Pact tests by writing the pact contract file to disk
///
/// - Parameters:
Expand Down
6 changes: 5 additions & 1 deletion Sources/Extensions/Task+Timeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension Task where Failure == Error {

// Start a new Task with a timeout. If the timeout expires before the operation is
// completed then the task is cancelled and an error is thrown.
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
Expand All @@ -28,7 +28,11 @@ extension Task where Failure == Error {
try await withThrowingTaskGroup(of: Success.self) { group -> Success in
group.addTask(operation: operation)
group.addTask {
#if os(Linux)
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
#else
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * Double(NSEC_PER_SEC)))
#endif
throw TimeoutError(interval: timeout)
}
guard let success = try await group.next() else {
Expand Down

0 comments on commit 173cf71

Please sign in to comment.