FSTLevelDBMutationQueueTests.mm 6.1 KB

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