TimeoutTests.swift 3.1 KB

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