MethodConfigsTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 GRPCCore
  17. import XCTest
  18. final class MethodConfigsTests: XCTestCase {
  19. func testGetConfigurationForKnownMethod() async throws {
  20. let policy = HedgingPolicy(
  21. maxAttempts: 10,
  22. hedgingDelay: .seconds(1),
  23. nonFatalStatusCodes: []
  24. )
  25. let defaultConfiguration = MethodConfig(names: [], executionPolicy: .hedge(policy))
  26. var configurations = MethodConfigs()
  27. configurations.setDefaultConfig(defaultConfiguration)
  28. let descriptor = MethodDescriptor(fullyQualifiedService: "test", method: "first")
  29. let retryPolicy = RetryPolicy(
  30. maxAttempts: 10,
  31. initialBackoff: .seconds(1),
  32. maxBackoff: .seconds(1),
  33. backoffMultiplier: 1.0,
  34. retryableStatusCodes: [.unavailable]
  35. )
  36. let overrideConfiguration = MethodConfig(names: [], executionPolicy: .retry(retryPolicy))
  37. configurations[descriptor] = overrideConfiguration
  38. XCTAssertEqual(configurations[descriptor], overrideConfiguration)
  39. }
  40. func testGetConfigurationForUnknownMethodButServiceOverride() {
  41. let policy = HedgingPolicy(
  42. maxAttempts: 10,
  43. hedgingDelay: .seconds(1),
  44. nonFatalStatusCodes: []
  45. )
  46. let defaultConfiguration = MethodConfig(names: [], executionPolicy: .hedge(policy))
  47. var configurations = MethodConfigs()
  48. configurations.setDefaultConfig(defaultConfiguration)
  49. let firstDescriptor = MethodDescriptor(fullyQualifiedService: "test", method: "")
  50. let retryPolicy = RetryPolicy(
  51. maxAttempts: 10,
  52. initialBackoff: .seconds(1),
  53. maxBackoff: .seconds(1),
  54. backoffMultiplier: 1.0,
  55. retryableStatusCodes: [.unavailable]
  56. )
  57. let overrideConfiguration = MethodConfig(names: [], executionPolicy: .retry(retryPolicy))
  58. configurations[firstDescriptor] = overrideConfiguration
  59. let secondDescriptor = MethodDescriptor(fullyQualifiedService: "test", method: "second")
  60. XCTAssertEqual(configurations[secondDescriptor], overrideConfiguration)
  61. }
  62. func testGetConfigurationForUnknownMethodDefaultValue() {
  63. let policy = HedgingPolicy(
  64. maxAttempts: 10,
  65. hedgingDelay: .seconds(1),
  66. nonFatalStatusCodes: []
  67. )
  68. let defaultConfiguration = MethodConfig(names: [], executionPolicy: .hedge(policy))
  69. var configurations = MethodConfigs()
  70. configurations.setDefaultConfig(defaultConfiguration)
  71. let firstDescriptor = MethodDescriptor(fullyQualifiedService: "test1", method: "first")
  72. let retryPolicy = RetryPolicy(
  73. maxAttempts: 10,
  74. initialBackoff: .seconds(1),
  75. maxBackoff: .seconds(1),
  76. backoffMultiplier: 1.0,
  77. retryableStatusCodes: [.unavailable]
  78. )
  79. let overrideConfiguration = MethodConfig(names: [], executionPolicy: .retry(retryPolicy))
  80. configurations[firstDescriptor] = overrideConfiguration
  81. let secondDescriptor = MethodDescriptor(fullyQualifiedService: "test2", method: "second")
  82. XCTAssertEqual(configurations[secondDescriptor], defaultConfiguration)
  83. }
  84. }