Skip to content

Commit e226001

Browse files
author
Greg Soltis
authored
Port FSTRemoteDocumentCacheTest to use C++ interface (#2194)
1 parent bc0329f commit e226001

9 files changed

+173
-39
lines changed

Firestore/Example/Tests/Local/FSTLevelDBRemoteDocumentCacheTests.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <memory>
1718
#include <string>
1819

1920
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
2021
#import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
2122
#import "Firestore/Source/Local/FSTLevelDB.h"
23+
#include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h"
2224

2325
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
26+
#include "absl/memory/memory.h"
2427
#include "leveldb/db.h"
2528

2629
NS_ASSUME_NONNULL_BEGIN
2730

2831
using leveldb::WriteOptions;
32+
using firebase::firestore::local::LevelDbRemoteDocumentCache;
2933
using firebase::firestore::util::OrderedCode;
3034

3135
// A dummy document value, useful for testing code that's known to examine only document keys.
@@ -41,24 +45,31 @@ @interface FSTLevelDBRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
4145

4246
@implementation FSTLevelDBRemoteDocumentCacheTests {
4347
FSTLevelDB *_db;
48+
std::unique_ptr<LevelDbRemoteDocumentCache> _cache;
4449
}
4550

4651
- (void)setUp {
4752
[super setUp];
4853
_db = [FSTPersistenceTestHelpers levelDBPersistence];
4954
self.persistence = _db;
50-
self.remoteDocumentCache = [self.persistence remoteDocumentCache];
55+
HARD_ASSERT(!_cache, "Previous cache not torn down");
56+
_cache = absl::make_unique<LevelDbRemoteDocumentCache>(_db, _db.serializer);
5157

5258
// Write a couple dummy rows that should appear before/after the remote_documents table to make
5359
// sure the tests are unaffected.
5460
[self writeDummyRowWithSegments:@[ @"remote_documentr", @"foo", @"bar" ]];
5561
[self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]];
5662
}
5763

64+
- (LevelDbRemoteDocumentCache *_Nullable)remoteDocumentCache {
65+
return _cache.get();
66+
}
67+
5868
- (void)tearDown {
5969
[super tearDown];
6070
self.remoteDocumentCache = nil;
6171
self.persistence = nil;
72+
_cache.reset();
6273
_db = nil;
6374
}
6475

Firestore/Example/Tests/Local/FSTMemoryRemoteDocumentCacheTests.mm

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h"
17+
#include <memory>
1818

1919
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
20+
#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h"
21+
#include "absl/memory/memory.h"
2022

2123
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
2224
#import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
2325

26+
using firebase::firestore::local::MemoryRemoteDocumentCache;
27+
2428
@interface FSTMemoryRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
2529
@end
2630

@@ -29,18 +33,25 @@ @interface FSTMemoryRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
2933
* protocol in FSTRemoteDocumentCacheTests. This class is merely responsible for setting up and
3034
* tearing down the @a remoteDocumentCache.
3135
*/
32-
@implementation FSTMemoryRemoteDocumentCacheTests
36+
@implementation FSTMemoryRemoteDocumentCacheTests {
37+
std::unique_ptr<MemoryRemoteDocumentCache> _cache;
38+
}
3339

3440
- (void)setUp {
3541
[super setUp];
3642

3743
self.persistence = [FSTPersistenceTestHelpers eagerGCMemoryPersistence];
38-
self.remoteDocumentCache = [self.persistence remoteDocumentCache];
44+
HARD_ASSERT(!_cache, "Previous cache not torn down");
45+
_cache = absl::make_unique<MemoryRemoteDocumentCache>();
46+
}
47+
48+
- (MemoryRemoteDocumentCache *)remoteDocumentCache {
49+
return _cache.get();
3950
}
4051

4152
- (void)tearDown {
53+
_cache.reset();
4254
self.persistence = nil;
43-
self.remoteDocumentCache = nil;
4455

4556
[super tearDown];
4657
}

Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#import <XCTest/XCTest.h>
2020

21+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
22+
2123
@protocol FSTPersistence;
2224

2325
NS_ASSUME_NONNULL_BEGIN
@@ -32,7 +34,7 @@ NS_ASSUME_NONNULL_BEGIN
3234
* + override -tearDown, cleaning up remoteDocumentCache and persistence
3335
*/
3436
@interface FSTRemoteDocumentCacheTests : XCTestCase
35-
@property(nonatomic, strong, nullable) id<FSTRemoteDocumentCache> remoteDocumentCache;
37+
@property(nonatomic, nullable) firebase::firestore::local::RemoteDocumentCache* remoteDocumentCache;
3638
@property(nonatomic, strong, nullable) id<FSTPersistence> persistence;
3739
@end
3840

Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.mm

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
#import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
1818

19+
#include <memory>
20+
1921
#import "Firestore/Source/Core/FSTQuery.h"
2022
#import "Firestore/Source/Local/FSTPersistence.h"
2123
#import "Firestore/Source/Model/FSTDocument.h"
2224
#import "Firestore/Source/Model/FSTDocumentSet.h"
2325

2426
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2527

