From 99c5a3951a937627395f81db9496fde285008081 Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Tue, 18 Jun 2024 16:16:47 -0700 Subject: [PATCH] Mork work on getting the async-version of polling expectations to be compatible with swift 6 --- Sources/Nimble/AsyncExpression.swift | 2 +- Sources/Nimble/Polling+AsyncAwait.swift | 110 ++++++++++++++++++++---- 2 files changed, 94 insertions(+), 18 deletions(-) diff --git a/Sources/Nimble/AsyncExpression.swift b/Sources/Nimble/AsyncExpression.swift index 8ceb2f12..0690c6b7 100644 --- a/Sources/Nimble/AsyncExpression.swift +++ b/Sources/Nimble/AsyncExpression.swift @@ -165,7 +165,7 @@ public struct AsyncExpression { ) } - public func evaluate() async throws -> Value? { + public func evaluate() async throws -> sending Value? { try await self._expression(_withoutCaching) } diff --git a/Sources/Nimble/Polling+AsyncAwait.swift b/Sources/Nimble/Polling+AsyncAwait.swift index cef8669e..8627e3e3 100644 --- a/Sources/Nimble/Polling+AsyncAwait.swift +++ b/Sources/Nimble/Polling+AsyncAwait.swift @@ -4,7 +4,13 @@ import Dispatch @MainActor -internal func execute(_ expression: AsyncExpression, style: ExpectationStyle, to: String, description: String?, matcherExecutor: () async throws -> MatcherResult) async -> (Bool, FailureMessage) { +internal func execute( + _ expression: AsyncExpression, + style: ExpectationStyle, + to: String, + description: String?, + matcherExecutor: @Sendable () async throws -> MatcherResult +) async -> (Bool, FailureMessage) { let msg = FailureMessage() msg.userDescription = description msg.to = to @@ -33,7 +39,7 @@ internal actor Poller { timeout: NimbleTimeInterval, poll: NimbleTimeInterval, fnName: String, - matcherRunner: @escaping () async throws -> MatcherResult) async -> MatcherResult { + matcherRunner: @escaping @Sendable () async throws -> MatcherResult) async -> MatcherResult { let fnName = "expect(...).\(fnName)(...)" let result = await pollBlock( pollInterval: poll, @@ -72,7 +78,7 @@ internal func poll( timeout: NimbleTimeInterval, poll: NimbleTimeInterval, fnName: String, - matcherRunner: @escaping () async throws -> MatcherResult + matcherRunner: @escaping @Sendable () async throws -> MatcherResult ) async -> MatcherResult { let poller = Poller() return await poller.poll( @@ -91,7 +97,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to match by checking continuously /// at each pollInterval until the timeout is reached. @discardableResult - public func toEventually(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventually( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -117,7 +128,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to not match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toEventuallyNot(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventuallyNot( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -145,14 +161,24 @@ extension SyncExpectation { /// /// Alias of toEventuallyNot() @discardableResult - public func toNotEventually(_ matcher: Matcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNotEventually( + _ matcher: Matcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to never match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toNever(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNever( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -179,14 +205,24 @@ extension SyncExpectation { /// /// Alias of toNever() @discardableResult - public func neverTo(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func neverTo( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toNever(matcher, until: until, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to always match by checking /// continusouly at each pollInterval until the timeout is reached @discardableResult - public func toAlways(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toAlways( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -213,7 +249,12 @@ extension SyncExpectation { /// /// Alias of toAlways() @discardableResult - public func alwaysTo(_ matcher: Matcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func alwaysTo( + _ matcher: Matcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description) } @@ -221,7 +262,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to match by checking continuously /// at each pollInterval until the timeout is reached. @discardableResult - public func toEventually(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventually( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -247,7 +293,12 @@ extension SyncExpectation { /// Tests the actual value using a matcher to not match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toEventuallyNot(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toEventuallyNot( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -275,14 +326,24 @@ extension SyncExpectation { /// /// Alias of toEventuallyNot() @discardableResult - public func toNotEventually(_ matcher: AsyncMatcher, timeout: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNotEventually( + _ matcher: AsyncMatcher, + timeout: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toEventuallyNot(matcher, timeout: timeout, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to never match by checking /// continuously at each pollInterval until the timeout is reached. @discardableResult - public func toNever(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toNever( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -309,14 +370,24 @@ extension SyncExpectation { /// /// Alias of toNever() @discardableResult - public func neverTo(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func neverTo( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toNever(matcher, until: until, pollInterval: pollInterval, description: description) } /// Tests the actual value using a matcher to always match by checking /// continusouly at each pollInterval until the timeout is reached @discardableResult - public func toAlways(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func toAlways( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { nimblePrecondition(expression.isClosure, "NimbleInternalError", toEventuallyRequiresClosureError.stringValue) let asyncExpression = expression.toAsyncExpression() @@ -343,7 +414,12 @@ extension SyncExpectation { /// /// Alias of toAlways() @discardableResult - public func alwaysTo(_ matcher: AsyncMatcher, until: NimbleTimeInterval = PollingDefaults.timeout, pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, description: String? = nil) async -> Self { + public func alwaysTo( + _ matcher: AsyncMatcher, + until: NimbleTimeInterval = PollingDefaults.timeout, + pollInterval: NimbleTimeInterval = PollingDefaults.pollInterval, + description: String? = nil + ) async -> Self { return await toAlways(matcher, until: until, pollInterval: pollInterval, description: description) } }