|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 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 <XCTest/XCTest.h> |
| 18 | + |
| 19 | +#import "FBLPromise+Testing.h" |
| 20 | +#import "FBLPromises.h" |
| 21 | + |
| 22 | +#import "FIRInstallationsSingleOperationPromiseCache.h" |
| 23 | + |
| 24 | +@interface FIRInstallationsSingleOperationPromiseCacheTests : XCTestCase |
| 25 | + |
| 26 | +@end |
| 27 | + |
| 28 | +@implementation FIRInstallationsSingleOperationPromiseCacheTests |
| 29 | + |
| 30 | +// This test is flaky by definition. |
| 31 | +// If this test fails at least once it means there must be a concurrency issue. |
| 32 | +- (void)testRaceCondition { |
| 33 | + for (NSInteger i = 0; i < 100; i++) { |
| 34 | + [self assertRaceConditionWithParallelOperationCount:10000]; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +- (void)assertRaceConditionWithParallelOperationCount:(NSInteger)count { |
| 39 | + FIRInstallationsSingleOperationPromiseCache *promiseCache = |
| 40 | + [[FIRInstallationsSingleOperationPromiseCache alloc] |
| 41 | + initWithNewOperationHandler:^FBLPromise *_Nonnull { |
| 42 | + [NSThread sleepForTimeInterval:0.001]; |
| 43 | + return [[FBLPromise resolvedWith:[[NSObject alloc] init]] delay:0.001]; |
| 44 | + }]; |
| 45 | + |
| 46 | + XCTestExpectation *operationsExpectation = |
| 47 | + [self expectationWithDescription:@"operationsExpectation"]; |
| 48 | + operationsExpectation.expectedFulfillmentCount = count; |
| 49 | + |
| 50 | + for (NSInteger i = 0; i < count; i++) { |
| 51 | + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ |
| 52 | + XCTAssertNoThrow([promiseCache getExistingPendingPromise]); |
| 53 | + }); |
| 54 | + |
| 55 | + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ |
| 56 | + FBLPromise *promise = [promiseCache getExistingPendingOrCreateNewPromise]; |
| 57 | + XCTAssertNotNil(promise); |
| 58 | + [operationsExpectation fulfill]; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + XCTAssert(FBLWaitForPromisesWithTimeout(10)); |
| 63 | + [self waitForExpectations:@[ operationsExpectation ] timeout:10]; |
| 64 | + |
| 65 | + // The resolved promise cleanup may not happen instantly. Wait until it happens. |
| 66 | + FBLPromise *existingPromise = [promiseCache getExistingPendingPromise]; |
| 67 | + if (existingPromise) { |
| 68 | + XCTestExpectation *cleanupExpectation = [self expectationWithDescription:@""]; |
| 69 | + |
| 70 | + [existingPromise then:^id _Nullable(id _Nullable value) { |
| 71 | + [cleanupExpectation fulfill]; |
| 72 | + return nil; |
| 73 | + }]; |
| 74 | + |
| 75 | + [self waitForExpectations:@[ cleanupExpectation ] timeout:0.5]; |
| 76 | + } |
| 77 | + |
| 78 | + // Check if resolved promise cleaned up. |
| 79 | + XCTAssertNil([promiseCache getExistingPendingPromise]); |
| 80 | +} |
| 81 | + |
| 82 | +@end |
0 commit comments