TimerTests.swift 2.8 KB

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