Metadata+GRPCTests.swift 2.9 KB

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