FSTEventAccumulator.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Example/Tests/Util/XCTestCase+Await.h"
  19. #import "Firestore/Source/Public/FirebaseFirestore/FIRDocumentSnapshot.h"
  20. #import "Firestore/Source/Public/FirebaseFirestore/FIRQuerySnapshot.h"
  21. #import "Firestore/Source/Public/FirebaseFirestore/FIRSnapshotMetadata.h"
  22. #include "Firestore/core/src/util/hard_assert.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FSTEventAccumulator ()
  25. - (instancetype)initForTest:(XCTestCase *)testCase NS_DESIGNATED_INITIALIZER;
  26. @property(nonatomic, weak, readonly) XCTestCase *testCase;
  27. @property(nonatomic, assign) NSUInteger maxEvents;
  28. @property(nonatomic, strong, nullable) XCTestExpectation *expectation;
  29. @end
  30. @implementation FSTEventAccumulator {
  31. NSMutableArray<id> *_events;
  32. bool _rejectAdditionalEvents;
  33. }
  34. + (instancetype)accumulatorForTest:(XCTestCase *)testCase {
  35. return [[FSTEventAccumulator alloc] initForTest:testCase];
  36. }
  37. - (instancetype)initForTest:(XCTestCase *)testCase {
  38. if (self = [super init]) {
  39. _testCase = testCase;
  40. _events = [NSMutableArray array];
  41. _rejectAdditionalEvents = false;
  42. }
  43. return self;
  44. }
  45. - (NSArray<id> *)awaitEvents:(NSUInteger)events name:(NSString *)name {
  46. @synchronized(self) {
  47. HARD_ASSERT(!self.expectation, "Existing expectation still pending?");
  48. self.expectation = [self.testCase expectationWithDescription:name];
  49. self.maxEvents = self.maxEvents + events;
  50. [self checkFulfilled];
  51. }
  52. // Don't await within @synchronized block to avoid deadlocking.
  53. [self.testCase awaitExpectations];
  54. return [_events subarrayWithRange:NSMakeRange(self.maxEvents - events, events)];
  55. }
  56. - (id)awaitEventWithName:(NSString *)name {
  57. NSArray<id> *events = [self awaitEvents:1 name:name];
  58. return events[0];
  59. }
  60. - (id)awaitLocalEvent {
  61. id event;
  62. do {
  63. event = [self awaitEventWithName:@"Local Event"];
  64. } while (![self hasPendingWrites:event]);
  65. return event;
  66. }
  67. - (id)awaitRemoteEvent {
  68. id event;
  69. do {
  70. event = [self awaitEventWithName:@"Remote Event"];
  71. } while ([self hasPendingWrites:event]);
  72. return event;
  73. }
  74. - (BOOL)hasPendingWrites:(id)event {
  75. if ([event isKindOfClass:[FIRDocumentSnapshot class]]) {
  76. return ((FIRDocumentSnapshot *)event).metadata.hasPendingWrites;
  77. } else {
  78. HARD_ASSERT([event isKindOfClass:[FIRQuerySnapshot class]], "Unexpected event: %s", event);
  79. return ((FIRQuerySnapshot *)event).metadata.hasPendingWrites;
  80. }
  81. }
  82. - (void (^)(id _Nullable, NSError *_Nullable))valueEventHandler {
  83. return ^void(id _Nullable value, NSError *) {
  84. // We can't store nil in the _events array, but these are still interesting to tests so store
  85. // NSNull instead.
  86. id event = value ? value : [NSNull null];
  87. @synchronized(self) {
  88. HARD_ASSERT(!self->_rejectAdditionalEvents, "Unexpected event: %s", event);
  89. [self->_events addObject:event];
  90. [self checkFulfilled];
  91. }
  92. };
  93. }
  94. - (void)assertNoAdditionalEvents {
  95. @synchronized(self) {
  96. _rejectAdditionalEvents = true;
  97. }
  98. }
  99. - (void)checkFulfilled {
  100. if (_events.count >= self.maxEvents) {
  101. [self.expectation fulfill];
  102. self.expectation = nil;
  103. }
  104. }
  105. @end
  106. NS_ASSUME_NONNULL_END