FSTLevelDBMutationQueueTests.mm 5.8 KB

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