FIRAuthSerialTaskQueueTests.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2017 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 "FIRAuthGlobalWorkQueue.h"
  18. #import "FIRAuthSerialTaskQueue.h"
  19. /** @var kTimeout
  20. @brief Time-out in seconds waiting for tasks to be executed.
  21. */
  22. static const NSTimeInterval kTimeout = 1;
  23. /** @class FIRAuthSerialTaskQueueTests
  24. @brief Tests for @c FIRAuthSerialTaskQueue .
  25. */
  26. @interface FIRAuthSerialTaskQueueTests : XCTestCase
  27. @end
  28. @implementation FIRAuthSerialTaskQueueTests
  29. - (void)testExecution {
  30. XCTestExpectation *expectation = [self expectationWithDescription:@"executed"];
  31. FIRAuthSerialTaskQueue *queue = [[FIRAuthSerialTaskQueue alloc] init];
  32. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  33. completionArg();
  34. [expectation fulfill];
  35. }];
  36. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  37. }
  38. - (void)testCompletion {
  39. XCTestExpectation *expectation = [self expectationWithDescription:@"executed"];
  40. FIRAuthSerialTaskQueue *queue = [[FIRAuthSerialTaskQueue alloc] init];
  41. __block FIRAuthSerialTaskCompletionBlock completion = nil;
  42. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  43. completion = completionArg;
  44. [expectation fulfill];
  45. }];
  46. __block XCTestExpectation *nextExpectation = nil;
  47. __block BOOL executed = NO;
  48. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  49. executed = YES;
  50. completionArg();
  51. [nextExpectation fulfill];
  52. }];
  53. // The second task should not be executed until the first is completed.
  54. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  55. XCTAssertNotNil(completion);
  56. XCTAssertFalse(executed);
  57. nextExpectation = [self expectationWithDescription:@"executed next"];
  58. completion();
  59. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  60. XCTAssertTrue(executed);
  61. }
  62. - (void)testTargetQueue {
  63. XCTestExpectation *expectation = [self expectationWithDescription:@"executed"];
  64. FIRAuthSerialTaskQueue *queue = [[FIRAuthSerialTaskQueue alloc] init];
  65. __block BOOL executed = NO;
  66. dispatch_suspend(FIRAuthGlobalWorkQueue());
  67. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  68. executed = YES;
  69. completionArg();
  70. [expectation fulfill];
  71. }];
  72. // The task should not executed until the global work queue is resumed.
  73. usleep(kTimeout * USEC_PER_SEC);
  74. XCTAssertFalse(executed);
  75. dispatch_resume(FIRAuthGlobalWorkQueue());
  76. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  77. }
  78. - (void)testTaskQueueNoAffectTargetQueue {
  79. FIRAuthSerialTaskQueue *queue = [[FIRAuthSerialTaskQueue alloc] init];
  80. __block FIRAuthSerialTaskCompletionBlock completion = nil;
  81. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  82. completion = completionArg;
  83. }];
  84. __block XCTestExpectation *nextExpectation = nil;
  85. __block BOOL executed = NO;
  86. [queue enqueueTask:^(FIRAuthSerialTaskCompletionBlock completionArg) {
  87. executed = YES;
  88. completionArg();
  89. [nextExpectation fulfill];
  90. }];
  91. XCTestExpectation *expectation = [self expectationWithDescription:@"executed"];
  92. dispatch_async(FIRAuthGlobalWorkQueue(), ^{
  93. [expectation fulfill];
  94. });
  95. // The task queue waiting for completion should not affect the global work queue.
  96. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  97. XCTAssertNotNil(completion);
  98. XCTAssertFalse(executed);
  99. nextExpectation = [self expectationWithDescription:@"executed next"];
  100. completion();
  101. [self waitForExpectationsWithTimeout:kTimeout handler:nil];
  102. XCTAssertTrue(executed);
  103. }
  104. @end