28+
#include "Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h"
29+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
2630
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2731
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
2832
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
@@ -32,6 +36,7 @@
3236

3337
namespace testutil = firebase::firestore::testutil;
3438
namespace util = firebase::firestore::util;
39+
using firebase::firestore::local::RemoteDocumentCache;
3540
using firebase::firestore::model::DocumentKey;
3641
using firebase::firestore::model::DocumentKeySet;
3742
using firebase::firestore::model::DocumentMap;
@@ -62,15 +67,15 @@ - (void)testReadDocumentNotInCache {
6267
if (!self.remoteDocumentCache) return;
6368

6469
self.persistence.run("testReadDocumentNotInCache", [&]() {
65-
XCTAssertNil([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)]);
70+
XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
6671
});
6772
}
6873

6974
// Helper for next two tests.
7075
- (void)setAndReadADocumentAtPath:(const absl::string_view)path {
7176
self.persistence.run("setAndReadADocumentAtPath", [&]() {
7277
FSTDocument *written = [self setTestDocumentAtPath:path];
73-
FSTMaybeDocument *read = [self.remoteDocumentCache entryForKey:testutil::Key(path)];
78+
FSTMaybeDocument *read = self.remoteDocumentCache->Get(testutil::Key(path));
7479
XCTAssertEqualObjects(read, written);
7580
});
7681
}
@@ -87,8 +92,8 @@ - (void)testSetAndReadSeveralDocuments {
8792
self.persistence.run("testSetAndReadSeveralDocuments", [=]() {
8893
NSArray<FSTDocument *> *written =
8994
@[ [self setTestDocumentAtPath:kDocPath], [self setTestDocumentAtPath:kLongDocPath] ];
90-
MaybeDocumentMap read = [self.remoteDocumentCache
91-
entriesForKeys:DocumentKeySet{testutil::Key(kDocPath), testutil::Key(kLongDocPath)}];
95+
MaybeDocumentMap read = self.remoteDocumentCache->GetAll(
96+
DocumentKeySet{testutil::Key(kDocPath), testutil::Key(kLongDocPath)});
9297
[self expectMap:read hasDocsInArray:written exactly:YES];
9398
});
9499
}
@@ -99,12 +104,11 @@ - (void)testSetAndReadSeveralDocumentsIncludingMissingDocument {
99104
self.persistence.run("testSetAndReadSeveralDocumentsIncludingMissingDocument", [=]() {
100105
NSArray<FSTDocument *> *written =
101106
@[ [self setTestDocumentAtPath:kDocPath], [self setTestDocumentAtPath:kLongDocPath] ];
102-
MaybeDocumentMap read =
103-
[self.remoteDocumentCache entriesForKeys:DocumentKeySet{
104-
testutil::Key(kDocPath),
105-
testutil::Key(kLongDocPath),
106-
testutil::Key("foo/nonexistent"),
107-
}];
107+
MaybeDocumentMap read = self.remoteDocumentCache->GetAll(DocumentKeySet{
108+
testutil::Key(kDocPath),
109+
testutil::Key(kLongDocPath),
110+
testutil::Key("foo/nonexistent"),
111+
});
108112
[self expectMap:read hasDocsInArray:written exactly:NO];
109113
auto found = read.find(DocumentKey::FromPathString("foo/nonexistent"));
110114
XCTAssertTrue(found != read.end());
@@ -123,10 +127,9 @@ - (void)testSetAndReadDeletedDocument {
123127

124128
self.persistence.run("testSetAndReadDeletedDocument", [&]() {
125129
FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc(kDocPath, kVersion, NO);
126-
[self.remoteDocumentCache addEntry:deletedDoc];
130+
self.remoteDocumentCache->AddEntry(deletedDoc);
127131

128-
XCTAssertEqualObjects([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)],
129-
deletedDoc);
132+
XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), deletedDoc);
130133
});
131134
}
132135

@@ -136,8 +139,8 @@ - (void)testSetDocumentToNewValue {
136139
self.persistence.run("testSetDocumentToNewValue", [&]() {
137140
[self setTestDocumentAtPath:kDocPath];
138141
FSTDocument *newDoc = FSTTestDoc(kDocPath, kVersion, @{@"data" : @2}, FSTDocumentStateSynced);
139-
[self.remoteDocumentCache addEntry:newDoc];
140-
XCTAssertEqualObjects([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)], newDoc);
142+
self.remoteDocumentCache->AddEntry(newDoc);
143+
XCTAssertEqualObjects(self.remoteDocumentCache->Get(testutil::Key(kDocPath)), newDoc);
141144
});
142145
}
143146

@@ -146,9 +149,9 @@ - (void)testRemoveDocument {
146149

147150
self.persistence.run("testRemoveDocument", [&]() {
148151
[self setTestDocumentAtPath:kDocPath];
149-
[self.remoteDocumentCache removeEntryForKey:testutil::Key(kDocPath)];
152+
self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath));
150153

151-
XCTAssertNil([self.remoteDocumentCache entryForKey:testutil::Key(kDocPath)]);
154+
XCTAssertNil(self.remoteDocumentCache->Get(testutil::Key(kDocPath)));
152155
});
153156
}
154157

