ConnectionFailingTests.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2020, 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 EchoModel
  17. import GRPC
  18. import NIO
  19. import XCTest
  20. class ConnectionFailingTests: GRPCTestCase {
  21. func testStartRPCWhenChannelIsInTransientFailure() throws {
  22. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  23. defer {
  24. XCTAssertNoThrow(try group.syncShutdownGracefully())
  25. }
  26. let waiter = RecordingConnectivityDelegate()
  27. let connection = ClientConnection.insecure(group: group)
  28. // We want to make sure we sit in transient failure for a long time.
  29. .withConnectionBackoff(initial: .hours(24))
  30. .withCallStartBehavior(.fastFailure)
  31. .withConnectivityStateDelegate(waiter)
  32. .connect(host: "http://unreachable.invalid", port: 0)
  33. defer {
  34. XCTAssertNoThrow(try connection.close().wait())
  35. }
  36. let echo = Echo_EchoClient(channel: connection)
  37. // Set our expectation.
  38. waiter.expectChanges(2) { changes in
  39. XCTAssertEqual(changes[0], Change(from: .idle, to: .connecting))
  40. XCTAssertEqual(changes[1], Change(from: .connecting, to: .transientFailure))
  41. }
  42. // This will trigger a connection attempt and subsequently fail.
  43. _ = echo.get(.with { $0.text = "cheddar" })
  44. // Wait for the changes.
  45. waiter.waitForExpectedChanges(timeout: .seconds(10))
  46. // Okay, now let's try another RPC. It should fail immediately with the connection error.
  47. let get = echo.get(.with { $0.text = "comté" })
  48. XCTAssertThrowsError(try get.response.wait())
  49. let status = try get.status.wait()
  50. XCTAssertEqual(status.code, .unavailable)
  51. // We can't say too much about the message here. It should contain details about the transient
  52. // failure error.
  53. XCTAssertNotNil(status.message)
  54. XCTAssertTrue(status.message?.contains("unreachable.invalid") ?? false)
  55. }
  56. }