google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 294 forks source link

Way to check for promises left pending? #87

Closed alastaircoote closed 5 years ago

alastaircoote commented 5 years ago

I was following the discussion in #76, which largely makes sense to me - I was using waitForPromises() but have switched to using test expectations instead, which seems to work great (and might be more future-proof, now that XCode has support for running tests concurrently?)

One thing I did like about waitForPromises, though, is that it would tell me if some internal promise has been left pending as a result of my tests. Is there any way for me to get a collection of pending promises to check that none are leftover after testing?

shoumikhin commented 5 years ago

Hi Alastair,

waitForPromises is based on dispatch_group_wait from dispatch_group_t's APIs. Every time a pending promise is created, dispatch_group_enter is invoked. Every time a promise is resolved, there's a matching dispatch_group_leave. Thus, to tell if anything is left pending, you can do something like:

Objective-C:

if (dispatch_group_wait(FBLPromise.dispatchGroup, 0) == 0) {
  // There're no pending promises at this moment.
}

Swift:

if DispatchGroup.promises.wait(timeout: DispatchTime(uptimeNanoseconds: 0)) == .success {
  // There're no pending promises at this moment.
}

Thanks.