2017-10-03 08:55:22 -07:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
|
|
|
|
|
#import "Firestore/Example/Tests/Local/FSTQueryCacheTests.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-07-27 13:58:11 -07:00
|
|
|
#include "Firestore/core/include/firebase/firestore/timestamp.h"
|
2019-09-12 14:28:10 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/leveldb_persistence.h"
|
2018-12-21 22:08:34 -08:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/leveldb_query_cache.h"
|
2019-09-12 14:28:10 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/persistence.h"
|
2019-08-20 14:56:32 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/query_data.h"
|
2019-09-12 14:28:10 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/reference_delegate.h"
|
2018-12-21 21:03:13 -08:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/reference_set.h"
|
2018-12-21 22:08:34 -08:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
|
2018-07-27 13:58:11 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
|
|
|
|
|
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
|
2018-12-21 22:08:34 -08:00
|
|
|
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
namespace core = firebase::firestore::core;
|
2018-12-21 22:08:34 -08:00
|
|
|
namespace testutil = firebase::firestore::testutil;
|
2019-09-12 14:28:10 -07:00
|
|
|
|
2018-07-27 13:58:11 -07:00
|
|
|
using firebase::Timestamp;
|
2019-09-12 14:28:10 -07:00
|
|
|
using firebase::firestore::local::LevelDbPersistence;
|
2018-12-21 22:08:34 -08:00
|
|
|
using firebase::firestore::local::LevelDbQueryCache;
|
2019-09-12 14:28:10 -07:00
|
|
|
using firebase::firestore::local::Persistence;
|
2019-08-20 14:56:32 -07:00
|
|
|
using firebase::firestore::local::QueryData;
|
|
|
|
|
using firebase::firestore::local::QueryPurpose;
|
2018-12-21 21:03:13 -08:00
|
|
|
using firebase::firestore::local::ReferenceSet;
|
2018-12-21 22:08:34 -08:00
|
|
|
using firebase::firestore::model::DocumentKey;
|
2018-08-16 14:02:03 -07:00
|
|
|
using firebase::firestore::model::ListenSequenceNumber;
|
2018-07-27 13:58:11 -07:00
|
|
|
using firebase::firestore::model::ResourcePath;
|
|
|
|
|
using firebase::firestore::model::SnapshotVersion;
|
2018-08-15 15:56:45 -07:00
|
|
|
using firebase::firestore::model::TargetId;
|
2018-08-17 14:42:05 -07:00
|
|
|
using firebase::firestore::util::Path;
|
2019-08-02 11:35:33 -07:00
|
|
|
using testutil::Query;
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@interface FSTLevelDBQueryCacheTests : FSTQueryCacheTests
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The tests for FSTLevelDBQueryCache are performed on the FSTQueryCache protocol in
|
|
|
|
|
* FSTQueryCacheTests. This class is merely responsible for setting up and tearing down the
|
|
|
|
|
* @a queryCache.
|
|
|
|
|
*/
|
2018-12-21 21:03:13 -08:00
|
|
|
@implementation FSTLevelDBQueryCacheTests {
|
2019-09-12 14:28:10 -07:00
|
|
|
std::unique_ptr<LevelDbPersistence> _db;
|
2018-12-21 21:03:13 -08:00
|
|
|
ReferenceSet _additionalReferences;
|
|
|
|
|
}
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
- (LevelDbQueryCache *)getCache:(Persistence *)persistence {
|
|
|
|
|
return static_cast<LevelDbQueryCache *>(persistence->query_cache());
|
2018-12-21 22:08:34 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
- (void)setUp {
|
|
|
|
|
[super setUp];
|
|
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
_db = [FSTPersistenceTestHelpers levelDBPersistence];
|
|
|
|
|
self.persistence = _db.get();
|
|
|
|
|
self.queryCache = [self getCache:_db.get()];
|
|
|
|
|
self.persistence->reference_delegate()->AddInMemoryPins(&_additionalReferences);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)tearDown {
|
2018-07-27 13:58:11 -07:00
|
|
|
[super tearDown];
|
2019-09-12 14:28:10 -07:00
|
|
|
self.persistence = nullptr;
|
|
|
|
|
self.queryCache = nullptr;
|
2018-07-27 13:58:11 -07:00
|
|
|
}
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-07-27 13:58:11 -07:00
|
|
|
- (void)testMetadataPersistedAcrossRestarts {
|
2019-09-12 14:28:10 -07:00
|
|
|
self.persistence->Shutdown();
|
|
|
|
|
_db.reset();
|
|
|
|
|
self.persistence = nullptr;
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2018-08-17 14:42:05 -07:00
|
|
|
Path dir = [FSTPersistenceTestHelpers levelDBDir];
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
auto db1 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
|
|
|
|
|
LevelDbQueryCache *queryCache = [self getCache:db1.get()];
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2018-12-21 22:08:34 -08:00
|
|
|
XCTAssertEqual(0, queryCache->highest_listen_sequence_number());
|
|
|
|
|
XCTAssertEqual(0, queryCache->highest_target_id());
|
2018-07-27 13:58:11 -07:00
|
|
|
SnapshotVersion versionZero;
|
2018-12-21 22:08:34 -08:00
|
|
|
XCTAssertEqual(versionZero, queryCache->GetLastRemoteSnapshotVersion());
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2018-08-16 14:02:03 -07:00
|
|
|
ListenSequenceNumber minimumSequenceNumber = 1234;
|
2018-08-15 15:56:45 -07:00
|
|
|
TargetId lastTargetId = 5;
|
2018-07-27 13:58:11 -07:00
|
|
|
SnapshotVersion lastVersion(Timestamp(1, 2));
|
|
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
db1->Run("add query data", [&] {
|
2019-08-02 11:35:33 -07:00
|
|
|
core::Query query = Query("some/path");
|
2019-08-20 14:56:32 -07:00
|
|
|
QueryData queryData(std::move(query), lastTargetId, minimumSequenceNumber,
|
|
|
|
|
QueryPurpose::Listen);
|
2018-12-21 22:08:34 -08:00
|
|
|
queryCache->AddTarget(queryData);
|
|
|
|
|
queryCache->SetLastRemoteSnapshotVersion(lastVersion);
|
2018-07-27 13:58:11 -07:00
|
|
|
});
|
|
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
db1->Shutdown();
|
|
|
|
|
db1.reset();
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
auto db2 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
|
|
|
|
|
db2->Run("verify sequence number", [&] {
|
2018-07-27 13:58:11 -07:00
|
|
|
// We should remember the previous sequence number, and the next transaction should
|
|
|
|
|
// have a higher one.
|
2019-09-12 14:28:10 -07:00
|
|
|
XCTAssertGreaterThan(db2->current_sequence_number(), minimumSequenceNumber);
|
2018-07-27 13:58:11 -07:00
|
|
|
});
|
|
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
LevelDbQueryCache *queryCache2 = [self getCache:db2.get()];
|
2018-12-21 22:08:34 -08:00
|
|
|
XCTAssertEqual(lastTargetId, queryCache2->highest_target_id());
|
|
|
|
|
XCTAssertEqual(lastVersion, queryCache2->GetLastRemoteSnapshotVersion());
|
2018-07-27 13:58:11 -07:00
|
|
|
|
2019-09-12 14:28:10 -07:00
|
|
|
db2->Shutdown();
|
|
|
|
|
db2.reset();
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-21 22:08:34 -08:00
|
|
|
- (void)testRemoveMatchingKeysForTargetID {
|
2019-09-12 14:28:10 -07:00
|
|
|
self.persistence->Run("testRemoveMatchingKeysForTargetID", [&]() {
|
2018-12-21 22:08:34 -08:00
|
|
|
DocumentKey key1 = testutil::Key("foo/bar");
|
|
|
|
|
DocumentKey key2 = testutil::Key("foo/baz");
|
|
|
|
|
DocumentKey key3 = testutil::Key("foo/blah");
|
|
|
|
|
|
|
|
|
|
LevelDbQueryCache *cache = [self getCache:self.persistence];
|
|
|
|
|
[self addMatchingKey:key1 forTargetID:1];
|
|
|
|
|
[self addMatchingKey:key2 forTargetID:1];
|
|
|
|
|
[self addMatchingKey:key3 forTargetID:2];
|
|
|
|
|
XCTAssertTrue(cache->Contains(key1));
|
|
|
|
|
XCTAssertTrue(cache->Contains(key2));
|
|
|
|
|
XCTAssertTrue(cache->Contains(key3));
|
|
|
|
|
|
|
|
|
|
cache->RemoveAllKeysForTarget(1);
|
|
|
|
|
XCTAssertFalse(self.queryCache->Contains(key1));
|
|
|
|
|
XCTAssertFalse(self.queryCache->Contains(key2));
|
|
|
|
|
XCTAssertTrue(self.queryCache->Contains(key3));
|
|
|
|
|
|
|
|
|
|
cache->RemoveAllKeysForTarget(2);
|
|
|
|
|
XCTAssertFalse(self.queryCache->Contains(key1));
|
|
|
|
|
XCTAssertFalse(self.queryCache->Contains(key2));
|
|
|
|
|
XCTAssertFalse(self.queryCache->Contains(key3));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|