RLMSyncTestCase.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMMultiProcessTestCase.h"
  19. #import "RLMSyncConfiguration_Private.h"
  20. typedef void(^RLMSyncBasicErrorReportingBlock)(NSError * _Nullable);
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface RLMSyncManager ()
  23. - (void)setSessionCompletionNotifier:(nullable RLMSyncBasicErrorReportingBlock)sessionCompletionNotifier;
  24. @end
  25. @interface SyncObject : RLMObject
  26. @property NSString *stringProp;
  27. @end
  28. @interface HugeSyncObject : RLMObject
  29. @property NSData *dataProp;
  30. + (instancetype)object;
  31. @end
  32. @interface RLMSyncTestCase : RLMMultiProcessTestCase
  33. + (NSURL *)authServerURL;
  34. + (NSURL *)secureAuthServerURL;
  35. + (RLMSyncCredentials *)basicCredentialsWithName:(NSString *)name register:(BOOL)shouldRegister;
  36. + (NSURL *)onDiskPathForSyncedRealm:(RLMRealm *)realm;
  37. /// Retrieve the administrator token.
  38. - (NSString *)adminToken;
  39. /// Read and delete the last email sent by ROS to the given address.
  40. /// Returns nil if none has been sent to that address.
  41. - (nullable NSString *)emailForAddress:(NSString *)email;
  42. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  43. - (RLMRealm *)openRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  44. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  45. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration;
  46. /// Synchronously open a synced Realm. Also run a block right after the Realm is created.
  47. - (RLMRealm *)openRealmForURL:(NSURL *)url
  48. user:(RLMSyncUser *)user
  49. immediatelyBlock:(nullable void(^)(void))block;
  50. /// Synchronously open a synced Realm with encryption key and stop policy.
  51. /// Also run a block right after the Realm is created.
  52. - (RLMRealm *)openRealmForURL:(NSURL *)url
  53. user:(RLMSyncUser *)user
  54. encryptionKey:(nullable NSData *)encryptionKey
  55. stopPolicy:(RLMSyncStopPolicy)stopPolicy
  56. immediatelyBlock:(nullable void(^)(void))block;
  57. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  58. /// Also run a block right after the Realm is created.
  59. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration
  60. immediatelyBlock:(nullable void(^)(void))block;
  61. ;
  62. /// Immediately open a synced Realm.
  63. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  64. /// Immediately open a synced Realm with encryption key and stop policy.
  65. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url
  66. user:(RLMSyncUser *)user
  67. encryptionKey:(nullable NSData *)encryptionKey
  68. stopPolicy:(RLMSyncStopPolicy)stopPolicy;
  69. /// Synchronously create, log in, and return a user.
  70. - (RLMSyncUser *)logInUserForCredentials:(RLMSyncCredentials *)credentials
  71. server:(NSURL *)url;
  72. /// Create and log in an admin user.
  73. - (RLMSyncUser *)createAdminUserForURL:(NSURL *)url username:(NSString *)username;
  74. /// Add a number of objects to a Realm.
  75. - (void)addSyncObjectsToRealm:(RLMRealm *)realm descriptions:(NSArray<NSString *> *)descriptions;
  76. /// Synchronously wait for downloads to complete for any number of Realms, and then check their `SyncObject` counts.
  77. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  78. realms:(NSArray<RLMRealm *> *)realms
  79. realmURLs:(NSArray<NSURL *> *)realmURLs
  80. expectedCounts:(NSArray<NSNumber *> *)counts;
  81. /// "Prime" the sync manager to signal the given semaphore the next time a session is bound. This method should be
  82. /// called right before a Realm is opened if that Realm's session is the one to be monitored.
  83. - (void)primeSyncManagerWithSemaphore:(nullable dispatch_semaphore_t)semaphore;
  84. /// Wait for downloads to complete; drop any error.
  85. - (void)waitForDownloadsForRealm:(RLMRealm *)realm;
  86. - (void)waitForDownloadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  87. /// Wait for uploads to complete; drop any error.
  88. - (void)waitForUploadsForRealm:(RLMRealm *)realm;
  89. - (void)waitForUploadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  90. /// Wait for downloads to complete while spinning the runloop. This method uses expectations.
  91. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  92. url:(NSURL *)url
  93. expectation:(nullable XCTestExpectation *)expectation
  94. error:(NSError **)error;
  95. /// Manually set the refresh token for a user. Used for testing invalid token conditions.
  96. - (void)manuallySetRefreshTokenForUser:(RLMSyncUser *)user value:(NSString *)tokenValue;
  97. - (void)setupSyncManager;
  98. - (void)resetSyncManager;
  99. @end
  100. NS_ASSUME_NONNULL_END
  101. #define WAIT_FOR_SEMAPHORE(macro_semaphore, macro_timeout) \
  102. { \
  103. int64_t delay_in_ns = (int64_t)(macro_timeout * NSEC_PER_SEC); \
  104. BOOL sema_success = dispatch_semaphore_wait(macro_semaphore, dispatch_time(DISPATCH_TIME_NOW, delay_in_ns)) == 0; \
  105. XCTAssertTrue(sema_success, @"Semaphore timed out."); \
  106. }
  107. #define CHECK_COUNT(d_count, macro_object_type, macro_realm) \
  108. { \
  109. [macro_realm refresh]; \
  110. NSInteger c = [macro_object_type allObjectsInRealm:macro_realm].count; \
  111. NSString *w = self.isParent ? @"parent" : @"child"; \
  112. XCTAssert(d_count == c, @"Expected %@ items, but actually got %@ (%@)", @(d_count), @(c), w); \
  113. }