FIRInstallationsSingleOperationPromiseCacheTests.m 2.8 KB

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