TimerTests.swift 3.7 KB

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