Files
firebase-ios-sdk/Firestore/Example/Tests/Local/FSTLevelDBMutationQueueTests.mm
T

160 lines
5.7 KiB
Plaintext
Raw Normal View History

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.
*/
#import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
2017-10-03 08:55:22 -07:00
#import <XCTest/XCTest.h>
#include <string>
2018-08-08 12:34:03 -07:00
#include <vector>
2017-10-03 08:55:22 -07:00
#import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
#import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
2018-08-08 12:34:03 -07:00
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
2017-10-03 08:55:22 -07:00
#include "Firestore/core/src/firebase/firestore/auth/user.h"
2018-08-08 12:34:03 -07:00
#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
2018-08-08 12:34:03 -07:00
#include "absl/strings/string_view.h"
#include "leveldb/db.h"
2017-10-03 08:55:22 -07:00
NS_ASSUME_NONNULL_BEGIN
2018-08-08 12:34:03 -07:00
using firebase::firestore::auth::User;
using firebase::firestore::local::LevelDbMutationKey;
using firebase::firestore::util::OrderedCode;
2017-10-03 08:55:22 -07:00
using leveldb::DB;
using leveldb::Slice;
using leveldb::Status;
using leveldb::WriteOptions;
// A dummy mutation value, useful for testing code that's known to examine only mutation keys.
static const char *kDummy = "1";
/**
* Most of the tests for FSTLevelDBMutationQueue are performed on the FSTMutationQueue protocol in
* FSTMutationQueueTests. This class is responsible for setting up the @a mutationQueue plus any
* additional LevelDB-specific tests.
*/
@interface FSTLevelDBMutationQueueTests : FSTMutationQueueTests
@end
/**
2018-08-08 12:34:03 -07:00
* Creates a key that's structurally the same as LevelDbMutationKey except it allows for
2017-10-03 08:55:22 -07:00
* nonstandard table names.
*/
2018-08-08 12:34:03 -07:00
std::string MutationLikeKey(absl::string_view table, absl::string_view userID, FSTBatchID batchID) {
2017-10-03 08:55:22 -07:00
std::string key;
OrderedCode::WriteString(&key, table);
OrderedCode::WriteString(&key, userID);
OrderedCode::WriteSignedNumIncreasing(&key, batchID);
return key;
}
@implementation FSTLevelDBMutationQueueTests {
FSTLevelDB *_db;
}
- (void)setUp {
[super setUp];
_db = [FSTPersistenceTestHelpers levelDBPersistence];
self.mutationQueue = [_db mutationQueueForUser:User("user")];
2017-10-03 08:55:22 -07:00
self.persistence = _db;
2018-03-30 10:18:25 -07:00
self.persistence.run("Setup", [&]() { [self.mutationQueue start]; });
2017-10-03 08:55:22 -07:00
}
- (void)testLoadNextBatchID_zeroWhenTotallyEmpty {
// Initial seek is invalid
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
}
- (void)testLoadNextBatchID_zeroWhenNoMutations {
// Initial seek finds no mutations
[self setDummyValueForKey:MutationLikeKey("mutationr", "foo", 20)];
[self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
}
- (void)testLoadNextBatchID_findsSingleRow {
// Seeks off the end of the table altogether
2018-08-08 12:34:03 -07:00
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
2017-10-03 08:55:22 -07:00
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
}
- (void)testLoadNextBatchID_findsSingleRowAmongNonMutations {
// Seeks into table following mutations.
2018-08-08 12:34:03 -07:00
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
2017-10-03 08:55:22 -07:00
[self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
}
- (void)testLoadNextBatchID_findsMaxAcrossUsers {
2018-08-08 12:34:03 -07:00
[self setDummyValueForKey:LevelDbMutationKey::Key("fo", 5)];
[self setDummyValueForKey:LevelDbMutationKey::Key("food", 3)];
2017-10-03 08:55:22 -07:00
2018-08-08 12:34:03 -07:00
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 2)];
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
2017-10-03 08:55:22 -07:00
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
}
- (void)testLoadNextBatchID_onlyFindsMutations {
// Write higher-valued batchIDs in nearby "tables"
2018-08-08 12:34:03 -07:00
std::vector<std::string> tables{"mutatio", "mutationsa", "bears", "zombies"};
2017-10-03 08:55:22 -07:00
FSTBatchID highBatchID = 5;
2018-08-08 12:34:03 -07:00
for (const auto &table : tables) {
2017-10-03 08:55:22 -07:00
[self setDummyValueForKey:MutationLikeKey(table, "", highBatchID++)];
}
2018-08-08 12:34:03 -07:00
[self setDummyValueForKey:LevelDbMutationKey::Key("bar", 3)];
[self setDummyValueForKey:LevelDbMutationKey::Key("bar", 2)];
[self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
2017-10-03 08:55:22 -07:00
// None of the higher tables should match -- this is the only entry that's in the mutations
// table
XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 4);
}
- (void)testEmptyProtoCanBeUpgraded {
// An empty protocol buffer serializes to a zero-length byte buffer.
GPBEmpty *empty = [GPBEmpty message];
NSData *emptyData = [empty data];
XCTAssertEqual(emptyData.length, 0);
// Choose some other (arbitrary) proto and parse it from the empty message and it should all be
// defaults. This shows that empty proto values within the index row value don't pose any future
// liability.
NSError *error;
FSTPBMutationQueue *parsedMessage = [FSTPBMutationQueue parseFromData:emptyData error:&error];
XCTAssertNil(error);
FSTPBMutationQueue *defaultMessage = [FSTPBMutationQueue message];
XCTAssertEqual(parsedMessage.lastAcknowledgedBatchId, defaultMessage.lastAcknowledgedBatchId);
XCTAssertEqualObjects(parsedMessage.lastStreamToken, defaultMessage.lastStreamToken);
}
- (void)setDummyValueForKey:(const std::string &)key {
_db.ptr->Put(WriteOptions(), key, kDummy);
}
@end
NS_ASSUME_NONNULL_END