FIRAppCheckTimerTests.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2021 Google LLC
  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 <XCTest/XCTest.h>
  17. #import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTimer.h"
  18. @interface FIRAppCheckTimerTests : XCTestCase
  19. @end
  20. @implementation FIRAppCheckTimerTests
  21. - (void)testTimerProvider {
  22. dispatch_queue_t queue =
  23. dispatch_queue_create("FIRAppCheckTimerTests.testInit", DISPATCH_QUEUE_SERIAL);
  24. NSTimeInterval fireTimerIn = 1;
  25. NSDate *startTime = [NSDate date];
  26. NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:fireTimerIn];
  27. FIRTimerProvider timerProvider = [FIRAppCheckTimer timerProvider];
  28. XCTestExpectation *timerExpectation = [self expectationWithDescription:@"timer"];
  29. FIRAppCheckTimer *timer = timerProvider(fireDate, queue, ^{
  30. NSTimeInterval actuallyFiredIn = [[NSDate date] timeIntervalSinceDate:startTime];
  31. // Check that fired at proper time (allowing some timer drift).
  32. XCTAssertLessThan(ABS(actuallyFiredIn - fireTimerIn), 0.5);
  33. [timerExpectation fulfill];
  34. });
  35. XCTAssertNotNil(timer);
  36. [self waitForExpectations:@[ timerExpectation ] timeout:fireTimerIn + 1];
  37. }
  38. - (void)testInit {
  39. dispatch_queue_t queue =
  40. dispatch_queue_create("FIRAppCheckTimerTests.testInit", DISPATCH_QUEUE_SERIAL);
  41. NSTimeInterval fireTimerIn = 2;
  42. NSDate *startTime = [NSDate date];
  43. NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:fireTimerIn];
  44. XCTestExpectation *timerExpectation = [self expectationWithDescription:@"timer"];
  45. FIRAppCheckTimer *timer = [[FIRAppCheckTimer alloc]
  46. initWithFireDate:fireDate
  47. dispatchQueue:queue
  48. block:^{
  49. NSTimeInterval actuallyFiredIn = [[NSDate date] timeIntervalSinceDate:startTime];
  50. // Check that fired at proper time (allowing some timer drift).
  51. XCTAssertLessThan(ABS(actuallyFiredIn - fireTimerIn), 0.5);
  52. [timerExpectation fulfill];
  53. }];
  54. XCTAssertNotNil(timer);
  55. [self waitForExpectations:@[ timerExpectation ] timeout:fireTimerIn + 1];
  56. }
  57. @end