FSTPersistenceTestHelpers.mm 3.2 KB

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