Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions Sources/Nimble/Utils/AsyncAwait.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#if !os(WASI)

#if canImport(CoreFoundation)
import CoreFoundation
#endif

import Dispatch
import Foundation

Expand Down
3 changes: 0 additions & 3 deletions Sources/Nimble/Utils/AsyncTimerSequence.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#if !os(WASI)

#if canImport(CoreFoundation)
import CoreFoundation
#endif
import Dispatch
import Foundation

Expand Down
70 changes: 25 additions & 45 deletions Sources/Nimble/Utils/PollAwait.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#if !os(WASI)

#if canImport(CoreFoundation)
import CoreFoundation
#endif
import Dispatch
import Foundation

Expand Down Expand Up @@ -198,48 +195,24 @@ internal class AwaitPromiseBuilder<T> {
let timedOutSem = DispatchSemaphore(value: 0)
let semTimedOutOrBlocked = DispatchSemaphore(value: 0)
semTimedOutOrBlocked.signal()
#if canImport(CoreFoundation)
let runLoop = CFRunLoopGetMain()
#if canImport(Darwin)
let runLoopMode = CFRunLoopMode.defaultMode.rawValue
#else
let runLoopMode = kCFRunLoopDefaultMode
#endif
CFRunLoopPerformBlock(runLoop, runLoopMode) {
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
timedOutSem.signal()
semTimedOutOrBlocked.signal()
if self.promise.resolveResult(.timedOut) {
CFRunLoopStop(CFRunLoopGetMain())
}
}
}
// potentially interrupt blocking code on run loop to let timeout code run
CFRunLoopStop(runLoop)
#else
let runLoop = RunLoop.main
runLoop.perform(inModes: [.default], block: {
if semTimedOutOrBlocked.wait(timeout: .now()) == .success {
timedOutSem.signal()
semTimedOutOrBlocked.signal()
if self.promise.resolveResult(.timedOut) {
RunLoop.main._stop()
RunLoop.main.stop()
}
}
})
// potentially interrupt blocking code on run loop to let timeout code run
runLoop._stop()
#endif
runLoop.stop()
let now = DispatchTime.now() + forcefullyAbortTimeout.dispatchTimeInterval
let didNotTimeOut = timedOutSem.wait(timeout: now) != .success
let timeoutWasNotTriggered = semTimedOutOrBlocked.wait(timeout: .now()) == .success
if didNotTimeOut && timeoutWasNotTriggered {
if self.promise.resolveResult(.blockedRunLoop) {
#if canImport(CoreFoundation)
CFRunLoopStop(CFRunLoopGetMain())
#else
RunLoop.main._stop()
#endif
runLoop.stop()
}
}
}
Expand Down Expand Up @@ -327,11 +300,7 @@ internal class Awaiter {
if completionCount < 2 {
func completeBlock() {
if promise.resolveResult(.completed(result)) {
#if canImport(CoreFoundation)
CFRunLoopStop(CFRunLoopGetMain())
#else
RunLoop.main._stop()
#endif
RunLoop.main.stop()
}
}

Expand Down Expand Up @@ -369,20 +338,12 @@ internal class Awaiter {
do {
if let result = try closure() {
if promise.resolveResult(.completed(result)) {
#if canImport(CoreFoundation)
CFRunLoopStop(CFRunLoopGetCurrent())
#else
RunLoop.current._stop()
#endif
RunLoop.current.stop()
}
}
} catch let error {
if promise.resolveResult(.errorThrown(error)) {
#if canImport(CoreFoundation)
CFRunLoopStop(CFRunLoopGetCurrent())
#else
RunLoop.current._stop()
#endif
RunLoop.current.stop()
}
}
}
Expand Down Expand Up @@ -416,4 +377,23 @@ internal func pollBlock(
return result
}

#if canImport(CoreFoundation)
import CoreFoundation

extension RunLoop {
func stop() {
CFRunLoopStop(getCFRunLoop())
}
}

#else

extension RunLoop {
func stop() {
_stop()
}
}

#endif

#endif // #if !os(WASI)
3 changes: 0 additions & 3 deletions Tests/NimbleTests/PollingTest+Require.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#if !os(WASI)

import Dispatch
#if canImport(CoreFoundation)
import CoreFoundation
#endif
import Foundation
import XCTest
import Nimble
Expand All @@ -12,7 +9,7 @@
#endif

// swiftlint:disable:next type_body_length
final class PollingRequireTest: XCTestCase {

Check warning on line 12 in Tests/NimbleTests/PollingTest+Require.swift

View workflow job for this annotation

GitHub Actions / lint

Superfluous Disable Command Violation: SwiftLint rule 'type_body_length' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
class Error: Swift.Error {}
let errorToThrow = Error()

Expand Down Expand Up @@ -50,7 +47,7 @@
}

func testPollUnwrapPositiveCase() {
var value: Int? = nil

Check warning on line 50 in Tests/NimbleTests/PollingTest+Require.swift

View workflow job for this annotation

GitHub Actions / lint

Redundant Optional Initialization Violation: Initializing an optional variable with nil is redundant (redundant_optional_initialization)
deferToMainQueue {
value = 1
}
Expand Down Expand Up @@ -249,4 +246,4 @@
}

#endif // #if !os(WASI)

Check warning on line 249 in Tests/NimbleTests/PollingTest+Require.swift

View workflow job for this annotation

GitHub Actions / lint

Trailing Newline Violation: Files should have a single trailing newline (trailing_newline)
Loading