GRPCTimeoutTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Foundation
  17. import GRPC
  18. import XCTest
  19. class GRPCTimeoutTests: GRPCTestCase {
  20. func testNegativeTimeoutThrows() throws {
  21. XCTAssertThrowsError(try GRPCTimeout.seconds(-10)) { error in
  22. XCTAssertEqual(error as? GRPCTimeoutError, GRPCTimeoutError.negative)
  23. }
  24. }
  25. func testTooLargeTimeout() throws {
  26. XCTAssertThrowsError(try GRPCTimeout.seconds(100_000_000)) { error in
  27. XCTAssertEqual(error as? GRPCTimeoutError, GRPCTimeoutError.tooManyDigits)
  28. }
  29. }
  30. func testRoundingNegativeTimeout() {
  31. let timeout: GRPCTimeout = .seconds(rounding: -10)
  32. XCTAssertEqual(String(describing: timeout), "0S")
  33. XCTAssertEqual(timeout.nanoseconds, 0)
  34. }
  35. func testRoundingNanosecondsTimeout() throws {
  36. let timeout: GRPCTimeout = .nanoseconds(rounding: 123_456_789)
  37. XCTAssertEqual(timeout, try .microseconds(123457))
  38. // 123_456_789 (nanoseconds) / 1_000
  39. // = 123_456.789
  40. // = 123_457 (microseconds, rounded up)
  41. XCTAssertEqual(String(describing: timeout), "123457u")
  42. // 123_457 (microseconds) * 1_000
  43. // = 123_457_000 (nanoseconds)
  44. XCTAssertEqual(timeout.nanoseconds, 123_457_000)
  45. }
  46. func testRoundingMicrosecondsTimeout() throws {
  47. let timeout: GRPCTimeout = .microseconds(rounding: 123_456_789)
  48. XCTAssertEqual(timeout, try .milliseconds(123457))
  49. // 123_456_789 (microseconds) / 1_000
  50. // = 123_456.789
  51. // = 123_457 (milliseconds, rounded up)
  52. XCTAssertEqual(String(describing: timeout), "123457m")
  53. // 123_457 (milliseconds) * 1_000 * 1_000
  54. // = 123_457_000_000 (nanoseconds)
  55. XCTAssertEqual(timeout.nanoseconds, 123_457_000_000)
  56. }
  57. func testRoundingMillisecondsTimeout() throws {
  58. let timeout: GRPCTimeout = .milliseconds(rounding: 123_456_789)
  59. XCTAssertEqual(timeout, try .seconds(123457))
  60. // 123_456_789 (milliseconds) / 1_000
  61. // = 123_456.789
  62. // = 123_457 (seconds, rounded up)
  63. XCTAssertEqual(String(describing: timeout), "123457S")
  64. // 123_457 (milliseconds) * 1_000 * 1_000 * 1_000
  65. // = 123_457_000_000_000 (nanoseconds)
  66. XCTAssertEqual(timeout.nanoseconds, 123_457_000_000_000)
  67. }
  68. func testRoundingSecondsTimeout() throws {
  69. let timeout: GRPCTimeout = .seconds(rounding: 123_456_789)
  70. XCTAssertEqual(timeout, try .minutes(2057614))
  71. // 123_456_789 (seconds) / 60
  72. // = 2_057_613.15
  73. // = 2_057_614 (minutes, rounded up)
  74. XCTAssertEqual(String(describing: timeout), "2057614M")
  75. // 2_057_614 (minutes) * 60 * 1_000 * 1_000 * 1_000
  76. // = 123_456_840_000_000_000 (nanoseconds)
  77. XCTAssertEqual(timeout.nanoseconds, 123_456_840_000_000_000)
  78. }
  79. func testRoundingMinutesTimeout() throws {
  80. let timeout: GRPCTimeout = .minutes(rounding: 123_456_789)
  81. XCTAssertEqual(timeout, try .hours(2057614))
  82. // 123_456_789 (minutes) / 60
  83. // = 2_057_613.15
  84. // = 2_057_614 (hours, rounded up)
  85. XCTAssertEqual(String(describing: timeout), "2057614H")
  86. // 123_457 (minutes) * 60 * 60 * 1_000 * 1_000 * 1_000
  87. // = 7_407_410_400_000_000_000 (nanoseconds)
  88. XCTAssertEqual(timeout.nanoseconds, 7_407_410_400_000_000_000)
  89. }
  90. func testRoundingHoursTimeout() throws {
  91. let timeout: GRPCTimeout = .hours(rounding: 123_456_789)
  92. XCTAssertEqual(timeout, try .hours(99_999_999))
  93. // Hours are the largest unit of time we have (as per the gRPC spec) so we can't round to a
  94. // different unit. In this case we clamp to the largest value.
  95. XCTAssertEqual(String(describing: timeout), "99999999H")
  96. // Unfortunately the largest value representable by the specification is too long to represent
  97. // in nanoseconds within 64 bits, again the value is clamped.
  98. XCTAssertEqual(timeout.nanoseconds, Int64.max)
  99. }
  100. }