|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 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 | + |
| 17 | +#import "Firestore/Example/Tests/Util/FSTTestingHooks.h" |
| 18 | + |
| 19 | +#import "FIRDocumentReference.h" |
| 20 | + |
| 21 | +#include <memory> |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +#include "Firestore/core/src/api/listener_registration.h" |
| 26 | +#include "Firestore/core/src/remote/bloom_filter.h" |
| 27 | +#include "Firestore/core/src/util/defer.h" |
| 28 | +#include "Firestore/core/src/util/string_apple.h" |
| 29 | +#include "Firestore/core/src/util/string_format.h" |
| 30 | +#include "Firestore/core/src/util/testing_hooks.h" |
| 31 | +#include "Firestore/core/test/unit/testutil/async_testing.h" |
| 32 | +#include "absl/types/optional.h" |
| 33 | + |
| 34 | +using firebase::firestore::api::ListenerRegistration; |
| 35 | +using firebase::firestore::remote::BloomFilter; |
| 36 | +using firebase::firestore::testutil::AsyncAccumulator; |
| 37 | +using firebase::firestore::util::Defer; |
| 38 | +using firebase::firestore::util::MakeStringView; |
| 39 | +using firebase::firestore::util::StringFormat; |
| 40 | +using firebase::firestore::util::TestingHooks; |
| 41 | + |
| 42 | +#pragma mark - FSTTestingHooksBloomFilter extension |
| 43 | + |
| 44 | +// Add private "init" methods to FSTTestingHooksBloomFilter. |
| 45 | +@interface FSTTestingHooksBloomFilter () |
| 46 | + |
| 47 | +- (instancetype)initWithApplied:(BOOL)applied |
| 48 | + hashCount:(int)hashCount |
| 49 | + bitmapLength:(int)bitmapLength |
| 50 | + padding:(int)padding NS_DESIGNATED_INITIALIZER; |
| 51 | + |
| 52 | +- (instancetype)initWithBloomFilterInfo:(const TestingHooks::BloomFilterInfo&)bloomFilterInfo; |
| 53 | + |
| 54 | +@end |
| 55 | + |
| 56 | +#pragma mark - FSTTestingHooksBloomFilter implementation |
| 57 | + |
| 58 | +@implementation FSTTestingHooksBloomFilter |
| 59 | + |
| 60 | +- (instancetype)initWithApplied:(BOOL)applied |
| 61 | + hashCount:(int)hashCount |
| 62 | + bitmapLength:(int)bitmapLength |
| 63 | + padding:(int)padding { |
| 64 | + if (self = [super init]) { |
| 65 | + _applied = applied; |
| 66 | + _hashCount = hashCount; |
| 67 | + _bitmapLength = bitmapLength; |
| 68 | + _padding = padding; |
| 69 | + } |
| 70 | + return self; |
| 71 | +} |
| 72 | + |
| 73 | +- (instancetype)initWithBloomFilterInfo:(const TestingHooks::BloomFilterInfo&)bloomFilterInfo { |
| 74 | + return [self initWithApplied:bloomFilterInfo.applied |
| 75 | + hashCount:bloomFilterInfo.hash_count |
| 76 | + bitmapLength:bloomFilterInfo.bitmap_length |
| 77 | + padding:bloomFilterInfo.padding]; |
| 78 | +} |
| 79 | + |
| 80 | +@end |
| 81 | + |
| 82 | +#pragma mark - FSTTestingHooksExistenceFilterMismatchInfo extension |
| 83 | + |
| 84 | +// Add private "init" methods to FSTTestingHooksExistenceFilterMismatchInfo. |
| 85 | +@interface FSTTestingHooksExistenceFilterMismatchInfo () |
| 86 | + |
| 87 | +- (instancetype)initWithLocalCacheCount:(int)localCacheCount |
| 88 | + existenceFilterCount:(int)existenceFilterCount |
| 89 | + bloomFilter:(nullable FSTTestingHooksBloomFilter*)bloomFilter |
| 90 | + NS_DESIGNATED_INITIALIZER; |
| 91 | + |
| 92 | +- (instancetype)initWithExistenceFilterMismatchInfo: |
| 93 | + (const TestingHooks::ExistenceFilterMismatchInfo&)existenceFilterMismatchInfo; |
| 94 | + |
| 95 | +@end |
| 96 | + |
| 97 | +#pragma mark - FSTTestingHooksExistenceFilterMismatchInfo implementation |
| 98 | + |
| 99 | +@implementation FSTTestingHooksExistenceFilterMismatchInfo |
| 100 | + |
| 101 | +- (instancetype)initWithLocalCacheCount:(int)localCacheCount |
| 102 | + existenceFilterCount:(int)existenceFilterCount |
| 103 | + bloomFilter:(nullable FSTTestingHooksBloomFilter*)bloomFilter { |
| 104 | + if (self = [super init]) { |
| 105 | + _localCacheCount = localCacheCount; |
| 106 | + _existenceFilterCount = existenceFilterCount; |
| 107 | + _bloomFilter = bloomFilter; |
| 108 | + } |
| 109 | + return self; |
| 110 | +} |
| 111 | + |
| 112 | +- (instancetype)initWithExistenceFilterMismatchInfo: |
| 113 | + (const TestingHooks::ExistenceFilterMismatchInfo&)existenceFilterMismatchInfo { |
| 114 | + FSTTestingHooksBloomFilter* bloomFilter; |
| 115 | + if (existenceFilterMismatchInfo.bloom_filter.has_value()) { |
| 116 | + bloomFilter = [[FSTTestingHooksBloomFilter alloc] |
| 117 | + initWithBloomFilterInfo:existenceFilterMismatchInfo.bloom_filter.value()]; |
| 118 | + } else { |
| 119 | + bloomFilter = nil; |
| 120 | + } |
| 121 | + |
| 122 | + return [self initWithLocalCacheCount:existenceFilterMismatchInfo.local_cache_count |
| 123 | + existenceFilterCount:existenceFilterMismatchInfo.existence_filter_count |
| 124 | + bloomFilter:bloomFilter]; |
| 125 | +} |
| 126 | + |
| 127 | +@end |
| 128 | + |
| 129 | +#pragma mark - FSTTestingHooks implementation |
| 130 | + |
| 131 | +@implementation FSTTestingHooks |
| 132 | + |
| 133 | ++ (NSArray<FSTTestingHooksExistenceFilterMismatchInfo*>*) |
| 134 | + captureExistenceFilterMismatchesDuringBlock:(void (^)())block { |
| 135 | + auto accumulator = AsyncAccumulator<TestingHooks::ExistenceFilterMismatchInfo>::NewInstance(); |
| 136 | + |
| 137 | + TestingHooks& testing_hooks = TestingHooks::GetInstance(); |
| 138 | + std::shared_ptr<ListenerRegistration> registration = |
| 139 | + testing_hooks.OnExistenceFilterMismatch(accumulator->AsCallback()); |
| 140 | + Defer unregister_callback([registration]() { registration->Remove(); }); |
| 141 | + |
| 142 | + block(); |
| 143 | + |
| 144 | + NSMutableArray<FSTTestingHooksExistenceFilterMismatchInfo*>* mismatches = |
| 145 | + [[NSMutableArray alloc] init]; |
| 146 | + while (!accumulator->IsEmpty()) { |
| 147 | + [mismatches addObject:[[FSTTestingHooksExistenceFilterMismatchInfo alloc] |
| 148 | + initWithExistenceFilterMismatchInfo:accumulator->Shift()]]; |
| 149 | + } |
| 150 | + |
| 151 | + return mismatches; |
| 152 | +} |
| 153 | + |
| 154 | +@end |
0 commit comments