FSTLevelDBRemoteDocumentCacheTests.mm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <memory>
  17. #include <string>
  18. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  19. #import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
  20. #import "Firestore/Source/Local/FSTLevelDB.h"
  21. #include "Firestore/core/src/firebase/firestore/local/leveldb_remote_document_cache.h"
  22. #include "Firestore/core/src/firebase/firestore/local/remote_document_cache.h"
  23. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  24. #include "absl/memory/memory.h"
  25. #include "leveldb/db.h"
  26. NS_ASSUME_NONNULL_BEGIN
  27. using leveldb::WriteOptions;
  28. using firebase::firestore::local::LevelDbRemoteDocumentCache;
  29. using firebase::firestore::local::RemoteDocumentCache;
  30. using firebase::firestore::util::OrderedCode;
  31. // A dummy document value, useful for testing code that's known to examine only document keys.
  32. static const char *kDummy = "1";
  33. /**
  34. * The tests for FSTLevelDBRemoteDocumentCache are performed on the FSTRemoteDocumentCache
  35. * protocol in FSTRemoteDocumentCacheTests. This class is merely responsible for setting up and
  36. * tearing down the @a remoteDocumentCache.
  37. */
  38. @interface FSTLevelDBRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
  39. @end
  40. @implementation FSTLevelDBRemoteDocumentCacheTests {
  41. FSTLevelDB *_db;
  42. std::unique_ptr<LevelDbRemoteDocumentCache> _cache;
  43. }
  44. - (void)setUp {
  45. [super setUp];
  46. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  47. self.persistence = _db;
  48. HARD_ASSERT(!_cache, "Previous cache not torn down");
  49. _cache = absl::make_unique<LevelDbRemoteDocumentCache>(_db, _db.serializer);
  50. // Write a couple dummy rows that should appear before/after the remote_documents table to make
  51. // sure the tests are unaffected.
  52. [self writeDummyRowWithSegments:@[ @"remote_documentr", @"foo", @"bar" ]];
  53. [self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]];
  54. }
  55. - (RemoteDocumentCache *_Nullable)remoteDocumentCache {
  56. return _cache.get();
  57. }
  58. - (void)tearDown {
  59. [super tearDown];
  60. self.remoteDocumentCache = nil;
  61. self.persistence = nil;
  62. _cache.reset();
  63. _db = nil;
  64. }
  65. - (void)writeDummyRowWithSegments:(NSArray<NSString *> *)segments {
  66. std::string key;
  67. for (NSString *segment in segments) {
  68. OrderedCode::WriteString(&key, segment.UTF8String);
  69. }
  70. _db.ptr->Put(WriteOptions(), key, kDummy);
  71. }
  72. @end
  73. NS_ASSUME_NONNULL_END