FSTDispatchQueueTests.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2018 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 "Firestore/Source/Util/FSTDispatchQueue.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/XCTestCase+Await.h"
  19. // In these generic tests the specific TimerIDs don't matter.
  20. static const FSTTimerID timerID1 = FSTTimerIDListenStreamConnection;
  21. static const FSTTimerID timerID2 = FSTTimerIDListenStreamIdle;
  22. static const FSTTimerID timerID3 = FSTTimerIDWriteStreamConnection;
  23. @interface FSTDispatchQueueTests : XCTestCase
  24. @end
  25. @implementation FSTDispatchQueueTests {
  26. FSTDispatchQueue *_queue;
  27. NSMutableArray *_completedSteps;
  28. NSArray *_expectedSteps;
  29. XCTestExpectation *_expectation;
  30. }
  31. - (void)setUp {
  32. [super setUp];
  33. dispatch_queue_t dispatch_queue =
  34. dispatch_queue_create("FSTDispatchQueueTests", DISPATCH_QUEUE_SERIAL);
  35. _queue = [[FSTDispatchQueue alloc] initWithQueue:dispatch_queue];
  36. _completedSteps = [NSMutableArray array];
  37. _expectedSteps = nil;
  38. }
  39. /**
  40. * Helper to return a block that adds @(n) to _completedSteps when run and fulfils _expectation if
  41. * the _completedSteps match the _expectedSteps.
  42. */
  43. - (void (^)())blockForStep:(int)n {
  44. return ^void() {
  45. [self->_completedSteps addObject:@(n)];
  46. if (self->_expectedSteps && self->_completedSteps.count >= self->_expectedSteps.count) {
  47. XCTAssertEqualObjects(self->_completedSteps, self->_expectedSteps);
  48. [self->_expectation fulfill];
  49. }
  50. };
  51. }
  52. - (void)testCanScheduleCallbacksInTheFuture {
  53. _expectation = [self expectationWithDescription:@"Expected steps"];
  54. _expectedSteps = @[ @1, @2, @3, @4 ];
  55. [_queue dispatchAsync:[self blockForStep:1]];
  56. [_queue dispatchAfterDelay:0.005 timerID:timerID1 block:[self blockForStep:4]];
  57. [_queue dispatchAfterDelay:0.001 timerID:timerID2 block:[self blockForStep:3]];
  58. [_queue dispatchAsync:[self blockForStep:2]];
  59. [self awaitExpectations];
  60. }
  61. - (void)testCanCancelDelayedCallbacks {
  62. _expectation = [self expectationWithDescription:@"Expected steps"];
  63. _expectedSteps = @[ @1, @3 ];
  64. // Queue everything from the queue to ensure nothing completes before we cancel.
  65. [_queue dispatchAsync:^{
  66. [_queue dispatchAsyncAllowingSameQueue:[self blockForStep:1]];
  67. FSTDelayedCallback *step2Timer =
  68. [_queue dispatchAfterDelay:.001 timerID:timerID1 block:[self blockForStep:2]];
  69. [_queue dispatchAfterDelay:.005 timerID:timerID2 block:[self blockForStep:3]];
  70. XCTAssertTrue([_queue containsDelayedCallbackWithTimerID:timerID1]);
  71. [step2Timer cancel];
  72. XCTAssertFalse([_queue containsDelayedCallbackWithTimerID:timerID1]);
  73. }];
  74. [self awaitExpectations];
  75. }
  76. - (void)testCanManuallyDrainAllDelayedCallbacksForTesting {
  77. [_queue dispatchAsync:[self blockForStep:1]];
  78. [_queue dispatchAfterDelay:20 timerID:timerID1 block:[self blockForStep:4]];
  79. [_queue dispatchAfterDelay:10 timerID:timerID2 block:[self blockForStep:3]];
  80. [_queue dispatchAsync:[self blockForStep:2]];
  81. [_queue runDelayedCallbacksUntil:FSTTimerIDAll];
  82. XCTAssertEqualObjects(_completedSteps, (@[ @1, @2, @3, @4 ]));
  83. }
  84. - (void)testCanManuallyDrainSpecificDelayedCallbacksForTesting {
  85. [_queue dispatchAsync:[self blockForStep:1]];
  86. [_queue dispatchAfterDelay:20 timerID:timerID1 block:[self blockForStep:5]];
  87. [_queue dispatchAfterDelay:10 timerID:timerID2 block:[self blockForStep:3]];
  88. [_queue dispatchAfterDelay:15 timerID:timerID3 block:[self blockForStep:4]];
  89. [_queue dispatchAsync:[self blockForStep:2]];
  90. [_queue runDelayedCallbacksUntil:timerID3];
  91. XCTAssertEqualObjects(_completedSteps, (@[ @1, @2, @3, @4 ]));
  92. }
  93. @end