FSTEventAccumulator.mm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/FIRDocumentSnapshot.h"
  20. #import "Firestore/Source/Public/FIRQuerySnapshot.h"
  21. #import "Firestore/Source/Public/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. }
  33. + (instancetype)accumulatorForTest:(XCTestCase *)testCase {
  34. return [[FSTEventAccumulator alloc] initForTest:testCase];
  35. }
  36. - (instancetype)initForTest:(XCTestCase *)testCase {
  37. if (self = [super init]) {
  38. _testCase = testCase;
  39. _events = [NSMutableArray array];
  40. }
  41. return self;
  42. }
  43. - (NSArray<id> *)awaitEvents:(NSUInteger)events name:(NSString *)name {
  44. @synchronized(self) {
  45. HARD_ASSERT(!self.expectation, "Existing expectation still pending?");
  46. self.expectation = [self.testCase expectationWithDescription:name];
  47. self.maxEvents = self.maxEvents + events;
  48. [self checkFulfilled];
  49. }
  50. // Don't await within @synchronized block to avoid deadlocking.
  51. [self.testCase awaitExpectations];
  52. return [_events subarrayWithRange:NSMakeRange(self.maxEvents - events, events)];
  53. }
  54. - (id)awaitEventWithName:(NSString *)name {
  55. NSArray<id> *events = [self awaitEvents:1 name:name];
  56. return events[0];
  57. }
  58. - (id)awaitLocalEvent {
  59. id event;
  60. do {
  61. event = [self awaitEventWithName:@"Local Event"];
  62. } while (![self hasPendingWrites:event]);
  63. return event;
  64. }
  65. - (id)awaitRemoteEvent {
  66. id event;
  67. do {
  68. event = [self awaitEventWithName:@"Remote Event"];
  69. } while ([self hasPendingWrites:event]);
  70. return event;
  71. }
  72. - (BOOL)hasPendingWrites:(id)event {
  73. if ([event isKindOfClass:[FIRDocumentSnapshot class]]) {
  74. return ((FIRDocumentSnapshot *)event).metadata.hasPendingWrites;
  75. } else {
  76. HARD_ASSERT([event isKindOfClass:[FIRQuerySnapshot class]], "Unexpected event: %s", event);
  77. return ((FIRQuerySnapshot *)event).metadata.hasPendingWrites;
  78. }
  79. }
  80. - (void (^)(id _Nullable, NSError *_Nullable))valueEventHandler {
  81. return ^void(id _Nullable value, NSError *) {
  82. // We can't store nil in the _events array, but these are still interesting to tests so store
  83. // NSNull instead.
  84. id event = value ? value : [NSNull null];
  85. @synchronized(self) {
  86. [self->_events addObject:event];
  87. [self checkFulfilled];
  88. }
  89. };
  90. }
  91. - (void)checkFulfilled {
  92. if (_events.count >= self.maxEvents) {
  93. [self.expectation fulfill];
  94. self.expectation = nil;
  95. }
  96. }
  97. @end
  98. NS_ASSUME_NONNULL_END