FSTTestDispatchQueue.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "Firestore/Example/Tests/Util/FSTTestDispatchQueue.h"
  17. #import <XCTest/XCTestExpectation.h>
  18. #import "Firestore/Source/Util/FSTAssert.h"
  19. @interface FSTTestDispatchQueue ()
  20. @property(nonatomic, weak) XCTestExpectation* expectation;
  21. @end
  22. @implementation FSTTestDispatchQueue
  23. /** The delay used by the idle timeout */
  24. static const NSTimeInterval kIdleDispatchDelay = 60.0;
  25. /** The maximum delay we use in a test run. */
  26. static const NSTimeInterval kTestDispatchDelay = 1.0;
  27. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue {
  28. return [[FSTTestDispatchQueue alloc] initWithQueue:dispatchQueue];
  29. }
  30. - (instancetype)initWithQueue:(dispatch_queue_t)dispatchQueue {
  31. return (self = [super initWithQueue:dispatchQueue]);
  32. }
  33. - (void)dispatchAfterDelay:(NSTimeInterval)delay block:(void (^)(void))block {
  34. [super dispatchAfterDelay:MIN(delay, kTestDispatchDelay)
  35. block:^() {
  36. block();
  37. if (delay == kIdleDispatchDelay) {
  38. [_expectation fulfill];
  39. _expectation = nil;
  40. }
  41. }];
  42. }
  43. - (void)fulfillOnExecution:(XCTestExpectation*)expectation {
  44. FSTAssert(_expectation == nil, @"Previous expectation still active");
  45. _expectation = expectation;
  46. }
  47. @end