RLMSyncTestCase.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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:(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. + (RLMSyncManager *)managerForCurrentTest;
  34. + (NSURL *)authServerURL;
  35. + (NSURL *)secureAuthServerURL;
  36. + (RLMSyncCredentials *)basicCredentialsWithName:(NSString *)name register:(BOOL)shouldRegister;
  37. + (NSURL *)onDiskPathForSyncedRealm:(RLMRealm *)realm;
  38. /// Retrieve the administrator token.
  39. - (NSString *)adminToken;
  40. /// Read and delete the last email sent by ROS to the given address.
  41. /// Returns nil if none has been sent to that address.
  42. - (nullable NSString *)emailForAddress:(NSString *)email;
  43. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  44. - (RLMRealm *)openRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  45. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  46. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration;
  47. /// Synchronously open a synced Realm. Also run a block right after the Realm is created.
  48. - (RLMRealm *)openRealmForURL:(NSURL *)url
  49. user:(RLMSyncUser *)user
  50. immediatelyBlock:(nullable void(^)(void))block;
  51. /// Synchronously open a synced Realm with encryption key and stop policy.
  52. /// Also run a block right after the Realm is created.
  53. - (RLMRealm *)openRealmForURL:(NSURL *)url
  54. user:(RLMSyncUser *)user
  55. encryptionKey:(nullable NSData *)encryptionKey
  56. stopPolicy:(RLMSyncStopPolicy)stopPolicy
  57. immediatelyBlock:(nullable void(^)(void))block;
  58. /// Synchronously open a synced Realm and wait until the binding process has completed or failed.
  59. /// Also run a block right after the Realm is created.
  60. - (RLMRealm *)openRealmWithConfiguration:(RLMRealmConfiguration *)configuration
  61. immediatelyBlock:(nullable void(^)(void))block;
  62. ;
  63. /// Immediately open a synced Realm.
  64. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url user:(RLMSyncUser *)user;
  65. /// Immediately open a synced Realm with encryption key and stop policy.
  66. - (RLMRealm *)immediatelyOpenRealmForURL:(NSURL *)url
  67. user:(RLMSyncUser *)user
  68. encryptionKey:(nullable NSData *)encryptionKey
  69. stopPolicy:(RLMSyncStopPolicy)stopPolicy;
  70. /// Synchronously create, log in, and return a user.
  71. - (RLMSyncUser *)logInUserForCredentials:(RLMSyncCredentials *)credentials
  72. server:(NSURL *)url;
  73. /// Create and log in an admin user.
  74. - (RLMSyncUser *)createAdminUserForURL:(NSURL *)url username:(NSString *)username;
  75. /// Add a number of objects to a Realm.
  76. - (void)addSyncObjectsToRealm:(RLMRealm *)realm descriptions:(NSArray<NSString *> *)descriptions;
  77. /// Synchronously wait for downloads to complete for any number of Realms, and then check their `SyncObject` counts.
  78. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  79. realms:(NSArray<RLMRealm *> *)realms
  80. realmURLs:(NSArray<NSURL *> *)realmURLs
  81. expectedCounts:(NSArray<NSNumber *> *)counts;
  82. /// "Prime" the sync manager to signal the given semaphore the next time a session is bound. This method should be
  83. /// called right before a Realm is opened if that Realm's session is the one to be monitored.
  84. - (void)primeSyncManagerWithSemaphore:(nullable dispatch_semaphore_t)semaphore;
  85. /// Wait for downloads to complete; drop any error.
  86. - (void)waitForDownloadsForRealm:(RLMRealm *)realm;
  87. - (void)waitForDownloadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  88. /// Wait for uploads to complete; drop any error.
  89. - (void)waitForUploadsForRealm:(RLMRealm *)realm;
  90. - (void)waitForUploadsForRealm:(RLMRealm *)realm error:(NSError **)error;
  91. /// Wait for downloads to complete while spinning the runloop. This method uses expectations.
  92. - (void)waitForDownloadsForUser:(RLMSyncUser *)user
  93. url:(NSURL *)url
  94. expectation:(nullable XCTestExpectation *)expectation
  95. error:(NSError **)error;
  96. /// Manually set the refresh token for a user. Used for testing invalid token conditions.
  97. - (void)manuallySetRefreshTokenForUser:(RLMSyncUser *)user value:(NSString *)tokenValue;
  98. @end
  99. NS_ASSUME_NONNULL_END
  100. #define WAIT_FOR_SEMAPHORE(macro_semaphore, macro_timeout) \
  101. { \
  102. int64_t delay_in_ns = (int64_t)(macro_timeout * NSEC_PER_SEC); \
  103. BOOL sema_success = dispatch_semaphore_wait(macro_semaphore, dispatch_time(DISPATCH_TIME_NOW, delay_in_ns)) == 0; \
  104. XCTAssertTrue(sema_success, @"Semaphore timed out."); \
  105. }
  106. #define CHECK_COUNT(d_count, macro_object_type, macro_realm) \
  107. { \
  108. [macro_realm refresh]; \
  109. NSInteger c = [macro_object_type allObjectsInRealm:macro_realm].count; \
  110. NSString *w = self.isParent ? @"parent" : @"child"; \
  111. XCTAssert(d_count == c, @"Expected %@ items, but actually got %@ (%@)", @(d_count), @(c), w); \
  112. }