FSTPersistenceTestHelpers.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/Model/FSTDatabaseID.h"
  21. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation FSTPersistenceTestHelpers
  24. + (FSTLevelDB *)levelDBPersistence {
  25. NSError *error;
  26. NSFileManager *files = [NSFileManager defaultManager];
  27. NSString *dir =
  28. [NSTemporaryDirectory() stringByAppendingPathComponent:@"FSTPersistenceTestHelpers"];
  29. if ([files fileExistsAtPath:dir]) {
  30. // Delete the directory first to ensure isolation between runs.
  31. BOOL success = [files removeItemAtPath:dir error:&error];
  32. if (!success) {
  33. [NSException raise:NSInternalInconsistencyException
  34. format:@"Failed to clean up leveldb path %@: %@", dir, error];
  35. }
  36. }
  37. FSTDatabaseID *databaseID = [FSTDatabaseID databaseIDWithProject:@"p" database:@"d"];
  38. FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:databaseID];
  39. FSTLocalSerializer *serializer =
  40. [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
  41. FSTLevelDB *db = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
  42. BOOL success = [db start:&error];
  43. if (!success) {
  44. [NSException raise:NSInternalInconsistencyException
  45. format:@"Failed to create leveldb path %@: %@", dir, error];
  46. }
  47. return db;
  48. }
  49. + (FSTMemoryPersistence *)memoryPersistence {
  50. NSError *error;
  51. FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistence];
  52. BOOL success = [persistence start:&error];
  53. if (!success) {
  54. [NSException raise:NSInternalInconsistencyException
  55. format:@"Failed to start memory persistence: %@", error];
  56. }
  57. return persistence;
  58. }
  59. @end
  60. NS_ASSUME_NONNULL_END