@@ -157,7 +160,7 @@ - (void)testRemoveNonExistentDocument {
157160

158161
self.persistence.run("testRemoveNonExistentDocument", [&]() {
159162
// no-op, but make sure it doesn't throw.
160-
XCTAssertNoThrow([self.remoteDocumentCache removeEntryForKey:testutil::Key(kDocPath)]);
163+
XCTAssertNoThrow(self.remoteDocumentCache->RemoveEntry(testutil::Key(kDocPath)));
161164
});
162165
}
163166

@@ -174,7 +177,7 @@ - (void)testDocumentsMatchingQuery {
174177
[self setTestDocumentAtPath:"c/1"];
175178

176179
FSTQuery *query = FSTTestQuery("b");
177-
DocumentMap results = [self.remoteDocumentCache documentsMatchingQuery:query];
180+
DocumentMap results = self.remoteDocumentCache->GetMatchingDocuments(query);
178181
[self expectMap:results.underlying_map()
179182
hasDocsInArray:@[
180183
FSTTestDoc("b/1", kVersion, _kDocData, FSTDocumentStateSynced),
@@ -189,7 +192,7 @@ - (void)testDocumentsMatchingQuery {
189192

190193
- (FSTDocument *)setTestDocumentAtPath:(const absl::string_view)path {
191194
FSTDocument *doc = FSTTestDoc(path, kVersion, _kDocData, FSTDocumentStateSynced);
192-
[self.remoteDocumentCache addEntry:doc];
195+
self.remoteDocumentCache->AddEntry(doc);
193196
return doc;
194197
}
195198

Firestore/Source/Local/FSTLevelDB.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ NS_ASSUME_NONNULL_BEGIN
8383

8484
@property(nonatomic, readonly, strong) FSTLevelDBLRUDelegate *referenceDelegate;
8585

86+
@property(nonatomic, readonly, strong) FSTLocalSerializer *serializer;
87+
8688
@end
8789

8890
NS_ASSUME_NONNULL_END

Firestore/Source/Local/FSTLevelDB.mm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ @interface FSTLevelDB ()
8383
- (size_t)byteSize;
8484

8585
@property(nonatomic, assign, getter=isStarted) BOOL started;
86-
@property(nonatomic, strong, readonly) FSTLocalSerializer *serializer;
8786

8887
@end
8988

Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <vector>
2525

26+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
2627
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2728
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
2829
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
@@ -41,16 +42,16 @@ namespace firestore {
4142
namespace local {
4243

4344
/** Cached Remote Documents backed by leveldb. */
44-
class LevelDbRemoteDocumentCache {
45+
class LevelDbRemoteDocumentCache : public RemoteDocumentCache {
4546
public:
4647
LevelDbRemoteDocumentCache(FSTLevelDB* db, FSTLocalSerializer* serializer);
4748

48-
void AddEntry(FSTMaybeDocument* document);
49-
void RemoveEntry(const model::DocumentKey& key);
49+
void AddEntry(FSTMaybeDocument* document) override;
50+
void RemoveEntry(const model::DocumentKey& key) override;
5051

51-
FSTMaybeDocument* _Nullable Get(const model::DocumentKey& key);
52-
model::MaybeDocumentMap GetAll(const model::DocumentKeySet& keys);
53-
model::DocumentMap GetMatchingDocuments(FSTQuery* query);
52+
FSTMaybeDocument* _Nullable Get(const model::DocumentKey& key) override;
53+
model::MaybeDocumentMap GetAll(const model::DocumentKeySet& keys) override;
54+
model::DocumentMap GetMatchingDocuments(FSTQuery* query) override;
5455

5556
private:
5657
FSTMaybeDocument* DecodeMaybeDocument(absl::string_view encoded,

Firestore/core/src/firebase/firestore/local/memory_remote_document_cache.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <vector>
2525

26+
#include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
2627
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2728
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
2829
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
@@ -39,14 +40,14 @@ namespace firebase {
3940
namespace firestore {
4041
namespace local {
4142

42-
class MemoryRemoteDocumentCache {
43+
class MemoryRemoteDocumentCache : public RemoteDocumentCache {
4344
public:
44-
void AddEntry(FSTMaybeDocument *document);
45-
void RemoveEntry(const model::DocumentKey &key);
45+
void AddEntry(FSTMaybeDocument *document) override;
46+
void RemoveEntry(const model::DocumentKey &key) override;
4647

47-
FSTMaybeDocument *_Nullable Get(const model::DocumentKey &key);
48-
model::MaybeDocumentMap GetAll(const model::DocumentKeySet &keys);
49-
model::DocumentMap GetMatchingDocuments(FSTQuery *query);
48+
FSTMaybeDocument *_Nullable Get(const model::DocumentKey &key) override;
49+
model::MaybeDocumentMap GetAll(const model::DocumentKeySet &keys) override;
50+
model::DocumentMap GetMatchingDocuments(FSTQuery *query) override;
5051

5152
std::vector<model::DocumentKey> RemoveOrphanedDocuments(
5253
FSTMemoryLRUReferenceDelegate *reference_delegate,

0 commit comments

Comments
 (0)