FSTLevelDBMutationQueueTests.mm 5.9 KB

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