ConnectionFailingTests.swift 2.4 KB

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