FSTLevelDBQueryCacheTests.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/FSTLevelDBQueryCache.h"
  17. #import "Firestore/Source/Core/FSTQuery.h"
  18. #import "Firestore/Source/Local/FSTLevelDB.h"
  19. #import "Firestore/Source/Local/FSTLocalSerializer.h"
  20. #import "Firestore/Source/Local/FSTQueryData.h"
  21. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  22. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  23. #import "Firestore/Example/Tests/Local/FSTQueryCacheTests.h"
  24. #include "Firestore/core/include/firebase/firestore/timestamp.h"
  25. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  26. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  27. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  28. using firebase::Timestamp;
  29. using firebase::firestore::model::DatabaseId;
  30. using firebase::firestore::model::ListenSequenceNumber;
  31. using firebase::firestore::model::ResourcePath;
  32. using firebase::firestore::model::SnapshotVersion;
  33. using firebase::firestore::model::TargetId;
  34. using firebase::firestore::util::Path;
  35. NS_ASSUME_NONNULL_BEGIN
  36. @interface FSTLevelDBQueryCacheTests : FSTQueryCacheTests
  37. @end
  38. /**
  39. * The tests for FSTLevelDBQueryCache are performed on the FSTQueryCache protocol in
  40. * FSTQueryCacheTests. This class is merely responsible for setting up and tearing down the
  41. * @a queryCache.
  42. */
  43. @implementation FSTLevelDBQueryCacheTests
  44. - (void)setUp {
  45. [super setUp];
  46. self.persistence = [FSTPersistenceTestHelpers levelDBPersistence];
  47. self.queryCache = [self.persistence queryCache];
  48. }
  49. - (void)tearDown {
  50. [super tearDown];
  51. self.persistence = nil;
  52. self.queryCache = nil;
  53. }
  54. - (void)testMetadataPersistedAcrossRestarts {
  55. [self.persistence shutdown];
  56. self.persistence = nil;
  57. Path dir = [FSTPersistenceTestHelpers levelDBDir];
  58. FSTLevelDB *db1 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
  59. FSTLevelDBQueryCache *queryCache = [db1 queryCache];
  60. XCTAssertEqual(0, queryCache.highestListenSequenceNumber);
  61. XCTAssertEqual(0, queryCache.highestTargetID);
  62. SnapshotVersion versionZero;
  63. XCTAssertEqual(versionZero, queryCache.lastRemoteSnapshotVersion);
  64. ListenSequenceNumber minimumSequenceNumber = 1234;
  65. TargetId lastTargetId = 5;
  66. SnapshotVersion lastVersion(Timestamp(1, 2));
  67. db1.run("add query data", [&]() {
  68. FSTQuery *query = [FSTQuery queryWithPath:ResourcePath{"some", "path"}];
  69. FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
  70. targetID:lastTargetId
  71. listenSequenceNumber:minimumSequenceNumber
  72. purpose:FSTQueryPurposeListen];
  73. [queryCache addQueryData:queryData];
  74. [queryCache setLastRemoteSnapshotVersion:lastVersion];
  75. });
  76. [db1 shutdown];
  77. db1 = nil;
  78. FSTLevelDB *db2 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
  79. db2.run("verify sequence number", [&]() {
  80. // We should remember the previous sequence number, and the next transaction should
  81. // have a higher one.
  82. XCTAssertGreaterThan(db2.currentSequenceNumber, minimumSequenceNumber);
  83. });
  84. FSTLevelDBQueryCache *queryCache2 = [db2 queryCache];
  85. XCTAssertEqual(lastTargetId, queryCache2.highestTargetID);
  86. XCTAssertEqual(lastVersion, queryCache2.lastRemoteSnapshotVersion);
  87. [db2 shutdown];
  88. db2 = nil;
  89. }
  90. @end
  91. NS_ASSUME_NONNULL_END