FSTLevelDBMutationQueueTests.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
  17. #import <XCTest/XCTest.h>
  18. #include <string>
  19. #include <vector>
  20. #import "Firestore/Example/Tests/Local/FSTMutationQueueTests.h"
  21. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  22. #import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
  23. #import "Firestore/Source/Local/FSTLevelDB.h"
  24. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  25. #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
  26. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  27. #include "absl/strings/string_view.h"
  28. #include "leveldb/db.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. using firebase::firestore::auth::User;
  31. using firebase::firestore::local::LevelDbMutationKey;
  32. using firebase::firestore::model::BatchId;
  33. using firebase::firestore::util::OrderedCode;
  34. using leveldb::DB;
  35. using leveldb::Slice;
  36. using leveldb::Status;
  37. using leveldb::WriteOptions;
  38. // A dummy mutation value, useful for testing code that's known to examine only mutation keys.
  39. static const char *kDummy = "1";
  40. /**
  41. * Most of the tests for FSTLevelDBMutationQueue are performed on the FSTMutationQueue protocol in
  42. * FSTMutationQueueTests. This class is responsible for setting up the @a mutationQueue plus any
  43. * additional LevelDB-specific tests.
  44. */
  45. @interface FSTLevelDBMutationQueueTests : FSTMutationQueueTests
  46. @end
  47. /**
  48. * Creates a key that's structurally the same as LevelDbMutationKey except it allows for
  49. * nonstandard table names.
  50. */
  51. std::string MutationLikeKey(absl::string_view table, absl::string_view userID, BatchId batchID) {
  52. std::string key;
  53. OrderedCode::WriteString(&key, table);
  54. OrderedCode::WriteString(&key, userID);
  55. OrderedCode::WriteSignedNumIncreasing(&key, batchID);
  56. return key;
  57. }
  58. @implementation FSTLevelDBMutationQueueTests {
  59. FSTLevelDB *_db;
  60. }
  61. - (void)setUp {
  62. [super setUp];
  63. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  64. self.mutationQueue = [_db mutationQueueForUser:User("user")];
  65. self.persistence = _db;
  66. self.persistence.run("Setup", [&]() { [self.mutationQueue start]; });
  67. }
  68. - (void)testLoadNextBatchID_zeroWhenTotallyEmpty {
  69. // Initial seek is invalid
  70. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  71. }
  72. - (void)testLoadNextBatchID_zeroWhenNoMutations {
  73. // Initial seek finds no mutations
  74. [self setDummyValueForKey:MutationLikeKey("mutationr", "foo", 20)];
  75. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  76. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  77. }
  78. - (void)testLoadNextBatchID_findsSingleRow {
  79. // Seeks off the end of the table altogether
  80. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  81. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  82. }
  83. - (void)testLoadNextBatchID_findsSingleRowAmongNonMutations {
  84. // Seeks into table following mutations.
  85. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  86. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  87. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  88. }
  89. - (void)testLoadNextBatchID_findsMaxAcrossUsers {
  90. [self setDummyValueForKey:LevelDbMutationKey::Key("fo", 5)];
  91. [self setDummyValueForKey:LevelDbMutationKey::Key("food", 3)];
  92. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  93. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 2)];
  94. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
  95. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  96. }
  97. - (void)testLoadNextBatchID_onlyFindsMutations {
  98. // Write higher-valued batchIDs in nearby "tables"
  99. std::vector<std::string> tables{"mutatio", "mutationsa", "bears", "zombies"};
  100. BatchId highBatchID = 5;
  101. for (const auto &table : tables) {
  102. [self setDummyValueForKey:MutationLikeKey(table, "", highBatchID++)];
  103. }
  104. [self setDummyValueForKey:LevelDbMutationKey::Key("bar", 3)];
  105. [self setDummyValueForKey:LevelDbMutationKey::Key("bar", 2)];
  106. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
  107. // None of the higher tables should match -- this is the only entry that's in the mutations
  108. // table
  109. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 4);
  110. }
  111. - (void)testEmptyProtoCanBeUpgraded {
  112. // An empty protocol buffer serializes to a zero-length byte buffer.
  113. GPBEmpty *empty = [GPBEmpty message];
  114. NSData *emptyData = [empty data];
  115. XCTAssertEqual(emptyData.length, 0);
  116. // Choose some other (arbitrary) proto and parse it from the empty message and it should all be
  117. // defaults. This shows that empty proto values within the index row value don't pose any future
  118. // liability.
  119. NSError *error;
  120. FSTPBMutationQueue *parsedMessage = [FSTPBMutationQueue parseFromData:emptyData error:&error];
  121. XCTAssertNil(error);
  122. FSTPBMutationQueue *defaultMessage = [FSTPBMutationQueue message];
  123. XCTAssertEqual(parsedMessage.lastAcknowledgedBatchId, defaultMessage.lastAcknowledgedBatchId);
  124. XCTAssertEqualObjects(parsedMessage.lastStreamToken, defaultMessage.lastStreamToken);
  125. }
  126. - (void)setDummyValueForKey:(const std::string &)key {
  127. _db.ptr->Put(WriteOptions(), key, kDummy);
  128. }
  129. @end
  130. NS_ASSUME_NONNULL_END