TimerTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  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 Atomics
  17. import NIOEmbedded
  18. import XCTest
  19. @testable import GRPCHTTP2Core
  20. internal final class TimerTests: XCTestCase {
  21. func testScheduleOneOffTimer() {
  22. let loop = EmbeddedEventLoop()
  23. defer { try! loop.close() }
  24. var value = 0
  25. var timer = Timer(delay: .seconds(1), repeat: false)
  26. timer.schedule(on: loop) {
  27. XCTAssertEqual(value, 0)
  28. value += 1
  29. }
  30. loop.advanceTime(by: .milliseconds(999))
  31. XCTAssertEqual(value, 0)
  32. loop.advanceTime(by: .milliseconds(1))
  33. XCTAssertEqual(value, 1)
  34. // Run again to make sure the task wasn't repeated.
  35. loop.advanceTime(by: .seconds(1))
  36. XCTAssertEqual(value, 1)
  37. }
  38. func testCancelOneOffTimer() {
  39. let loop = EmbeddedEventLoop()
  40. defer { try! loop.close() }
  41. var timer = Timer(delay: .seconds(1), repeat: false)
  42. timer.schedule(on: loop) {
  43. XCTFail("Timer wasn't cancelled")
  44. }
  45. loop.advanceTime(by: .milliseconds(999))
  46. timer.cancel()
  47. loop.advanceTime(by: .milliseconds(1))
  48. }
  49. func testScheduleRepeatedTimer() throws {
  50. let loop = EmbeddedEventLoop()
  51. defer { try! loop.close() }
  52. var values = [Int]()
  53. var timer = Timer(delay: .seconds(1), repeat: true)
  54. timer.schedule(on: loop) {
  55. values.append(values.count)
  56. }
  57. loop.advanceTime(by: .milliseconds(999))
  58. XCTAssertEqual(values, [])
  59. loop.advanceTime(by: .milliseconds(1))
  60. XCTAssertEqual(values, [0])
  61. loop.advanceTime(by: .seconds(1))
  62. XCTAssertEqual(values, [0, 1])
  63. loop.advanceTime(by: .seconds(1))
  64. XCTAssertEqual(values, [0, 1, 2])
  65. timer.cancel()
  66. loop.advanceTime(by: .seconds(1))
  67. XCTAssertEqual(values, [0, 1, 2])
  68. }
  69. func testCancelRepeatedTimer() {
  70. let loop = EmbeddedEventLoop()
  71. defer { try! loop.close() }
  72. var timer = Timer(delay: .seconds(1), repeat: true)
  73. timer.schedule(on: loop) {
  74. XCTFail("Timer wasn't cancelled")
  75. }
  76. loop.advanceTime(by: .milliseconds(999))
  77. timer.cancel()
  78. loop.advanceTime(by: .milliseconds(1))
  79. }
  80. }