GRPCTimeoutTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2019, 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 Dispatch
  17. import Foundation
  18. @testable import GRPC
  19. import NIOCore
  20. import XCTest
  21. class GRPCTimeoutTests: GRPCTestCase {
  22. func testRoundingNegativeTimeout() {
  23. let timeout = GRPCTimeout(rounding: -10, unit: .seconds)
  24. XCTAssertEqual(String(describing: timeout), "0S")
  25. XCTAssertEqual(timeout.nanoseconds, 0)
  26. }
  27. func testRoundingNanosecondsTimeout() throws {
  28. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .nanoseconds)
  29. XCTAssertEqual(timeout, GRPCTimeout(amount: 123_457, unit: .microseconds))
  30. // 123_456_789 (nanoseconds) / 1_000
  31. // = 123_456.789
  32. // = 123_457 (microseconds, rounded up)
  33. XCTAssertEqual(String(describing: timeout), "123457u")
  34. // 123_457 (microseconds) * 1_000
  35. // = 123_457_000 (nanoseconds)
  36. XCTAssertEqual(timeout.nanoseconds, 123_457_000)
  37. }
  38. func testRoundingMicrosecondsTimeout() throws {
  39. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .microseconds)
  40. XCTAssertEqual(timeout, GRPCTimeout(amount: 123_457, unit: .milliseconds))
  41. // 123_456_789 (microseconds) / 1_000
  42. // = 123_456.789
  43. // = 123_457 (milliseconds, rounded up)
  44. XCTAssertEqual(String(describing: timeout), "123457m")
  45. // 123_457 (milliseconds) * 1_000 * 1_000
  46. // = 123_457_000_000 (nanoseconds)
  47. XCTAssertEqual(timeout.nanoseconds, 123_457_000_000)
  48. }
  49. func testRoundingMillisecondsTimeout() throws {
  50. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .milliseconds)
  51. XCTAssertEqual(timeout, GRPCTimeout(amount: 123_457, unit: .seconds))
  52. // 123_456_789 (milliseconds) / 1_000
  53. // = 123_456.789
  54. // = 123_457 (seconds, rounded up)
  55. XCTAssertEqual(String(describing: timeout), "123457S")
  56. // 123_457 (milliseconds) * 1_000 * 1_000 * 1_000
  57. // = 123_457_000_000_000 (nanoseconds)
  58. XCTAssertEqual(timeout.nanoseconds, 123_457_000_000_000)
  59. }
  60. func testRoundingSecondsTimeout() throws {
  61. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .seconds)
  62. XCTAssertEqual(timeout, GRPCTimeout(amount: 2_057_614, unit: .minutes))
  63. // 123_456_789 (seconds) / 60
  64. // = 2_057_613.15
  65. // = 2_057_614 (minutes, rounded up)
  66. XCTAssertEqual(String(describing: timeout), "2057614M")
  67. // 2_057_614 (minutes) * 60 * 1_000 * 1_000 * 1_000
  68. // = 123_456_840_000_000_000 (nanoseconds)
  69. XCTAssertEqual(timeout.nanoseconds, 123_456_840_000_000_000)
  70. }
  71. func testRoundingMinutesTimeout() throws {
  72. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .minutes)
  73. XCTAssertEqual(timeout, GRPCTimeout(amount: 2_057_614, unit: .hours))
  74. // 123_456_789 (minutes) / 60
  75. // = 2_057_613.15
  76. // = 2_057_614 (hours, rounded up)
  77. XCTAssertEqual(String(describing: timeout), "2057614H")
  78. // 123_457 (minutes) * 60 * 60 * 1_000 * 1_000 * 1_000
  79. // = 7_407_410_400_000_000_000 (nanoseconds)
  80. XCTAssertEqual(timeout.nanoseconds, 7_407_410_400_000_000_000)
  81. }
  82. func testRoundingHoursTimeout() throws {
  83. let timeout = GRPCTimeout(rounding: 123_456_789, unit: .hours)
  84. XCTAssertEqual(timeout, GRPCTimeout(amount: 99_999_999, unit: .hours))
  85. // Hours are the largest unit of time we have (as per the gRPC spec) so we can't round to a
  86. // different unit. In this case we clamp to the largest value.
  87. XCTAssertEqual(String(describing: timeout), "99999999H")
  88. // Unfortunately the largest value representable by the specification is too long to represent
  89. // in nanoseconds within 64 bits, again the value is clamped.
  90. XCTAssertEqual(timeout.nanoseconds, Int64.max)
  91. }
  92. func testTimeoutFromDeadline() throws {
  93. let deadline = NIODeadline.uptimeNanoseconds(0) + .seconds(42)
  94. let timeout = GRPCTimeout(deadline: deadline, testingOnlyNow: .uptimeNanoseconds(0))
  95. XCTAssertEqual(timeout.nanoseconds, 42_000_000_000)
  96. // Wire encoding may have at most 8 digits, we should automatically coarsen the resolution until
  97. // we're within that limit.
  98. XCTAssertEqual(timeout.wireEncoding, "42000000u")
  99. }
  100. func testTimeoutFromPastDeadline() throws {
  101. let deadline = NIODeadline.uptimeNanoseconds(100) + .nanoseconds(50)
  102. // testingOnlyNow >= deadline: timeout should be zero.
  103. let timeout = GRPCTimeout(deadline: deadline, testingOnlyNow: .uptimeNanoseconds(200))
  104. XCTAssertEqual(timeout.nanoseconds, 0)
  105. }
  106. func testTimeoutFromDistantFuture() throws {
  107. XCTAssertEqual(GRPCTimeout(deadline: .distantFuture), .infinite)
  108. }
  109. }