aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests
diff options
context:
space:
mode:
authorGravatar Greg Soltis <gsoltis@google.com>2018-03-22 15:45:05 -0700
committerGravatar GitHub <noreply@github.com>2018-03-22 15:45:05 -0700
commit4afaafdc770612185c4e11d3f09226ce168462a5 (patch)
tree21c912e4c719a1b0a0961457017efce1f4b385c0 /Firestore/Example/Tests
parent1e769d708459ff64ca3571ab562377cc56a9d637 (diff)
Change write groups to use transactions (#912)
* Start work on leveldb transactions * Style * Working API. Not plumbed in yet * Move files into correct place * Wrangling file locations and associations * Tests pass * Add some comments * style * Fix copyright * Rewrite iterator internals to handle deletion-while-iterating. Also add tests for same * Switch to strings instead of slices * Style * More style fixes * Start switching writegroup over * Swap out write group tracking for transaction usage * Style * Response to feedback before updating docs * Style * Add comment * Initialize version_ * Satisfy the linter * Start switching writegroup over * Swap out write group tracking for transaction usage * Style * Checkpoint before implementing BatchDescription * Style * Everything passing * Drop unused function * Style * Renaming * Style * Add log line * Style * Add debug log line for commits, drop unused BatchDescription
Diffstat (limited to 'Firestore/Example/Tests')
-rw-r--r--Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm58
-rw-r--r--Firestore/Example/Tests/Local/FSTWriteGroupTests.mm20
2 files changed, 34 insertions, 44 deletions
diff --git a/Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm b/Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
index 3559d5d..822e3ca 100644
--- a/Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
+++ b/Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
@@ -18,10 +18,10 @@
#include <leveldb/db.h>
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
+#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Local/FSTLevelDBMigrations.h"
#import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
-#import "Firestore/Source/Local/FSTWriteGroup.h"
#include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
@@ -29,6 +29,7 @@
NS_ASSUME_NONNULL_BEGIN
+using firebase::firestore::local::LevelDbTransaction;
using firebase::firestore::util::OrderedCode;
using leveldb::DB;
using leveldb::Options;
@@ -60,43 +61,52 @@ using leveldb::Status;
- (void)testAddsTargetGlobal {
FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
- [FSTLevelDBMigrations runMigrationsOnDB:_db];
+ LevelDbTransaction transaction(_db.get());
+ [FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
+ transaction.Commit();
metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
}
- (void)testSetsVersionNumber {
- FSTLevelDBSchemaVersion initial = [FSTLevelDBMigrations schemaVersionForDB:_db];
+ LevelDbTransaction transaction(_db.get());
+ FSTLevelDBSchemaVersion initial =
+ [FSTLevelDBMigrations schemaVersionWithTransaction:&transaction];
XCTAssertEqual(0, initial, "No version should be equivalent to 0");
// Pick an arbitrary high migration number and migrate to it.
- [FSTLevelDBMigrations runMigrationsOnDB:_db];
- FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionForDB:_db];
+ [FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
+ FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionWithTransaction:&transaction];
XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
}
- (void)testCountsQueries {
NSUInteger expected = 50;
- FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"Setup"];
- for (int i = 0; i < expected; i++) {
- std::string key = [FSTLevelDBTargetKey keyWithTargetID:i];
- [group setData:"dummy" forKey:key];
+ {
+ // Setup some targets to be counted in the migration.
+ LevelDbTransaction transaction(_db.get());
+ for (int i = 0; i < expected; i++) {
+ std::string key = [FSTLevelDBTargetKey keyWithTargetID:i];
+ transaction.Put(key, "dummy");
+ }
+ // Add a dummy entry after the targets to make sure the iteration is correctly bounded.
+ // Use a table that would sort logically right after that table 'target'.
+ std::string dummyKey;
+ // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
+ // table.
+ OrderedCode::WriteSignedNumIncreasing(&dummyKey, 5);
+ OrderedCode::WriteString(&dummyKey, "targetA");
+ transaction.Put(dummyKey, "dummy");
+ transaction.Commit();
+ }
+
+ {
+ LevelDbTransaction transaction(_db.get());
+ [FSTLevelDBMigrations runMigrationsWithTransaction:&transaction];
+ transaction.Commit();
+ FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
+ XCTAssertEqual(expected, metadata.targetCount, @"Failed to count all of the targets we added");
}
- // Add a dummy entry after the targets to make sure the iteration is correctly bounded.
- // Use a table that would sort logically right after that table 'target'.
- std::string dummyKey;
- // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
- // table.
- OrderedCode::WriteSignedNumIncreasing(&dummyKey, 5);
- OrderedCode::WriteString(&dummyKey, "targetA");
- [group setData:"dummy" forKey:dummyKey];
-
- Status status = [group writeToDB:_db];
- XCTAssertTrue(status.ok(), @"Failed to write targets");
-
- [FSTLevelDBMigrations runMigrationsOnDB:_db];
- FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
- XCTAssertEqual(expected, metadata.targetCount, @"Failed to count all of the targets we added");
}
@end
diff --git a/Firestore/Example/Tests/Local/FSTWriteGroupTests.mm b/Firestore/Example/Tests/Local/FSTWriteGroupTests.mm
index edd05a3..871bd99 100644
--- a/Firestore/Example/Tests/Local/FSTWriteGroupTests.mm
+++ b/Firestore/Example/Tests/Local/FSTWriteGroupTests.mm
@@ -80,26 +80,6 @@ NS_ASSUME_NONNULL_BEGIN
XCTAssertTrue(status.IsNotFound());
}
-- (void)testDescription {
- std::string key = [FSTLevelDBMutationKey keyWithUserID:"user1" batchID:42];
- FSTPBWriteBatch *message = [FSTPBWriteBatch message];
- message.batchId = 42;
-
- FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"Action"];
- XCTAssertEqualObjects([group description], @"<FSTWriteGroup for Action: 0 changes (0 bytes):>");
-
- [group setMessage:message forKey:key];
- XCTAssertEqualObjects([group description],
- @"<FSTWriteGroup for Action: 1 changes (2 bytes):\n"
- " - Put [mutation: userID=user1 batchID=42] (2 bytes)>");
-
- [group removeMessageForKey:key];
- XCTAssertEqualObjects([group description],
- @"<FSTWriteGroup for Action: 2 changes (2 bytes):\n"
- " - Put [mutation: userID=user1 batchID=42] (2 bytes)\n"
- " - Delete [mutation: userID=user1 batchID=42]>");
-}
-
- (void)testCommittingWrongGroupThrows {
// If you don't create the group through persistence, it should throw.
FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"group"];