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