TimerTests.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 NIOEmbedded
  19. import Synchronization
  20. import XCTest
  21. internal final class TimerTests: XCTestCase {
  22. func testScheduleOneOffTimer() {
  23. let loop = EmbeddedEventLoop()
  24. defer { try! loop.close() }
  25. let value = Atomic(0)
  26. var timer = Timer(delay: .seconds(1), repeat: false)
  27. timer.schedule(on: loop) {
  28. let (old, _) = value.add(1, ordering: .releasing)
  29. XCTAssertEqual(old, 0)
  30. }
  31. loop.advanceTime(by: .milliseconds(999))
  32. XCTAssertEqual(value.load(ordering: .acquiring), 0)
  33. loop.advanceTime(by: .milliseconds(1))
  34. XCTAssertEqual(value.load(ordering: .acquiring), 1)
  35. // Run again to make sure the task wasn't repeated.
  36. loop.advanceTime(by: .seconds(1))
  37. XCTAssertEqual(value.load(ordering: .acquiring), 1)
  38. }
  39. func testCancelOneOffTimer() {
  40. let loop = EmbeddedEventLoop()
  41. defer { try! loop.close() }
  42. var timer = Timer(delay: .seconds(1), repeat: false)
  43. timer.schedule(on: loop) {
  44. XCTFail("Timer wasn't cancelled")
  45. }
  46. loop.advanceTime(by: .milliseconds(999))
  47. timer.cancel()
  48. loop.advanceTime(by: .milliseconds(1))
  49. }
  50. func testScheduleRepeatedTimer() throws {
  51. let loop = EmbeddedEventLoop()
  52. defer { try! loop.close() }
  53. let counter = AtomicCounter()
  54. var timer = Timer(delay: .seconds(1), repeat: true)
  55. timer.schedule(on: loop) {
  56. counter.increment()
  57. }
  58. loop.advanceTime(by: .milliseconds(999))
  59. XCTAssertEqual(counter.value, 0)
  60. loop.advanceTime(by: .milliseconds(1))
  61. XCTAssertEqual(counter.value, 1)
  62. loop.advanceTime(by: .seconds(1))
  63. XCTAssertEqual(counter.value, 2)
  64. loop.advanceTime(by: .seconds(1))
  65. XCTAssertEqual(counter.value, 3)
  66. timer.cancel()
  67. loop.advanceTime(by: .seconds(1))
  68. XCTAssertEqual(counter.value, 3)
  69. }
  70. func testCancelRepeatedTimer() {
  71. let loop = EmbeddedEventLoop()
  72. defer { try! loop.close() }
  73. var timer = Timer(delay: .seconds(1), repeat: true)
  74. timer.schedule(on: loop) {
  75. XCTFail("Timer wasn't cancelled")
  76. }
  77. loop.advanceTime(by: .milliseconds(999))
  78. timer.cancel()
  79. loop.advanceTime(by: .milliseconds(1))
  80. }
  81. }