1 |
efrain |
1 |
/**
|
|
|
2 |
Copyright 2018 Google Inc. All rights reserved.
|
|
|
3 |
|
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
you may not use this file except in compliance with the License.
|
|
|
6 |
You may obtain a copy of the License at:
|
|
|
7 |
|
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
|
|
|
10 |
Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
See the License for the specific language governing permissions and
|
|
|
14 |
limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "FBLPromise+Testing.h"
|
|
|
18 |
|
|
|
19 |
BOOL FBLWaitForPromisesWithTimeout(NSTimeInterval timeout) {
|
|
|
20 |
BOOL isTimedOut = NO;
|
|
|
21 |
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
|
|
|
22 |
static NSTimeInterval const minimalTimeout = 0.01;
|
|
|
23 |
static int64_t const minimalTimeToWait = (int64_t)(minimalTimeout * NSEC_PER_SEC);
|
|
|
24 |
dispatch_time_t waitTime = dispatch_time(DISPATCH_TIME_NOW, minimalTimeToWait);
|
|
|
25 |
dispatch_group_t dispatchGroup = FBLPromise.dispatchGroup;
|
|
|
26 |
NSRunLoop *runLoop = NSRunLoop.currentRunLoop;
|
|
|
27 |
while (dispatch_group_wait(dispatchGroup, waitTime)) {
|
|
|
28 |
isTimedOut = timeoutDate.timeIntervalSinceNow < 0.0;
|
|
|
29 |
if (isTimedOut) {
|
|
|
30 |
break;
|
|
|
31 |
}
|
|
|
32 |
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:minimalTimeout]];
|
|
|
33 |
}
|
|
|
34 |
return !isTimedOut;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
@implementation FBLPromise (TestingAdditions)
|
|
|
38 |
|
|
|
39 |
// These properties are implemented in the FBLPromise class itself.
|
|
|
40 |
@dynamic isPending;
|
|
|
41 |
@dynamic isFulfilled;
|
|
|
42 |
@dynamic isRejected;
|
|
|
43 |
@dynamic value;
|
|
|
44 |
@dynamic error;
|
|
|
45 |
|
|
|
46 |
+ (dispatch_group_t)dispatchGroup {
|
|
|
47 |
static dispatch_group_t gDispatchGroup;
|
|
|
48 |
static dispatch_once_t onceToken;
|
|
|
49 |
dispatch_once(&onceToken, ^{
|
|
|
50 |
gDispatchGroup = dispatch_group_create();
|
|
|
51 |
});
|
|
|
52 |
return gDispatchGroup;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
@end
|