RLMSyncUser+ObjectServerTests.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "RLMSyncUser+ObjectServerTests.h"
  19. #import "RLMSyncSession_Private.hpp"
  20. #import "RLMRealmUtil.hpp"
  21. #import "sync/sync_session.hpp"
  22. using namespace realm;
  23. @implementation RLMSyncUser (ObjectServerTests)
  24. - (BOOL)waitForUploadToFinish:(NSURL *)url {
  25. const NSTimeInterval timeout = 20;
  26. dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  27. RLMSyncSession *session = [self sessionForURL:url];
  28. NSAssert(session, @"Cannot call with invalid URL");
  29. BOOL couldWait = [session waitForUploadCompletionOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
  30. callback:^(NSError *){
  31. dispatch_semaphore_signal(sema);
  32. }];
  33. if (!couldWait) {
  34. return NO;
  35. }
  36. return dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))) == 0;
  37. }
  38. - (BOOL)waitForDownloadToFinish:(NSURL *)url {
  39. const NSTimeInterval timeout = 20;
  40. dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  41. RLMSyncSession *session = [self sessionForURL:url];
  42. NSAssert(session, @"Cannot call with invalid URL");
  43. BOOL couldWait = [session waitForDownloadCompletionOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
  44. callback:^(NSError *){
  45. dispatch_semaphore_signal(sema);
  46. }];
  47. if (!couldWait) {
  48. return NO;
  49. }
  50. return dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC))) == 0;
  51. }
  52. - (void)simulateClientResetErrorForSession:(NSURL *)url {
  53. RLMSyncSession *session = [self sessionForURL:url];
  54. NSAssert(session, @"Cannot call with invalid URL");
  55. std::shared_ptr<SyncSession> raw_session = session->_session.lock();
  56. std::error_code code = std::error_code{
  57. static_cast<int>(realm::sync::ProtocolError::bad_client_file_ident),
  58. realm::sync::protocol_error_category()
  59. };
  60. SyncSession::OnlyForTesting::handle_error(*raw_session, {code, "Not a real error message", false});
  61. }
  62. @end