FSTLevelDBQueryCacheTests.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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::ResourcePath;
  31. using firebase::firestore::model::SnapshotVersion;
  32. NS_ASSUME_NONNULL_BEGIN
  33. @interface FSTLevelDBQueryCacheTests : FSTQueryCacheTests
  34. @end
  35. /**
  36. * The tests for FSTLevelDBQueryCache are performed on the FSTQueryCache protocol in
  37. * FSTQueryCacheTests. This class is merely responsible for setting up and tearing down the
  38. * @a queryCache.
  39. */
  40. @implementation FSTLevelDBQueryCacheTests
  41. - (void)setUp {
  42. [super setUp];
  43. self.persistence = [FSTPersistenceTestHelpers levelDBPersistence];
  44. self.queryCache = [self.persistence queryCache];
  45. }
  46. - (void)tearDown {
  47. [super tearDown];
  48. self.persistence = nil;
  49. self.queryCache = nil;
  50. }
  51. - (void)testMetadataPersistedAcrossRestarts {
  52. [self.persistence shutdown];
  53. self.persistence = nil;
  54. NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
  55. FSTLevelDB *db1 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
  56. FSTLevelDBQueryCache *queryCache = [db1 queryCache];
  57. XCTAssertEqual(0, queryCache.highestListenSequenceNumber);
  58. XCTAssertEqual(0, queryCache.highestTargetID);
  59. SnapshotVersion versionZero;
  60. XCTAssertEqual(versionZero, queryCache.lastRemoteSnapshotVersion);
  61. FSTListenSequenceNumber minimumSequenceNumber = 1234;
  62. FSTTargetID lastTargetId = 5;
  63. SnapshotVersion lastVersion(Timestamp(1, 2));
  64. db1.run("add query data", [&]() {
  65. FSTQuery *query = [FSTQuery queryWithPath:ResourcePath{"some", "path"}];
  66. FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
  67. targetID:lastTargetId
  68. listenSequenceNumber:minimumSequenceNumber
  69. purpose:FSTQueryPurposeListen];
  70. [queryCache addQueryData:queryData];
  71. [queryCache setLastRemoteSnapshotVersion:lastVersion];
  72. });
  73. [db1 shutdown];
  74. db1 = nil;
  75. FSTLevelDB *db2 = [FSTPersistenceTestHelpers levelDBPersistenceWithDir:dir];
  76. db2.run("verify sequence number", [&]() {
  77. // We should remember the previous sequence number, and the next transaction should
  78. // have a higher one.
  79. XCTAssertGreaterThan(db2.currentSequenceNumber, minimumSequenceNumber);
  80. });
  81. FSTLevelDBQueryCache *queryCache2 = [db2 queryCache];
  82. XCTAssertEqual(lastTargetId, queryCache2.highestTargetID);
  83. XCTAssertEqual(lastVersion, queryCache2.lastRemoteSnapshotVersion);
  84. [db2 shutdown];
  85. db2 = nil;
  86. }
  87. @end
  88. NS_ASSUME_NONNULL_END