RLMSyncSessionRefreshHandle+ObjectServerTests.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2017 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 "RLMSyncSessionRefreshHandle+ObjectServerTests.h"
  19. #import "RLMTestUtils.h"
  20. static BOOL s_calculateFireDatesWithTestLogic = NO;
  21. static void(^s_onRefreshCompletedOrErrored)(BOOL) = nil;
  22. @interface RLMSyncSessionRefreshHandle ()
  23. + (NSDate *)fireDateForTokenExpirationDate:(NSDate *)date nowDate:(NSDate *)date;
  24. - (BOOL)_onRefreshCompletionWithError:(NSError *)error json:(NSDictionary *)json;
  25. @end
  26. @implementation RLMSyncSessionRefreshHandle (ObjectServerTests)
  27. + (void)calculateFireDateUsingTestLogic:(BOOL)forTest blockOnRefreshCompletion:(void(^)(BOOL))block {
  28. s_onRefreshCompletedOrErrored = block;
  29. s_calculateFireDatesWithTestLogic = forTest;
  30. }
  31. + (void)load {
  32. RLMSwapOutClassMethod(self,
  33. @selector(fireDateForTokenExpirationDate:nowDate:),
  34. @selector(ost_fireDateForTokenExpirationDate:nowDate:));
  35. RLMSwapOutInstanceMethod(self,
  36. @selector(_onRefreshCompletionWithError:json:),
  37. @selector(ost_onRefreshCompletionWithError:json:));
  38. }
  39. + (NSDate *)ost_fireDateForTokenExpirationDate:(NSDate *)date nowDate:(NSDate *)nowDate {
  40. if (s_calculateFireDatesWithTestLogic) {
  41. // Force the refresh to take place one second later.
  42. return [NSDate dateWithTimeIntervalSinceNow:1];
  43. } else {
  44. // Use the original logic.
  45. return [self ost_fireDateForTokenExpirationDate:date nowDate:nowDate];
  46. }
  47. }
  48. - (BOOL)ost_onRefreshCompletionWithError:(NSError *)error json:(NSDictionary *)json {
  49. BOOL status = [self ost_onRefreshCompletionWithError:error json:json];
  50. // For the sake of testing, call a callback afterwards to let the test update its state.
  51. if (s_onRefreshCompletedOrErrored) {
  52. s_onRefreshCompletedOrErrored(status);
  53. }
  54. return status;
  55. }
  56. @end