-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Firestore: Convert testing_hooks_util.h/cc from C++ to Objective-C in…
… FSTTestingHooks.h/mm (#11639)
- Loading branch information
Showing
6 changed files
with
291 additions
and
140 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// NOTE: For Swift compatibility, please keep this header Objective-C only. | ||
// Swift cannot interact with any C++ definitions. | ||
#import <Foundation/Foundation.h> | ||
|
||
@class FIRDocumentReference; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
#pragma mark - FSTTestingHooksBloomFilter | ||
|
||
/** | ||
* Information about the bloom filter provided by Watch in the ExistenceFilter message's | ||
* `unchanged_names` field. | ||
*/ | ||
@interface FSTTestingHooksBloomFilter : NSObject | ||
|
||
- (instancetype)init __attribute__((unavailable("instances cannot be created directly"))); | ||
|
||
/** | ||
* Whether a full requery was averted by using the bloom filter. If false, then something happened, | ||
* such as a false positive, to prevent using the bloom filter to avoid a full requery. | ||
*/ | ||
@property(nonatomic, readonly) BOOL applied; | ||
|
||
/** The number of hash functions used in the bloom filter. */ | ||
@property(nonatomic, readonly) int hashCount; | ||
|
||
/** The number of bytes in the bloom filter's bitmask. */ | ||
@property(nonatomic, readonly) int bitmapLength; | ||
|
||
/** The number of bits of padding in the last byte of the bloom filter. */ | ||
@property(nonatomic, readonly) int padding; | ||
|
||
@end // @interface FSTTestingHooksBloomFilter | ||
|
||
#pragma mark - FSTTestingHooksExistenceFilterMismatchInfo | ||
|
||
/** | ||
* Information about an existence filter mismatch. | ||
*/ | ||
@interface FSTTestingHooksExistenceFilterMismatchInfo : NSObject | ||
|
||
- (instancetype)init __attribute__((unavailable("instances cannot be created directly"))); | ||
|
||
/** The number of documents that matched the query in the local cache. */ | ||
@property(nonatomic, readonly) int localCacheCount; | ||
|
||
/** | ||
* The number of documents that matched the query on the server, as specified in the | ||
* `ExistenceFilter` message's `count` field. | ||
*/ | ||
@property(nonatomic, readonly) int existenceFilterCount; | ||
|
||
/** | ||
* Information about the bloom filter provided by Watch in the ExistenceFilter message's | ||
* `unchanged_names` field. If nil, then that means that Watch did _not_ provide a bloom filter. | ||
*/ | ||
@property(nonatomic, readonly, nullable) FSTTestingHooksBloomFilter* bloomFilter; | ||
|
||
@end | ||
|
||
#pragma mark - FSTTestingHooks | ||
|
||
/** | ||
* Manages "testing hooks", hooks into the internals of the SDK to verify internal state and events | ||
* during integration tests. | ||
*/ | ||
@interface FSTTestingHooks : NSObject | ||
|
||
- (instancetype)init __attribute__((unavailable("instances cannot be created"))); | ||
|
||
/** | ||
* Captures all existence filter mismatches in the Watch 'Listen' stream that occur during the | ||
* execution of the given block. | ||
* | ||
* @param block The block to execute; during the execution of this block all existence filter | ||
* mismatches will be captured. | ||
* | ||
* @return the captured existence filter mismatches. | ||
*/ | ||
+ (NSArray<FSTTestingHooksExistenceFilterMismatchInfo*>*) | ||
captureExistenceFilterMismatchesDuringBlock:(void (^)())block; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "Firestore/Example/Tests/Util/FSTTestingHooks.h" | ||
|
||
#import "FIRDocumentReference.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
|
||
#include "Firestore/core/src/api/listener_registration.h" | ||
#include "Firestore/core/src/remote/bloom_filter.h" | ||
#include "Firestore/core/src/util/defer.h" | ||
#include "Firestore/core/src/util/string_apple.h" | ||
#include "Firestore/core/src/util/string_format.h" | ||
#include "Firestore/core/src/util/testing_hooks.h" | ||
#include "Firestore/core/test/unit/testutil/async_testing.h" | ||
#include "absl/types/optional.h" | ||
|
||
using firebase::firestore::api::ListenerRegistration; | ||
using firebase::firestore::remote::BloomFilter; | ||
using firebase::firestore::testutil::AsyncAccumulator; | ||
using firebase::firestore::util::Defer; | ||
using firebase::firestore::util::MakeStringView; | ||
using firebase::firestore::util::StringFormat; | ||
using firebase::firestore::util::TestingHooks; | ||
|
||
#pragma mark - FSTTestingHooksBloomFilter extension | ||
|
||
// Add private "init" methods to FSTTestingHooksBloomFilter. | ||
@interface FSTTestingHooksBloomFilter () | ||
|
||
- (instancetype)initWithApplied:(BOOL)applied | ||
hashCount:(int)hashCount | ||
bitmapLength:(int)bitmapLength | ||
padding:(int)padding NS_DESIGNATED_INITIALIZER; | ||
|
||
- (instancetype)initWithBloomFilterInfo:(const TestingHooks::BloomFilterInfo&)bloomFilterInfo; | ||
|
||
@end | ||
|
||
#pragma mark - FSTTestingHooksBloomFilter implementation | ||
|
||
@implementation FSTTestingHooksBloomFilter | ||
|
||
- (instancetype)initWithApplied:(BOOL)applied | ||
hashCount:(int)hashCount | ||
bitmapLength:(int)bitmapLength | ||
padding:(int)padding { | ||
if (self = [super init]) { | ||
_applied = applied; | ||
_hashCount = hashCount; | ||
_bitmapLength = bitmapLength; | ||
_padding = padding; | ||
} | ||
return self; | ||
} | ||
|
||
- (instancetype)initWithBloomFilterInfo:(const TestingHooks::BloomFilterInfo&)bloomFilterInfo { | ||
return [self initWithApplied:bloomFilterInfo.applied | ||
hashCount:bloomFilterInfo.hash_count | ||
bitmapLength:bloomFilterInfo.bitmap_length | ||
padding:bloomFilterInfo.padding]; | ||
} | ||
|
||
@end | ||
|
||
#pragma mark - FSTTestingHooksExistenceFilterMismatchInfo extension | ||
|
||
// Add private "init" methods to FSTTestingHooksExistenceFilterMismatchInfo. | ||
@interface FSTTestingHooksExistenceFilterMismatchInfo () | ||
|
||
- (instancetype)initWithLocalCacheCount:(int)localCacheCount | ||
existenceFilterCount:(int)existenceFilterCount | ||
bloomFilter:(nullable FSTTestingHooksBloomFilter*)bloomFilter | ||
NS_DESIGNATED_INITIALIZER; | ||
|
||
- (instancetype)initWithExistenceFilterMismatchInfo: | ||
(const TestingHooks::ExistenceFilterMismatchInfo&)existenceFilterMismatchInfo; | ||
|
||
@end | ||
|
||
#pragma mark - FSTTestingHooksExistenceFilterMismatchInfo implementation | ||
|
||
@implementation FSTTestingHooksExistenceFilterMismatchInfo | ||
|
||
- (instancetype)initWithLocalCacheCount:(int)localCacheCount | ||
existenceFilterCount:(int)existenceFilterCount | ||
bloomFilter:(nullable FSTTestingHooksBloomFilter*)bloomFilter { | ||
if (self = [super init]) { | ||
_localCacheCount = localCacheCount; | ||
_existenceFilterCount = existenceFilterCount; | ||
_bloomFilter = bloomFilter; | ||
} | ||
return self; | ||
} | ||
|
||
- (instancetype)initWithExistenceFilterMismatchInfo: | ||
(const TestingHooks::ExistenceFilterMismatchInfo&)existenceFilterMismatchInfo { | ||
FSTTestingHooksBloomFilter* bloomFilter; | ||
if (existenceFilterMismatchInfo.bloom_filter.has_value()) { | ||
bloomFilter = [[FSTTestingHooksBloomFilter alloc] | ||
initWithBloomFilterInfo:existenceFilterMismatchInfo.bloom_filter.value()]; | ||
} else { | ||
bloomFilter = nil; | ||
} | ||
|
||
return [self initWithLocalCacheCount:existenceFilterMismatchInfo.local_cache_count | ||
existenceFilterCount:existenceFilterMismatchInfo.existence_filter_count | ||
bloomFilter:bloomFilter]; | ||
} | ||
|
||
@end | ||
|
||
#pragma mark - FSTTestingHooks implementation | ||
|
||
@implementation FSTTestingHooks | ||
|
||
+ (NSArray<FSTTestingHooksExistenceFilterMismatchInfo*>*) | ||
captureExistenceFilterMismatchesDuringBlock:(void (^)())block { | ||
auto accumulator = AsyncAccumulator<TestingHooks::ExistenceFilterMismatchInfo>::NewInstance(); | ||
|
||
TestingHooks& testing_hooks = TestingHooks::GetInstance(); | ||
std::shared_ptr<ListenerRegistration> registration = | ||
testing_hooks.OnExistenceFilterMismatch(accumulator->AsCallback()); | ||
Defer unregister_callback([registration]() { registration->Remove(); }); | ||
|
||
block(); | ||
|
||
NSMutableArray<FSTTestingHooksExistenceFilterMismatchInfo*>* mismatches = | ||
[[NSMutableArray alloc] init]; | ||
while (!accumulator->IsEmpty()) { | ||
[mismatches addObject:[[FSTTestingHooksExistenceFilterMismatchInfo alloc] | ||
initWithExistenceFilterMismatchInfo:accumulator->Shift()]]; | ||
} | ||
|
||
return mismatches; | ||
} | ||
|
||
@end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.