FSTLevelDBMutationQueueTests.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/local/reference_set.h"
  27. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  28. #include "absl/strings/string_view.h"
  29. #include "leveldb/db.h"
  30. NS_ASSUME_NONNULL_BEGIN
  31. using firebase::firestore::auth::User;
  32. using firebase::firestore::local::LevelDbMutationKey;
  33. using firebase::firestore::local::ReferenceSet;
  34. using firebase::firestore::model::BatchId;
  35. using firebase::firestore::util::OrderedCode;
  36. using leveldb::DB;
  37. using leveldb::Slice;
  38. using leveldb::Status;
  39. using leveldb::WriteOptions;
  40. // A dummy mutation value, useful for testing code that's known to examine only mutation keys.
  41. static const char *kDummy = "1";
  42. /**
  43. * Most of the tests for FSTLevelDBMutationQueue are performed on the FSTMutationQueue protocol in
  44. * FSTMutationQueueTests. This class is responsible for setting up the @a mutationQueue plus any
  45. * additional LevelDB-specific tests.
  46. */
  47. @interface FSTLevelDBMutationQueueTests : FSTMutationQueueTests
  48. @end
  49. /**
  50. * Creates a key that's structurally the same as LevelDbMutationKey except it allows for
  51. * nonstandard table names.
  52. */
  53. std::string MutationLikeKey(absl::string_view table, absl::string_view userID, BatchId batchID) {
  54. std::string key;
  55. OrderedCode::WriteString(&key, table);
  56. OrderedCode::WriteString(&key, userID);
  57. OrderedCode::WriteSignedNumIncreasing(&key, batchID);
  58. return key;
  59. }
  60. @implementation FSTLevelDBMutationQueueTests {
  61. FSTLevelDB *_db;
  62. ReferenceSet _additionalReferences;
  63. }
  64. - (void)setUp {
  65. [super setUp];
  66. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  67. [_db.referenceDelegate addInMemoryPins:&_additionalReferences];
  68. self.mutationQueue = [_db mutationQueueForUser:User("user")];
  69. self.persistence = _db;
  70. self.persistence.run("Setup", [&]() { [self.mutationQueue start]; });
  71. }
  72. - (void)testLoadNextBatchID_zeroWhenTotallyEmpty {
  73. // Initial seek is invalid
  74. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  75. }
  76. - (void)testLoadNextBatchID_zeroWhenNoMutations {
  77. // Initial seek finds no mutations
  78. [self setDummyValueForKey:MutationLikeKey("mutationr", "foo", 20)];
  79. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  80. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  81. }
  82. - (void)testLoadNextBatchID_findsSingleRow {
  83. // Seeks off the end of the table altogether
  84. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  85. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  86. }
  87. - (void)testLoadNextBatchID_findsSingleRowAmongNonMutations {
  88. // Seeks into table following mutations.
  89. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  90. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  91. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  92. }
  93. - (void)testLoadNextBatchID_findsMaxAcrossUsers {
  94. [self setDummyValueForKey:LevelDbMutationKey::Key("fo", 5)];
  95. [self setDummyValueForKey:LevelDbMutationKey::Key("food", 3)];
  96. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 6)];
  97. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 2)];
  98. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
  99. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  100. }
  101. - (void)testLoadNextBatchID_onlyFindsMutations {
  102. // Write higher-valued batchIDs in nearby "tables"
  103. std::vector<std::string> tables{"mutatio", "mutationsa", "bears", "zombies"};
  104. BatchId highBatchID = 5;
  105. for (const auto &table : tables) {
  106. [self setDummyValueForKey:MutationLikeKey(table, "", highBatchID++)];
  107. }
  108. [self setDummyValueForKey:LevelDbMutationKey::Key("bar", 3)];
  109. [self setDummyValueForKey:LevelDbMutationKey::Key("bar", 2)];
  110. [self setDummyValueForKey:LevelDbMutationKey::Key("foo", 1)];
  111. // None of the higher tables should match -- this is the only entry that's in the mutations
  112. // table
  113. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 4);
  114. }
  115. - (void)testEmptyProtoCanBeUpgraded {
  116. // An empty protocol buffer serializes to a zero-length byte buffer.
  117. GPBEmpty *empty = [GPBEmpty message];
  118. NSData *emptyData = [empty data];
  119. XCTAssertEqual(emptyData.length, 0);
  120. // Choose some other (arbitrary) proto and parse it from the empty message and it should all be
  121. // defaults. This shows that empty proto values within the index row value don't pose any future
  122. // liability.
  123. NSError *error;
  124. FSTPBMutationQueue *parsedMessage = [FSTPBMutationQueue parseFromData:emptyData error:&error];
  125. XCTAssertNil(error);
  126. FSTPBMutationQueue *defaultMessage = [FSTPBMutationQueue message];
  127. XCTAssertEqual(parsedMessage.lastAcknowledgedBatchId, defaultMessage.lastAcknowledgedBatchId);
  128. XCTAssertEqualObjects(parsedMessage.lastStreamToken, defaultMessage.lastStreamToken);
  129. }
  130. - (void)setDummyValueForKey:(const std::string &)key {
  131. _db.ptr->Put(WriteOptions(), key, kDummy);
  132. }
  133. @end
  134. NS_ASSUME_NONNULL_END