FSTPersistenceTestHelpers.mm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 *)levelDBPersistence {
  42. // This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
  43. static DatabaseId database_id{"p", "d"};
  44. NSString *dir = [self levelDBDir];
  45. FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&database_id];
  46. FSTLocalSerializer *serializer =
  47. [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
  48. FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
  49. NSError *error;
  50. BOOL success = [db start:&error];
  51. if (!success) {
  52. [NSException raise:NSInternalInconsistencyException
  53. format:@"Failed to create leveldb path %@: %@", dir, error];
  54. }
  55. return db;
  56. }
  57. + (FSTMemoryPersistence *)memoryPersistence {
  58. NSError *error;
  59. FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistence];
  60. BOOL success = [persistence start:&error];
  61. if (!success) {
  62. [NSException raise:NSInternalInconsistencyException
  63. format:@"Failed to start memory persistence: %@", error];
  64. }
  65. return persistence;
  66. }
  67. @end
  68. NS_ASSUME_NONNULL_END