1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Copyright 2017 Google
*
* 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/Local/FSTQueryCacheTests.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
#import "Firestore/Source/Local/FSTPersistence.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Local/FSTWriteGroup.h"
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
namespace testutil = firebase::firestore::testutil;
using firebase::firestore::model::DocumentKey;
NS_ASSUME_NONNULL_BEGIN
@implementation FSTQueryCacheTests {
FSTQuery *_queryRooms;
FSTListenSequenceNumber _previousSequenceNumber;
FSTTargetID _previousTargetID;
FSTTestSnapshotVersion _previousSnapshotVersion;
}
- (void)setUp {
[super setUp];
_queryRooms = FSTTestQuery("rooms");
_previousSequenceNumber = 1000;
_previousTargetID = 500;
_previousSnapshotVersion = 100;
}
/**
* Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
* FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
*/
- (BOOL)isTestBaseClass {
return [self class] == [FSTQueryCacheTests class];
}
- (void)testReadQueryNotInCache {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"ReadQueryNotInCache"];
XCTAssertNil([self.queryCache queryDataForQuery:_queryRooms]);
[self.persistence commitGroup:group];
}
- (void)testSetAndReadAQuery {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"SetAndReadQuery"];
FSTQueryData *queryData = [self queryDataWithQuery:_queryRooms];
[self.queryCache addQueryData:queryData group:group];
FSTQueryData *result = [self.queryCache queryDataForQuery:_queryRooms];
XCTAssertEqualObjects(result.query, queryData.query);
XCTAssertEqual(result.targetID, queryData.targetID);
XCTAssertEqualObjects(result.resumeToken, queryData.resumeToken);
[self.persistence commitGroup:group];
}
- (void)testCanonicalIDCollision {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"CanonicalIDCollision"];
// Type information is currently lost in our canonicalID implementations so this currently an
// easy way to force colliding canonicalIDs
FSTQuery *q1 = [FSTTestQuery("a") queryByAddingFilter:FSTTestFilter("foo", @"==", @(1))];
FSTQuery *q2 = [FSTTestQuery("a") queryByAddingFilter:FSTTestFilter("foo", @"==", @"1")];
XCTAssertEqualObjects(q1.canonicalID, q2.canonicalID);
FSTQueryData *data1 = [self queryDataWithQuery:q1];
[self.queryCache addQueryData:data1 group:group];
// Using the other query should not return the query cache entry despite equal canonicalIDs.
XCTAssertNil([self.queryCache queryDataForQuery:q2]);
XCTAssertEqualObjects([self.queryCache queryDataForQuery:q1], data1);
FSTQueryData *data2 = [self queryDataWithQuery:q2];
[self.queryCache addQueryData:data2 group:group];
XCTAssertEqual([self.queryCache count], 2);
XCTAssertEqualObjects([self.queryCache queryDataForQuery:q1], data1);
XCTAssertEqualObjects([self.queryCache queryDataForQuery:q2], data2);
[self.queryCache removeQueryData:data1 group:group];
XCTAssertNil([self.queryCache queryDataForQuery:q1]);
XCTAssertEqualObjects([self.queryCache queryDataForQuery:q2], data2);
XCTAssertEqual([self.queryCache count], 1);
[self.queryCache removeQueryData:data2 group:group];
XCTAssertNil([self.queryCache queryDataForQuery:q1]);
XCTAssertNil([self.queryCache queryDataForQuery:q2]);
XCTAssertEqual([self.queryCache count], 0);
[self.persistence commitGroup:group];
}
- (void)testSetQueryToNewValue {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"SetQueryToNewValue"];
FSTQueryData *queryData1 =
[self queryDataWithQuery:_queryRooms targetID:1 listenSequenceNumber:10 version:1];
[self.queryCache addQueryData:queryData1 group:group];
FSTQueryData *queryData2 =
[self queryDataWithQuery:_queryRooms targetID:1 listenSequenceNumber:10 version:2];
[self.queryCache addQueryData:queryData2 group:group];
FSTQueryData *result = [self.queryCache queryDataForQuery:_queryRooms];
XCTAssertNotEqualObjects(queryData2.resumeToken, queryData1.resumeToken);
XCTAssertNotEqualObjects(queryData2.snapshotVersion, queryData1.snapshotVersion);
XCTAssertEqualObjects(result.resumeToken, queryData2.resumeToken);
XCTAssertEqualObjects(result.snapshotVersion, queryData2.snapshotVersion);
[self.persistence commitGroup:group];
}
- (void)testRemoveQuery {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveQuery"];
FSTQueryData *queryData1 = [self queryDataWithQuery:_queryRooms];
[self.queryCache addQueryData:queryData1 group:group];
[self.queryCache removeQueryData:queryData1 group:group];
FSTQueryData *result = [self.queryCache queryDataForQuery:_queryRooms];
XCTAssertNil(result);
[self.persistence commitGroup:group];
}
- (void)testRemoveNonExistentQuery {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveNonExistentQuery"];
FSTQueryData *queryData = [self queryDataWithQuery:_queryRooms];
// no-op, but make sure it doesn't throw.
XCTAssertNoThrow([self.queryCache removeQueryData:queryData group:group]);
[self.persistence commitGroup:group];
}
- (void)testRemoveQueryRemovesMatchingKeysToo {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group =
[self.persistence startGroupWithAction:@"RemoveQueryRemovesMatchingKeysToo"];
FSTQueryData *rooms = [self queryDataWithQuery:_queryRooms];
[self.queryCache addQueryData:rooms group:group];
DocumentKey key1 = testutil::Key("rooms/foo");
DocumentKey key2 = testutil::Key("rooms/bar");
[self addMatchingKey:key1 forTargetID:rooms.targetID group:group];
[self addMatchingKey:key2 forTargetID:rooms.targetID group:group];
XCTAssertTrue([self.queryCache containsKey:key1]);
XCTAssertTrue([self.queryCache containsKey:key2]);
[self.queryCache removeQueryData:rooms group:group];
XCTAssertFalse([self.queryCache containsKey:key1]);
XCTAssertFalse([self.queryCache containsKey:key2]);
[self.persistence commitGroup:group];
}
- (void)testAddOrRemoveMatchingKeys {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"AddOrRemoveMatchingKeys"];
DocumentKey key = testutil::Key("foo/bar");
XCTAssertFalse([self.queryCache containsKey:key]);
[self addMatchingKey:key forTargetID:1 group:group];
XCTAssertTrue([self.queryCache containsKey:key]);
[self addMatchingKey:key forTargetID:2 group:group];
XCTAssertTrue([self.queryCache containsKey:key]);
[self removeMatchingKey:key forTargetID:1 group:group];
XCTAssertTrue([self.queryCache containsKey:key]);
[self removeMatchingKey:key forTargetID:2 group:group];
XCTAssertFalse([self.queryCache containsKey:key]);
[self.persistence commitGroup:group];
}
- (void)testRemoveMatchingKeysForTargetID {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveMatchingKeysForTargetID"];
DocumentKey key1 = testutil::Key("foo/bar");
DocumentKey key2 = testutil::Key("foo/baz");
DocumentKey key3 = testutil::Key("foo/blah");
[self addMatchingKey:key1 forTargetID:1 group:group];
[self addMatchingKey:key2 forTargetID:1 group:group];
[self addMatchingKey:key3 forTargetID:2 group:group];
XCTAssertTrue([self.queryCache containsKey:key1]);
XCTAssertTrue([self.queryCache containsKey:key2]);
XCTAssertTrue([self.queryCache containsKey:key3]);
[self.queryCache removeMatchingKeysForTargetID:1 group:group];
XCTAssertFalse([self.queryCache containsKey:key1]);
XCTAssertFalse([self.queryCache containsKey:key2]);
XCTAssertTrue([self.queryCache containsKey:key3]);
[self.queryCache removeMatchingKeysForTargetID:2 group:group];
XCTAssertFalse([self.queryCache containsKey:key1]);
XCTAssertFalse([self.queryCache containsKey:key2]);
XCTAssertFalse([self.queryCache containsKey:key3]);
[self.persistence commitGroup:group];
}
- (void)testRemoveEmitsGarbageEvents {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveEmitsGarbageEvents"];
FSTEagerGarbageCollector *garbageCollector = [[FSTEagerGarbageCollector alloc] init];
[garbageCollector addGarbageSource:self.queryCache];
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({}));
FSTQueryData *rooms = [self queryDataWithQuery:FSTTestQuery("rooms")];
DocumentKey room1 = testutil::Key("rooms/bar");
DocumentKey room2 = testutil::Key("rooms/foo");
[self.queryCache addQueryData:rooms group:group];
[self addMatchingKey:room1 forTargetID:rooms.targetID group:group];
[self addMatchingKey:room2 forTargetID:rooms.targetID group:group];
FSTQueryData *halls = [self queryDataWithQuery:FSTTestQuery("halls")];
DocumentKey hall1 = testutil::Key("halls/bar");
DocumentKey hall2 = testutil::Key("halls/foo");
[self.queryCache addQueryData:halls group:group];
[self addMatchingKey:hall1 forTargetID:halls.targetID group:group];
[self addMatchingKey:hall2 forTargetID:halls.targetID group:group];
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({}));
[self removeMatchingKey:room1 forTargetID:rooms.targetID group:group];
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({room1}));
[self.queryCache removeQueryData:rooms group:group];
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({room2}));
[self.queryCache removeMatchingKeysForTargetID:halls.targetID group:group];
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({hall1, hall2}));
[self.persistence commitGroup:group];
}
- (void)testMatchingKeysForTargetID {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"MatchingKeysForTargetID"];
DocumentKey key1 = testutil::Key("foo/bar");
DocumentKey key2 = testutil::Key("foo/baz");
DocumentKey key3 = testutil::Key("foo/blah");
[self addMatchingKey:key1 forTargetID:1 group:group];
[self addMatchingKey:key2 forTargetID:1 group:group];
[self addMatchingKey:key3 forTargetID:2 group:group];
FSTAssertEqualSets([self.queryCache matchingKeysForTargetID:1], (@[ key1, key2 ]));
FSTAssertEqualSets([self.queryCache matchingKeysForTargetID:2], @[ key3 ]);
[self addMatchingKey:key1 forTargetID:2 group:group];
FSTAssertEqualSets([self.queryCache matchingKeysForTargetID:1], (@[ key1, key2 ]));
FSTAssertEqualSets([self.queryCache matchingKeysForTargetID:2], (@[ key1, key3 ]));
[self.persistence commitGroup:group];
}
- (void)testHighestListenSequenceNumber {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"HighestListenSequenceNumber"];
FSTQueryData *query1 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("rooms")
targetID:1
listenSequenceNumber:10
purpose:FSTQueryPurposeListen];
[self.queryCache addQueryData:query1 group:group];
FSTQueryData *query2 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("halls")
targetID:2
listenSequenceNumber:20
purpose:FSTQueryPurposeListen];
[self.queryCache addQueryData:query2 group:group];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 20);
// TargetIDs never come down.
[self.queryCache removeQueryData:query2 group:group];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 20);
// A query with an empty result set still counts.
FSTQueryData *query3 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("garages")
targetID:42
listenSequenceNumber:100
purpose:FSTQueryPurposeListen];
[self.queryCache addQueryData:query3 group:group];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 100);
[self.queryCache removeQueryData:query1 group:group];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 100);
[self.queryCache removeQueryData:query3 group:group];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 100);
[self.persistence commitGroup:group];
// Verify that the highestTargetID even survives restarts.
[self.queryCache shutdown];
self.queryCache = [self.persistence queryCache];
[self.queryCache start];
XCTAssertEqual([self.queryCache highestListenSequenceNumber], 100);
}
- (void)testHighestTargetID {
if ([self isTestBaseClass]) return;
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveMatchingKeysForTargetID"];
XCTAssertEqual([self.queryCache highestTargetID], 0);
FSTQueryData *query1 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("rooms")
targetID:1
listenSequenceNumber:10
purpose:FSTQueryPurposeListen];
DocumentKey key1 = testutil::Key("rooms/bar");
DocumentKey key2 = testutil::Key("rooms/foo");
[self.queryCache addQueryData:query1 group:group];
[self addMatchingKey:key1 forTargetID:1 group:group];
[self addMatchingKey:key2 forTargetID:1 group:group];
FSTQueryData *query2 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("halls")
targetID:2
listenSequenceNumber:20
purpose:FSTQueryPurposeListen];
DocumentKey key3 = testutil::Key("halls/foo");
[self.queryCache addQueryData:query2 group:group];
[self addMatchingKey:key3 forTargetID:2 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 2);
// TargetIDs never come down.
[self.queryCache removeQueryData:query2 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 2);
// A query with an empty result set still counts.
FSTQueryData *query3 = [[FSTQueryData alloc] initWithQuery:FSTTestQuery("garages")
targetID:42
listenSequenceNumber:100
purpose:FSTQueryPurposeListen];
[self.queryCache addQueryData:query3 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 42);
[self.queryCache removeQueryData:query1 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 42);
[self.queryCache removeQueryData:query3 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 42);
[self.persistence commitGroup:group];
// Verify that the highestTargetID even survives restarts.
[self.queryCache shutdown];
self.queryCache = [self.persistence queryCache];
[self.queryCache start];
XCTAssertEqual([self.queryCache highestTargetID], 42);
}
- (void)testLastRemoteSnapshotVersion {
if ([self isTestBaseClass]) return;
XCTAssertEqualObjects([self.queryCache lastRemoteSnapshotVersion],
[FSTSnapshotVersion noVersion]);
// Can set the snapshot version.
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"setLastRemoteSnapshotVersion"];
[self.queryCache setLastRemoteSnapshotVersion:FSTTestVersion(42) group:group];
[self.persistence commitGroup:group];
XCTAssertEqualObjects([self.queryCache lastRemoteSnapshotVersion], FSTTestVersion(42));
// Snapshot version persists restarts.
self.queryCache = [self.persistence queryCache];
[self.queryCache start];
XCTAssertEqualObjects([self.queryCache lastRemoteSnapshotVersion], FSTTestVersion(42));
}
#pragma mark - Helpers
/**
* Creates a new FSTQueryData object from the given parameters, synthesizing a resume token from
* the snapshot version.
*/
- (FSTQueryData *)queryDataWithQuery:(FSTQuery *)query {
return [self queryDataWithQuery:query
targetID:++_previousTargetID
listenSequenceNumber:++_previousSequenceNumber
version:++_previousSnapshotVersion];
}
- (FSTQueryData *)queryDataWithQuery:(FSTQuery *)query
targetID:(FSTTargetID)targetID
listenSequenceNumber:(FSTListenSequenceNumber)sequenceNumber
version:(FSTTestSnapshotVersion)version {
NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(version);
return [[FSTQueryData alloc] initWithQuery:query
targetID:targetID
listenSequenceNumber:sequenceNumber
purpose:FSTQueryPurposeListen
snapshotVersion:FSTTestVersion(version)
resumeToken:resumeToken];
}
- (void)addMatchingKey:(const DocumentKey &)key
forTargetID:(FSTTargetID)targetID
group:(FSTWriteGroup *)group {
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];
keys = [keys setByAddingObject:key];
[self.queryCache addMatchingKeys:keys forTargetID:targetID group:group];
}
- (void)removeMatchingKey:(const DocumentKey &)key
forTargetID:(FSTTargetID)targetID
group:(FSTWriteGroup *)group {
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];
keys = [keys setByAddingObject:key];
[self.queryCache removeMatchingKeys:keys forTargetID:targetID group:group];
}
@end
NS_ASSUME_NONNULL_END
|