FSTPersistenceTestHelpers.mm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  17. #include <utility>
  18. #import "Firestore/Source/Local/FSTLevelDB.h"
  19. #import "Firestore/Source/Local/FSTLocalSerializer.h"
  20. #import "Firestore/Source/Local/FSTMemoryPersistence.h"
  21. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  22. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  23. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  24. #include "Firestore/core/src/firebase/firestore/util/path.h"
  25. #include "Firestore/core/src/firebase/firestore/util/status.h"
  26. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  27. namespace util = firebase::firestore::util;
  28. using firebase::firestore::model::DatabaseId;
  29. using firebase::firestore::util::Path;
  30. using firebase::firestore::util::Status;
  31. NS_ASSUME_NONNULL_BEGIN
  32. @implementation FSTPersistenceTestHelpers
  33. + (FSTLocalSerializer *)localSerializer {
  34. // This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
  35. static DatabaseId database_id{"p", "d"};
  36. FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&database_id];
  37. return [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
  38. }
  39. + (Path)levelDBDir {
  40. Path dir = util::TempDir().AppendUtf8("FSTPersistenceTestHelpers");
  41. // Delete the directory first to ensure isolation between runs.
  42. util::Status status = util::RecursivelyDelete(dir);
  43. if (!status.ok()) {
  44. [NSException
  45. raise:NSInternalInconsistencyException
  46. format:@"Failed to clean up leveldb path %s: %s", dir.c_str(), status.ToString().c_str()];
  47. }
  48. return dir;
  49. }
  50. + (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir {
  51. FSTLocalSerializer *serializer = [self localSerializer];
  52. FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:std::move(dir) serializer:serializer];
  53. Status status = [db start];
  54. if (!status.ok()) {
  55. [NSException raise:NSInternalInconsistencyException
  56. format:@"Failed to start leveldb persistence: %s", status.ToString().c_str()];
  57. }
  58. return db;
  59. }
  60. + (FSTLevelDB *)levelDBPersistence {
  61. return [self levelDBPersistenceWithDir:[self levelDBDir]];
  62. }
  63. + (FSTMemoryPersistence *)eagerGCMemoryPersistence {
  64. FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistenceWithEagerGC];
  65. Status status = [persistence start];
  66. if (!status.ok()) {
  67. [NSException raise:NSInternalInconsistencyException
  68. format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
  69. }
  70. return persistence;
  71. }
  72. + (FSTMemoryPersistence *)lruMemoryPersistence {
  73. FSTLocalSerializer *serializer = [self localSerializer];
  74. FSTMemoryPersistence *persistence =
  75. [FSTMemoryPersistence persistenceWithLRUGCAndSerializer:serializer];
  76. Status status = [persistence start];
  77. if (!status.ok()) {
  78. [NSException raise:NSInternalInconsistencyException
  79. format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
  80. }
  81. return persistence;
  82. }
  83. @end
  84. NS_ASSUME_NONNULL_END