How to use acquireWaitingLock method of AssertionWaitLock class

Best Nimble code snippet using AssertionWaitLock.acquireWaitingLock

Async.swift

Source:Async.swift Github

copy

Full Screen

...11 return "\(name) at \(file):\(lineNumber)"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 }...

Full Screen

Full Screen

acquireWaitingLock

Using AI Code Generation

copy

Full Screen

1let lock = AssertionWaitLock()2lock.acquireWaitingLock()3lock.releaseWaitingLock()4let lock = AssertionWaitLock()5lock.acquireWaitingLock()6lock.releaseWaitingLock()7let lock = AssertionWaitLock()8lock.acquireWaitingLock()9lock.releaseWaitingLock()

Full Screen

Full Screen

acquireWaitingLock

Using AI Code Generation

copy

Full Screen

1import Foundation2let lock = AssertionWaitLock()3lock.acquireWaitingLock()4import Foundation5let lock = AssertionWaitLock()6lock.releaseWaitingLock()7import Foundation8let lock = AssertionWaitLock()9lock.acquireWaitingLock()10import Foundation11let lock = AssertionWaitLock()12lock.releaseWaitingLock()13import Foundation14let lock = AssertionWaitLock()15lock.acquireWaitingLock()16import Foundation17let lock = AssertionWaitLock()18lock.releaseWaitingLock()19import Foundation20let lock = AssertionWaitLock()21lock.acquireWaitingLock()22import Foundation23let lock = AssertionWaitLock()24lock.releaseWaitingLock()25import Foundation26let lock = AssertionWaitLock()27lock.acquireWaitingLock()28import Foundation29let lock = AssertionWaitLock()30lock.releaseWaitingLock()31import Foundation32let lock = AssertionWaitLock()33lock.acquireWaitingLock()34import Foundation35let lock = AssertionWaitLock()36lock.releaseWaitingLock()37import Foundation38let lock = AssertionWaitLock()39lock.acquireWaitingLock()40import Foundation41let lock = AssertionWaitLock()42lock.releaseWaitingLock()

Full Screen

Full Screen

acquireWaitingLock

Using AI Code Generation

copy

Full Screen

1import Foundation2class AssertionWaitLock {3 let lock = NSCondition()4 func acquireWaitingLock() {5 lock.lock()6 while isLocked {7 lock.wait()8 }9 lock.unlock()10 }11}12import Foundation13class AssertionWaitLock {14 let lock = NSCondition()15 func acquireWaitingLock() {16 lock.lock()17 while isLocked {18 lock.wait()19 }20 lock.unlock()21 }22}23import Foundation24class AssertionWaitLock {25 let lock = NSCondition()26 func acquireWaitingLock() {27 lock.lock()28 while isLocked {29 lock.wait()30 }31 lock.unlock()32 }33}

Full Screen

Full Screen

acquireWaitingLock

Using AI Code Generation

copy

Full Screen

1func test() {2 let lock = AssertionWaitLock()3 lock.acquireWaitingLock()4 lock.release()5}6func test() {7 let lock = AssertionWaitLock()8 lock.acquireWaitingLock()9 lock.release()10}11func test() {12 let lock = AssertionWaitLock()13 lock.acquireWaitingLock()14 lock.release()15}16func test() {17 let lock = AssertionWaitLock()18 lock.acquireWaitingLock()19 lock.release()20}21func test() {22 let lock = AssertionWaitLock()23 lock.acquireWaitingLock()24 lock.release()25}26func test() {27 let lock = AssertionWaitLock()28 lock.acquireWaitingLock()29 lock.release()30}31func test() {32 let lock = AssertionWaitLock()33 lock.acquireWaitingLock()34 lock.release()35}36func test() {37 let lock = AssertionWaitLock()38 lock.acquireWaitingLock()39 lock.release()40}41func test() {42 let lock = AssertionWaitLock()43 lock.acquireWaitingLock()44 lock.release()45}46func test() {47 let lock = AssertionWaitLock()48 lock.acquireWaitingLock()49 lock.release()50}51func test() {

Full Screen

Full Screen

acquireWaitingLock

Using AI Code Generation

copy

Full Screen

1import Foundation2let lock = AssertionWaitLock()3lock.acquireWaitingLock()4print("Lock acquired")5lock.releaseWaitingLock()6print("Lock released")

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