FSTLevelDBMutationQueueTests.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #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/Example/Tests/Local/FSTMutationQueueTests.h"
  23. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  24. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  25. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  26. #include "leveldb/db.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. self.persistence.run("Setup", [&]() { [self.mutationQueue start]; });
  64. }
  65. - (void)testLoadNextBatchID_zeroWhenTotallyEmpty {
  66. // Initial seek is invalid
  67. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  68. }
  69. - (void)testLoadNextBatchID_zeroWhenNoMutations {
  70. // Initial seek finds no mutations
  71. [self setDummyValueForKey:MutationLikeKey("mutationr", "foo", 20)];
  72. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  73. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 0);
  74. }
  75. - (void)testLoadNextBatchID_findsSingleRow {
  76. // Seeks off the end of the table altogether
  77. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:6]];
  78. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  79. }
  80. - (void)testLoadNextBatchID_findsSingleRowAmongNonMutations {
  81. // Seeks into table following mutations.
  82. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:6]];
  83. [self setDummyValueForKey:MutationLikeKey("mutationsa", "foo", 10)];
  84. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  85. }
  86. - (void)testLoadNextBatchID_findsMaxAcrossUsers {
  87. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"fo" batchID:5]];
  88. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"food" batchID:3]];
  89. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:6]];
  90. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:2]];
  91. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:1]];
  92. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 7);
  93. }
  94. - (void)testLoadNextBatchID_onlyFindsMutations {
  95. // Write higher-valued batchIDs in nearby "tables"
  96. auto tables = @[ @"mutatio", @"mutationsa", @"bears", @"zombies" ];
  97. FSTBatchID highBatchID = 5;
  98. for (NSString *table in tables) {
  99. [self setDummyValueForKey:MutationLikeKey(table, "", highBatchID++)];
  100. }
  101. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"bar" batchID:3]];
  102. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"bar" batchID:2]];
  103. [self setDummyValueForKey:[FSTLevelDBMutationKey keyWithUserID:@"foo" batchID:1]];
  104. // None of the higher tables should match -- this is the only entry that's in the mutations
  105. // table
  106. XCTAssertEqual([FSTLevelDBMutationQueue loadNextBatchIDFromDB:_db.ptr], 4);
  107. }
  108. - (void)testEmptyProtoCanBeUpgraded {
  109. // An empty protocol buffer serializes to a zero-length byte buffer.
  110. GPBEmpty *empty = [GPBEmpty message];
  111. NSData *emptyData = [empty data];
  112. XCTAssertEqual(emptyData.length, 0);
  113. // Choose some other (arbitrary) proto and parse it from the empty message and it should all be
  114. // defaults. This shows that empty proto values within the index row value don't pose any future
  115. // liability.
  116. NSError *error;
  117. FSTPBMutationQueue *parsedMessage = [FSTPBMutationQueue parseFromData:emptyData error:&error];
  118. XCTAssertNil(error);
  119. FSTPBMutationQueue *defaultMessage = [FSTPBMutationQueue message];
  120. XCTAssertEqual(parsedMessage.lastAcknowledgedBatchId, defaultMessage.lastAcknowledgedBatchId);
  121. XCTAssertEqualObjects(parsedMessage.lastStreamToken, defaultMessage.lastStreamToken);
  122. }
  123. - (void)setDummyValueForKey:(const std::string &)key {
  124. _db.ptr->Put(WriteOptions(), key, kDummy);
  125. }
  126. @end
  127. NS_ASSUME_NONNULL_END