How to use releaseWaitingLock method of AssertionWaitLock class

Best Nimble code snippet using AssertionWaitLock.releaseWaitingLock

Async.swift

Source:Async.swift Github

copy

Full Screen

...12    }13}14internal protocol WaitLock {15    func acquireWaitingLock(fnName: String, file: String, line: UInt)16    func releaseWaitingLock()17    func isWaitingLocked() -> Bool18}19internal class AssertionWaitLock: WaitLock {20    private var currentWaiter: WaitingInfo? = nil21    init() { }22    func acquireWaitingLock(fnName: String, file: String, line: UInt) {23        let info = WaitingInfo(name: fnName, file: file, lineNumber: line)24        nimblePrecondition(25            NSThread.isMainThread(),26            "InvalidNimbleAPIUsage",27            "\(fnName) can only run on the main thread."28        )29        nimblePrecondition(30            currentWaiter == nil,31            "InvalidNimbleAPIUsage",32            "Nested async expectations are not allowed to avoid creating flaky tests.\n\n" +33            "The call to\n\t\(info)\n" +34            "triggered this exception because\n\t\(currentWaiter!)\n" +35            "is currently managing the main run loop."36        )37        currentWaiter = info38    }39    func isWaitingLocked() -> Bool {40        return currentWaiter != nil41    }42    func releaseWaitingLock() {43        currentWaiter = nil44    }45}46internal enum AwaitResult<T> {47    /// Incomplete indicates None (aka - this value hasn't been fulfilled yet)48    case Incomplete49    /// TimedOut indicates the result reached its defined timeout limit before returning50    case TimedOut51    /// BlockedRunLoop indicates the main runloop is too busy processing other blocks to trigger52    /// the timeout code.53    ///54    /// This may also mean the async code waiting upon may have never actually ran within the55    /// required time because other timers & sources are running on the main run loop.56    case BlockedRunLoop57    /// The async block successfully executed and returned a given result58    case Completed(T)59    /// When a Swift Error is thrown60    case ErrorThrown(ErrorType)61    /// When an Objective-C Exception is raised62    case RaisedException(NSException)63    func isIncomplete() -> Bool {64        switch self {65        case .Incomplete: return true66        default: return false67        }68    }69    func isCompleted() -> Bool {70        switch self {71        case .Completed(_): return true72        default: return false73        }74    }75}76/// Holds the resulting value from an asynchronous expectation.77/// This class is thread-safe at receiving an "response" to this promise.78internal class AwaitPromise<T> {79    private(set) internal var asyncResult: AwaitResult<T> = .Incomplete80    private var signal: dispatch_semaphore_t81    init() {82        signal = dispatch_semaphore_create(1)83    }84    /// Resolves the promise with the given result if it has not been resolved. Repeated calls to85    /// this method will resolve in a no-op.86    ///87    /// @returns a Bool that indicates if the async result was accepted or rejected because another88    ///          value was recieved first.89    func resolveResult(result: AwaitResult<T>) -> Bool {90        if dispatch_semaphore_wait(signal, DISPATCH_TIME_NOW) == 0 {91            self.asyncResult = result92            return true93        } else {94            return false95        }96    }97}98internal struct AwaitTrigger {99    let timeoutSource: dispatch_source_t100    let actionSource: dispatch_source_t?101    let start: () throws -> Void102}103/// Factory for building fully configured AwaitPromises and waiting for their results.104///105/// This factory stores all the state for an async expectation so that Await doesn't106/// doesn't have to manage it.107internal class AwaitPromiseBuilder<T> {108    let awaiter: Awaiter109    let waitLock: WaitLock110    let trigger: AwaitTrigger111    let promise: AwaitPromise<T>112    internal init(113        awaiter: Awaiter,114        waitLock: WaitLock,115        promise: AwaitPromise<T>,116        trigger: AwaitTrigger) {117            self.awaiter = awaiter118            self.waitLock = waitLock119            self.promise = promise120            self.trigger = trigger121    }122    func timeout(timeoutInterval: NSTimeInterval, forcefullyAbortTimeout: NSTimeInterval) -> Self {123        // = Discussion =124        //125        // There's a lot of technical decisions here that is useful to elaborate on. This is126        // definitely more lower-level than the previous NSRunLoop based implementation.127        //128        //129        // Why Dispatch Source?130        //131        //132        // We're using a dispatch source to have better control of the run loop behavior.133        // A timer source gives us deferred-timing control without having to rely as much on134        // a run loop's traditional dispatching machinery (eg - NSTimers, DefaultRunLoopMode, etc.)135        // which is ripe for getting corrupted by application code.136        //137        // And unlike dispatch_async(), we can control how likely our code gets prioritized to138        // executed (see leeway parameter) + DISPATCH_TIMER_STRICT.139        //140        // This timer is assumed to run on the HIGH priority queue to ensure it maintains the141        // highest priority over normal application / test code when possible.142        //143        //144        // Run Loop Management145        //146        // In order to properly interrupt the waiting behavior performed by this factory class,147        // this timer stops the main run loop to tell the waiter code that the result should be148        // checked.149        //150        // In addition, stopping the run loop is used to halt code executed on the main run loop.151        dispatch_source_set_timer(152            trigger.timeoutSource,153            dispatch_time(DISPATCH_TIME_NOW, Int64(timeoutInterval * Double(NSEC_PER_SEC))),154            DISPATCH_TIME_FOREVER,155            timeoutLeeway156        )157        dispatch_source_set_event_handler(trigger.timeoutSource) {158            guard self.promise.asyncResult.isIncomplete() else { return }159            let timedOutSem = dispatch_semaphore_create(0)160            let semTimedOutOrBlocked = dispatch_semaphore_create(0)161            dispatch_semaphore_signal(semTimedOutOrBlocked)162            let runLoop = CFRunLoopGetMain()163            CFRunLoopPerformBlock(runLoop, kCFRunLoopDefaultMode) {164                if dispatch_semaphore_wait(semTimedOutOrBlocked, DISPATCH_TIME_NOW) == 0 {165                    dispatch_semaphore_signal(timedOutSem)166                    dispatch_semaphore_signal(semTimedOutOrBlocked)167                    if self.promise.resolveResult(.TimedOut) {168                        CFRunLoopStop(CFRunLoopGetMain())169                    }170                }171            }172            // potentially interrupt blocking code on run loop to let timeout code run173            CFRunLoopStop(runLoop)174            let now = dispatch_time(DISPATCH_TIME_NOW, Int64(forcefullyAbortTimeout * Double(NSEC_PER_SEC)))175            let didNotTimeOut = dispatch_semaphore_wait(timedOutSem, now) != 0176            let timeoutWasNotTriggered = dispatch_semaphore_wait(semTimedOutOrBlocked, 0) == 0177            if didNotTimeOut && timeoutWasNotTriggered {178                if self.promise.resolveResult(.BlockedRunLoop) {179                    CFRunLoopStop(CFRunLoopGetMain())180                }181            }182        }183        return self184    }185    /// Blocks for an asynchronous result.186    ///187    /// @discussion188    /// This function must be executed on the main thread and cannot be nested. This is because189    /// this function (and it's related methods) coordinate through the main run loop. Tampering190    /// with the run loop can cause undesireable behavior.191    ///192    /// This method will return an AwaitResult in the following cases:193    ///194    /// - The main run loop is blocked by other operations and the async expectation cannot be195    ///   be stopped.196    /// - The async expectation timed out197    /// - The async expectation succeeded198    /// - The async expectation raised an unexpected exception (objc)199    /// - The async expectation raised an unexpected error (swift)200    ///201    /// The returned AwaitResult will NEVER be .Incomplete.202    func wait(fnName: String = __FUNCTION__, file: String = __FILE__, line: UInt = __LINE__) -> AwaitResult<T> {203        waitLock.acquireWaitingLock(204            fnName,205            file: file,206            line: line)207        let capture = NMBExceptionCapture(handler: ({ exception in208            self.promise.resolveResult(.RaisedException(exception))209        }), finally: ({210            self.waitLock.releaseWaitingLock()211        }))212        capture.tryBlock {213            do {214                try self.trigger.start()215            } catch let error {216                self.promise.resolveResult(.ErrorThrown(error))217            }218            dispatch_resume(self.trigger.timeoutSource)219            while self.promise.asyncResult.isIncomplete() {220                // Stopping the run loop does not work unless we run only 1 mode221                NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())222            }223            dispatch_suspend(self.trigger.timeoutSource)224            dispatch_source_cancel(self.trigger.timeoutSource)...

Full Screen

Full Screen

releaseWaitingLock

Using AI Code Generation

copy

Full Screen

1AssertionWaitLock.waitingLock.releaseWaitingLock()2AssertionWaitLock.waitingLock.releaseWaitingLock()3AssertionWaitLock.waitingLock.releaseWaitingLock()4AssertionWaitLock.waitingLock.releaseWaitingLock()5AssertionWaitLock.waitingLock.releaseWaitingLock()6AssertionWaitLock.waitingLock.releaseWaitingLock()7AssertionWaitLock.waitingLock.releaseWaitingLock()8AssertionWaitLock.waitingLock.releaseWaitingLock()9AssertionWaitLock.waitingLock.releaseWaitingLock()10AssertionWaitLock.waitingLock.releaseWaitingLock()11AssertionWaitLock.waitingLock.releaseWaitingLock()12AssertionWaitLock.waitingLock.releaseWaitingLock()13AssertionWaitLock.waitingLock.releaseWaitingLock()14AssertionWaitLock.waitingLock.releaseWaitingLock()15AssertionWaitLock.waitingLock.releaseWaitingLock()16AssertionWaitLock.waitingLock.releaseWaitingLock()

Full Screen

Full Screen

releaseWaitingLock

Using AI Code Generation

copy

Full Screen

1let lock = AssertionWaitLock()AssertionWaitLock.waitingLock.releaseWaitingLock()2let lock2 = ock()3lock.lock()4lock2.lock()5lreleaseWaitingLock(lock2)6lock2.unlock()7lock.unlock()8let lock = AssertionWaitLock()9let lock2 = AssertionWaitLock()10lock.lock()11lock2.lock()12lock.releaseWaitingLock(lock2)13lock2.unlock()14lock.unlock()15let lock = AssertionWaitLock()16let lock2 = AssertionWaitLock()17lock.lock()18lock2.lock()19lock.releaseWaitingLock(lock2)20lock2.unlock()21lock.unlock()22let lock = AssertionWaitLock()23let lock2 = AssertionWaitLock()24lock.lock()25lock2.lock()26lock.releaseWaitingLock(lock2)27lock2.unlock()28lock.unlock()29let lock = AssertionWaitLock()30let lock2 = AssertionWaitLock()31lock.lock()32lock2.lock()33lock.releaseWaitingLock(lock2)34lock2.unlock()35lock.unlock()36let lock = AssertionWaitLock()37let lock2 = AssertionWaitLock()38lock.lock()39lock2.lock()40lock.releaseWaitingLock(lock2)41lock2.unlock()42lock.unlock()43let lock = AssertionWaitLock()44let lock2 = AssertionWaitLock()45lock.lock()46lock2.lock()47lock.releaseWaitingLock(lock2)48lock2.unlock()49lock.unlock()50let lock = AssertionWaitLock()51let lock2 = AssertionWaitLock()52lock.lock()53lock2.lock()54lock.releaseWaitingLock(lock2)55lock2.unlock()56lock.unlock()57let lock = AssertionWaitLock()58let lock2 = AssertionWaitLock()59lock.lock()60lock2.lock()61lock.releaseWaitingLock(lock2)62lock2.unlock()63lock.unlock()

Full Screen

Full Screen

releaseWaitingLock

Using AI Code Generation

copy

Full Screen

1import Foundation2let assertionWaitLock = AssertionWaitLock()3assertionWaitLock.releaseWaitingLock()4AssertionWaitLock.waitingLock.releaseWaitingLock()5AssertionWaitLock.waitingLock.releaseWaitingLock()6AssertionWaitLock.waitingLock.releaseWaitingLock()7AssertionWaitLock.waitingLock.releaseWaitingLock()8AssertionWaitLock.waitingLock.releaseWaitingLock()9AssertionWaitLock.waitingLock.releaseWaitingLock()10AssertionWaitLock.waitingLock.releaseWaitingLock()11AssertionWaitLock.waitingLock.releaseWaitingLock()12AssertionWaitLock.waitingLock.releaseWaitingLock()13AssertionWaitLock.waitingLock.releaseWaitingLock()14AssertionWaitLock.waitingLock.releaseWaitingLock()

Full Screen

Full Screen

releaseWaitingLock

Using AI Code Generation

copy

Full Screen

1import Foundation2let lock = AssertionWaitLock()3let thread = Thread {4    print("Thread started")5    lock.releaseWaitingLock()6    print("Thread finished")7}8thread.start()9lock.wait()10print("Main thread finished")11import Foundation12let lock = AssertionWaitLock()13let thread = Thread {14    print("Thread started")15    lock.releaseWaitingLock()16    print("Thread finished")17}18thread.start()19lock.wait()20print("Main thread finished")21import Foundation22let lock = AssertionWaitLock()23let thread = Thread {24    print("Thread started")25    lock.releaseWaitingLock()26    print("Thread finished")27}28thread.start()29lock.wait()30print("Main thread finished")31import Foundation32let lock = AssertionWaitLock()33let thread = Thread {34    print("Thread started")35    lock.releaseWaitingLock()36    print("Thread finished")37}38thread.start()39lock.wait()40print("Main thread finished")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Nimble automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in AssertionWaitLock

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful