Metadata+GRPCTests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 XCTest
  17. @testable import GRPCCore
  18. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  19. final class MetadataGRPCTests: XCTestCase {
  20. func testPreviousRPCAttemptsValidValues() {
  21. let testData = [("0", 0), ("1", 1), ("-1", -1)]
  22. for (value, expected) in testData {
  23. let metadata: Metadata = ["grpc-previous-rpc-attempts": "\(value)"]
  24. XCTAssertEqual(metadata.previousRPCAttempts, expected)
  25. }
  26. }
  27. func testPreviousRPCAttemptsInvalidValues() {
  28. let values = ["foo", "42.0"]
  29. for value in values {
  30. let metadata: Metadata = ["grpc-previous-rpc-attempts": "\(value)"]
  31. XCTAssertNil(metadata.previousRPCAttempts)
  32. }
  33. }
  34. func testSetPreviousRPCAttemptsToValue() {
  35. var metadata: Metadata = [:]
  36. metadata.previousRPCAttempts = 42
  37. XCTAssertEqual(metadata, ["grpc-previous-rpc-attempts": "42"])
  38. metadata.previousRPCAttempts = nil
  39. XCTAssertEqual(metadata, [:])
  40. for i in 0 ..< 5 {
  41. metadata.addString("\(i)", forKey: "grpc-previous-rpc-attempts")
  42. }
  43. XCTAssertEqual(metadata.count, 5)
  44. // Should remove old values.
  45. metadata.previousRPCAttempts = 42
  46. XCTAssertEqual(metadata, ["grpc-previous-rpc-attempts": "42"])
  47. }
  48. func testRetryPushbackValidDelay() {
  49. let testData: [(String, Duration)] = [
  50. ("0", .zero),
  51. ("1", Duration(secondsComponent: 0, attosecondsComponent: 1_000_000_000_000_000)),
  52. ("999", Duration(secondsComponent: 0, attosecondsComponent: 999_000_000_000_000_000)),
  53. ("1000", Duration(secondsComponent: 1, attosecondsComponent: 0)),
  54. ("1001", Duration(secondsComponent: 1, attosecondsComponent: 1_000_000_000_000_000)),
  55. ("1999", Duration(secondsComponent: 1, attosecondsComponent: 999_000_000_000_000_000)),
  56. ]
  57. for (value, expectedDuration) in testData {
  58. let metadata: Metadata = ["grpc-retry-pushback-ms": "\(value)"]
  59. XCTAssertEqual(metadata.retryPushback, .retryAfter(expectedDuration))
  60. }
  61. }
  62. func testRetryPushbackInvalidDelay() {
  63. let testData: [String] = ["-1", "-inf", "not-a-number", "42.0"]
  64. for value in testData {
  65. let metadata: Metadata = ["grpc-retry-pushback-ms": "\(value)"]
  66. XCTAssertEqual(metadata.retryPushback, .stopRetrying)
  67. }
  68. }
  69. func testRetryPushbackNoValuePresent() {
  70. let metadata: Metadata = [:]
  71. XCTAssertNil(metadata.retryPushback)
  72. }
  73. }