Forráskód Böngészése

Tolerate channel error when connecting (#1349)

Motivation:

In some cases it's possible for the connection manager to pick an error
from the channel when in the connecting state.

For some reason we decided this was not a valid transition despite
another function explicitly allowing an error to be handled while
in this state. The difference is that the other path is triggered by the
'connect()' future rather than coming from the channel directly.

Modifications:

- Call the function for handling failed connection attempts when we
  receive an error from the channel in the connecting state.

Result:

Better tolerate connection errors.
George Barnett 3 éve
szülő
commit
4eb40c8bd5

+ 1 - 1
Sources/GRPC/ConnectionManager.swift

@@ -591,7 +591,7 @@ internal final class ConnectionManager {
       ])
 
     case .connecting:
-      self.invalidState()
+      self.connectionFailed(withError: error)
 
     case var .active(state):
       state.error = error

+ 22 - 0
Tests/GRPCTests/ConnectionManagerTests.swift

@@ -1120,6 +1120,28 @@ extension ConnectionManagerTests {
 
     XCTAssertEqual(http2.streamsClosed, 4)
   }
+
+  func testChannelErrorWhenConnecting() throws {
+    let channelPromise = self.loop.makePromise(of: Channel.self)
+    let manager = self.makeConnectionManager { _, _ in
+      return channelPromise.futureResult
+    }
+
+    let multiplexer: EventLoopFuture<HTTP2StreamMultiplexer> = self.waitForStateChange(
+      from: .idle,
+      to: .connecting
+    ) {
+      let channel = manager.getHTTP2Multiplexer()
+      self.loop.run()
+      return channel
+    }
+
+    self.waitForStateChange(from: .connecting, to: .shutdown) {
+      manager.channelError(EventLoopError.shutdown)
+    }
+
+    XCTAssertThrowsError(try multiplexer.wait())
+  }
 }
 
 internal struct Change: Hashable, CustomStringConvertible {