TimerTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 GRPCCore
  17. import GRPCNIOTransportCore
  18. import NIOCore
  19. import NIOEmbedded
  20. import Synchronization
  21. import XCTest
  22. internal final class TimerTests: XCTestCase {
  23. fileprivate struct CounterTimerHandler: NIOScheduledCallbackHandler {
  24. let counter = AtomicCounter(0)
  25. func handleScheduledCallback(eventLoop: some EventLoop) {
  26. counter.increment()
  27. }
  28. }
  29. func testOneOffTimer() {
  30. let loop = EmbeddedEventLoop()
  31. defer { try! loop.close() }
  32. let handler = CounterTimerHandler()
  33. let timer = Timer(eventLoop: loop, duration: .seconds(1), repeating: false, handler: handler)
  34. timer.start()
  35. // Timer hasn't fired because we haven't reached the required duration.
  36. loop.advanceTime(by: .milliseconds(999))
  37. XCTAssertEqual(handler.counter.value, 0)
  38. // Timer has fired once.
  39. loop.advanceTime(by: .milliseconds(1))
  40. XCTAssertEqual(handler.counter.value, 1)
  41. // Timer does not repeat.
  42. loop.advanceTime(by: .seconds(1))
  43. XCTAssertEqual(handler.counter.value, 1)
  44. // Timer can be restarted and then fires again after the duration.
  45. timer.start()
  46. loop.advanceTime(by: .seconds(1))
  47. XCTAssertEqual(handler.counter.value, 2)
  48. // Timer can be cancelled before the duration and then does not fire.
  49. timer.start()
  50. loop.advanceTime(by: .milliseconds(999))
  51. timer.cancel()
  52. loop.advanceTime(by: .milliseconds(1))
  53. XCTAssertEqual(handler.counter.value, 2)
  54. // Timer can be restarted after being cancelled.
  55. timer.start()
  56. loop.advanceTime(by: .seconds(1))
  57. XCTAssertEqual(handler.counter.value, 3)
  58. }
  59. func testRepeatedTimer() {
  60. let loop = EmbeddedEventLoop()
  61. defer { try! loop.close() }
  62. let handler = CounterTimerHandler()
  63. let timer = Timer(eventLoop: loop, duration: .seconds(1), repeating: true, handler: handler)
  64. timer.start()
  65. // Timer hasn't fired because we haven't reached the required duration.
  66. loop.advanceTime(by: .milliseconds(999))
  67. XCTAssertEqual(handler.counter.value, 0)
  68. // Timer has fired once.
  69. loop.advanceTime(by: .milliseconds(1))
  70. XCTAssertEqual(handler.counter.value, 1)
  71. // Timer hasn't fired again because we haven't reached the required duration again.
  72. loop.advanceTime(by: .milliseconds(999))
  73. XCTAssertEqual(handler.counter.value, 1)
  74. // Timer has fired again.
  75. loop.advanceTime(by: .milliseconds(1))
  76. XCTAssertEqual(handler.counter.value, 2)
  77. // Timer continues to fire on each second.
  78. loop.advanceTime(by: .seconds(1))
  79. XCTAssertEqual(handler.counter.value, 3)
  80. loop.advanceTime(by: .seconds(1))
  81. XCTAssertEqual(handler.counter.value, 4)
  82. loop.advanceTime(by: .seconds(1))
  83. XCTAssertEqual(handler.counter.value, 5)
  84. loop.advanceTime(by: .seconds(5))
  85. XCTAssertEqual(handler.counter.value, 10)
  86. // Timer does not fire again, after being cancelled.
  87. timer.cancel()
  88. loop.advanceTime(by: .seconds(5))
  89. XCTAssertEqual(handler.counter.value, 10)
  90. // Timer can be restarted after being cancelled and continues to fire once per second.
  91. timer.start()
  92. loop.advanceTime(by: .seconds(5))
  93. XCTAssertEqual(handler.counter.value, 15)
  94. }
  95. }