TimeoutTests.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2023, 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 Testing
  17. @testable import GRPCCore
  18. struct TimeoutTests {
  19. @Test("Initialize from invalid String value", arguments: ["", "H", "123", "100000000S", "123j"])
  20. func initFromStringWithInvalidValue(_ value: String) throws {
  21. #expect(Timeout(decoding: value) == nil)
  22. }
  23. @Test(
  24. "Initialize from String",
  25. arguments: [
  26. ("123H", .hours(123)),
  27. ("123M", .minutes(123)),
  28. ("123S", .seconds(123)),
  29. ("123m", .milliseconds(123)),
  30. ("123u", .microseconds(123)),
  31. ("123n", .nanoseconds(123)),
  32. ] as [(String, Duration)]
  33. )
  34. func initFromString(_ value: String, expected: Duration) throws {
  35. let timeout = try #require(Timeout(decoding: value))
  36. #expect(timeout.duration == expected)
  37. }
  38. @Test(
  39. "Initialize from Duration",
  40. arguments: [
  41. .hours(123),
  42. .minutes(43),
  43. .seconds(12345),
  44. .milliseconds(100),
  45. .microseconds(100),
  46. .nanoseconds(100),
  47. ] as [Duration]
  48. )
  49. func initFromDuration(_ value: Duration) {
  50. let timeout = Timeout(duration: value)
  51. #expect(timeout.duration == value)
  52. }
  53. @Test(
  54. "Initialize from Duration with loss of precision",
  55. arguments: [
  56. // 111,111,111 seconds / 60 = 1,851,851.85 minutes -rounding up-> 1,851,852 minutes * 60 = 111,111,120 seconds
  57. (.seconds(111_111_111), .minutes(1_851_852)),
  58. // 9,999,999,999 seconds / 60 = 166,666,666.65 minutes -rounding up->
  59. // 166,666,667 minutes / 60 = 2,777,777.78 hours -rounding up->
  60. // 2,777,778 hours * 60 -> 166,666,680 minutes * 60 = 10,000,000,800 seconds
  61. (.seconds(9_999_999_999 as Int64), .hours(2_777_778)),
  62. // The conversion from seconds to hours results in a number that still has
  63. // more than the maximum allowed 8 digits, so we must clamp it.
  64. // Make sure that `Timeout.maxAmount` is the amount used for the resulting timeout.
  65. (.seconds(999_999_999_999 as Int64), .hours(Timeout.maxAmount)),
  66. // We can't convert seconds to nanoseconds because that would require at least
  67. // 9 digits, and the maximum allowed is 8: we expect to simply drop the nanoseconds.
  68. (Duration(secondsComponent: 1, attosecondsComponent: Int64(1e11)), .seconds(1)),
  69. ] as [(Duration, Duration)]
  70. )
  71. func initFromDurationWithLossOfPrecision(original: Duration, rounded: Duration) {
  72. let timeout = Timeout(duration: original)
  73. #expect(timeout.duration == rounded)
  74. }
  75. }