FSTEventAccumulator.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/FSTEventAccumulator.h"
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Util/FSTAssert.h"
  19. #import "Firestore/Example/Tests/Util/XCTestCase+Await.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @interface FSTEventAccumulator ()
  22. - (instancetype)initForTest:(XCTestCase *)testCase NS_DESIGNATED_INITIALIZER;
  23. @property(nonatomic, weak, readonly) XCTestCase *testCase;
  24. @property(nonatomic, assign) NSUInteger maxEvents;
  25. @property(nonatomic, strong, nullable) XCTestExpectation *expectation;
  26. @end
  27. @implementation FSTEventAccumulator {
  28. NSMutableArray<id> *_events;
  29. }
  30. + (instancetype)accumulatorForTest:(XCTestCase *)testCase {
  31. return [[FSTEventAccumulator alloc] initForTest:testCase];
  32. }
  33. - (instancetype)initForTest:(XCTestCase *)testCase {
  34. if (self = [super init]) {
  35. _testCase = testCase;
  36. _events = [NSMutableArray array];
  37. }
  38. return self;
  39. }
  40. - (NSArray<id> *)awaitEvents:(NSUInteger)events name:(NSString *)name {
  41. @synchronized(self) {
  42. FSTAssert(!self.expectation, @"Existing expectation still pending?");
  43. self.expectation = [self.testCase expectationWithDescription:name];
  44. self.maxEvents = self.maxEvents + events;
  45. [self checkFulfilled];
  46. }
  47. // Don't await within @synchronized block to avoid deadlocking.
  48. [self.testCase awaitExpectations];
  49. return [_events subarrayWithRange:NSMakeRange(self.maxEvents - events, events)];
  50. }
  51. - (id)awaitEventWithName:(NSString *)name {
  52. NSArray<id> *events = [self awaitEvents:1 name:name];
  53. return events[0];
  54. }
  55. // Overrides the handler property
  56. - (void (^)(id _Nullable, NSError *))handler {
  57. return ^void(id _Nullable value, NSError *error) {
  58. // We can't store nil in the _events array, but these are still interesting to tests so store
  59. // NSNull instead.
  60. id event = value ? value : [NSNull null];
  61. @synchronized(self) {
  62. [_events addObject:event];
  63. [self checkFulfilled];
  64. }
  65. };
  66. }
  67. - (void)checkFulfilled {
  68. if (_events.count >= self.maxEvents) {
  69. [self.expectation fulfill];
  70. self.expectation = nil;
  71. }
  72. }
  73. @end
  74. NS_ASSUME_NONNULL_END