| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- /*
- * Copyright 2020, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- @testable import GRPC
- import NIO
- import NIOHTTP2
- import XCTest
- class ConnectionManagerTests: GRPCTestCase {
- private let loop = EmbeddedEventLoop()
- private let recorder = RecordingConnectivityDelegate()
- private var defaultConfiguration: ClientConnection.Configuration {
- return ClientConnection.Configuration(
- target: .unixDomainSocket("/ignored"),
- eventLoopGroup: self.loop,
- connectivityStateDelegate: self.recorder,
- connectionBackoff: nil,
- backgroundActivityLogger: self.clientLogger
- )
- }
- override func tearDown() {
- XCTAssertNoThrow(try self.loop.syncShutdownGracefully())
- super.tearDown()
- }
- private func waitForStateChange<Result>(
- from: ConnectivityState,
- to: ConnectivityState,
- timeout: DispatchTimeInterval = .seconds(1),
- file: StaticString = #file,
- line: UInt = #line,
- body: () throws -> Result
- ) rethrows -> Result {
- self.recorder.expectChange {
- XCTAssertEqual($0, Change(from: from, to: to), file: file, line: line)
- }
- let result = try body()
- self.recorder.waitForExpectedChanges(timeout: timeout, file: file, line: line)
- return result
- }
- private func waitForStateChanges<Result>(
- _ changes: [Change],
- timeout: DispatchTimeInterval = .seconds(1),
- file: StaticString = #file,
- line: UInt = #line,
- body: () throws -> Result
- ) rethrows -> Result {
- self.recorder.expectChanges(changes.count) {
- XCTAssertEqual($0, changes)
- }
- let result = try body()
- self.recorder.waitForExpectedChanges(timeout: timeout, file: file, line: line)
- return result
- }
- }
- extension ConnectionManagerTests {
- func testIdleShutdown() throws {
- let manager = ConnectionManager(configuration: self.defaultConfiguration, logger: self.logger)
- try self.waitForStateChange(from: .idle, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // Getting a channel should fail.
- let channel = manager.getChannel()
- self.loop.run()
- XCTAssertThrowsError(try channel.wait())
- }
- func testConnectFromIdleFailsWithNoReconnect() {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- let channel: EventLoopFuture<Channel> = self.waitForStateChange(from: .idle, to: .connecting) {
- let channel = manager.getChannel()
- self.loop.run()
- return channel
- }
- self.waitForStateChange(from: .connecting, to: .shutdown) {
- channelPromise.fail(DoomedChannelError())
- }
- XCTAssertThrowsError(try channel.wait()) {
- XCTAssertTrue($0 is DoomedChannelError)
- }
- }
- func testConnectAndDisconnect() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- self.waitForStateChange(from: .idle, to: .connecting) {
- _ = manager.getChannel()
- self.loop.run()
- }
- // Setup the real channel and activate it.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a settings frame on the root stream; this'll make the channel 'ready'.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // Close the channel.
- try self.waitForStateChange(from: .ready, to: .shutdown) {
- // Now the channel should be available: shut it down.
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testConnectAndIdle() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the channel.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a settings frame on the root stream; this'll make the channel 'ready'.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- // Wait for the channel, it _must_ be ready now.
- XCTAssertNoThrow(try readyChannel.wait())
- }
- // Go idle. This will shutdown the channel.
- try self.waitForStateChange(from: .ready, to: .idle) {
- self.loop.advanceTime(by: .minutes(5))
- XCTAssertNoThrow(try readyChannel.flatMap { $0.closeFuture }.wait())
- }
- // Now shutdown.
- try self.waitForStateChange(from: .idle, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testIdleTimeoutWhenThereAreActiveStreams() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the channel.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a settings frame on the root stream; this'll make the channel 'ready'.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- // Wait for the channel, it _must_ be ready now.
- XCTAssertNoThrow(try readyChannel.wait())
- }
- // "create" a stream; the details don't matter here.
- let streamCreated = NIOHTTP2StreamCreatedEvent(
- streamID: 1,
- localInitialWindowSize: nil,
- remoteInitialWindowSize: nil
- )
- channel.pipeline.fireUserInboundEventTriggered(streamCreated)
- // Wait for the idle timeout: this should _not_ cause the channel to idle.
- self.loop.advanceTime(by: .minutes(5))
- // Now we're going to close the stream and wait for an idle timeout and then shutdown.
- self.waitForStateChange(from: .ready, to: .idle) {
- // Close the stream.
- let streamClosed = StreamClosedEvent(streamID: 1, reason: nil)
- channel.pipeline.fireUserInboundEventTriggered(streamClosed)
- // ... wait for the idle timeout,
- self.loop.advanceTime(by: .minutes(5))
- }
- // Now shutdown.
- try self.waitForStateChange(from: .idle, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testConnectAndThenBecomeInactive() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the channel.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- try self.waitForStateChange(from: .connecting, to: .shutdown) {
- // Okay: now close the channel; the `readyChannel` future has not been completed yet.
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // We failed to get a channel and we don't have reconnect configured: we should be shutdown and
- // the `readyChannel` should error.
- XCTAssertThrowsError(try readyChannel.wait())
- }
- func testConnectOnSecondAttempt() throws {
- let channelPromise: EventLoopPromise<Channel> = self.loop.makePromise()
- let channelFutures: [EventLoopFuture<Channel>] = [
- self.loop.makeFailedFuture(DoomedChannelError()),
- channelPromise.futureResult,
- ]
- var channelFutureIterator = channelFutures.makeIterator()
- var configuration = self.defaultConfiguration
- configuration.connectionBackoff = .oneSecondFixed
- let manager = ConnectionManager.testingOnly(configuration: configuration, logger: self.logger) {
- guard let next = channelFutureIterator.next() else {
- XCTFail("Too many channels requested")
- return self.loop.makeFailedFuture(DoomedChannelError())
- }
- return next
- }
- let readyChannel: EventLoopFuture<Channel> = self.waitForStateChanges([
- Change(from: .idle, to: .connecting),
- Change(from: .connecting, to: .transientFailure),
- ]) {
- // Get a channel.
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Get a channel from the manager: it is a future for the same channel.
- let anotherReadyChannel = manager.getChannel()
- self.loop.run()
- // Move time forwards by a second to start the next connection attempt.
- self.waitForStateChange(from: .transientFailure, to: .connecting) {
- self.loop.advanceTime(by: .seconds(1))
- }
- // Setup the actual channel and complete the promise.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a SETTINGS frame on the root stream.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // Wait for the channel, it _must_ be ready now.
- XCTAssertNoThrow(try readyChannel.wait())
- XCTAssertNoThrow(try anotherReadyChannel.wait())
- // Now shutdown.
- try self.waitForStateChange(from: .ready, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testShutdownWhileConnecting() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Now shutdown.
- try self.waitForStateChange(from: .connecting, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // The channel we were requesting should fail.
- XCTAssertThrowsError(try readyChannel.wait())
- // We still have our channel promise to fulfil: if it succeeds then it too should be closed.
- channelPromise.succeed(EmbeddedChannel(loop: self.loop))
- let channel = try channelPromise.futureResult.wait()
- self.loop.run()
- XCTAssertNoThrow(try channel.closeFuture.wait())
- }
- func testShutdownWhileTransientFailure() throws {
- var configuration = self.defaultConfiguration
- configuration.connectionBackoff = .oneSecondFixed
- let manager = ConnectionManager.testingOnly(configuration: configuration, logger: self.logger) {
- self.loop.makeFailedFuture(DoomedChannelError())
- }
- let readyChannel: EventLoopFuture<Channel> = self.waitForStateChanges([
- Change(from: .idle, to: .connecting),
- Change(from: .connecting, to: .transientFailure),
- ]) {
- // Get a channel.
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Now shutdown.
- try self.waitForStateChange(from: .transientFailure, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // The channel we were requesting should fail.
- XCTAssertThrowsError(try readyChannel.wait())
- }
- func testShutdownWhileActive() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Prepare the channel
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // (No state change expected here: active is an internal state.)
- // Now shutdown.
- try self.waitForStateChange(from: .connecting, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // The channel we were requesting should fail.
- XCTAssertThrowsError(try readyChannel.wait())
- }
- func testShutdownWhileShutdown() throws {
- let manager = ConnectionManager(configuration: self.defaultConfiguration, logger: self.logger)
- try self.waitForStateChange(from: .idle, to: .shutdown) {
- let firstShutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try firstShutdown.wait())
- }
- let secondShutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try secondShutdown.wait())
- }
- func testTransientFailureWhileActive() throws {
- var configuration = self.defaultConfiguration
- configuration.connectionBackoff = .oneSecondFixed
- let channelPromise: EventLoopPromise<Channel> = self.loop.makePromise()
- let channelFutures: [EventLoopFuture<Channel>] = [
- channelPromise.futureResult,
- self.loop.makeFailedFuture(DoomedChannelError()),
- ]
- var channelFutureIterator = channelFutures.makeIterator()
- let manager = ConnectionManager.testingOnly(configuration: configuration, logger: self.logger) {
- guard let next = channelFutureIterator.next() else {
- XCTFail("Too many channels requested")
- return self.loop.makeFailedFuture(DoomedChannelError())
- }
- return next
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Prepare the channel
- let firstChannel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(firstChannel)
- XCTAssertNoThrow(
- try firstChannel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // (No state change expected here: active is an internal state.)
- // Close the channel (simulate e.g. TLS handshake failed)
- try self.waitForStateChange(from: .connecting, to: .transientFailure) {
- XCTAssertNoThrow(try firstChannel.close().wait())
- }
- // Start connecting again.
- self.waitForStateChanges([
- Change(from: .transientFailure, to: .connecting),
- Change(from: .connecting, to: .transientFailure),
- ]) {
- self.loop.advanceTime(by: .seconds(1))
- }
- // Now shutdown
- try self.waitForStateChange(from: .transientFailure, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // The channel never came up: it should be throw.
- XCTAssertThrowsError(try readyChannel.wait())
- }
- func testTransientFailureWhileReady() throws {
- var configuration = self.defaultConfiguration
- configuration.connectionBackoff = .oneSecondFixed
- let firstChannelPromise: EventLoopPromise<Channel> = self.loop.makePromise()
- let secondChannelPromise: EventLoopPromise<Channel> = self.loop.makePromise()
- let channelFutures: [EventLoopFuture<Channel>] = [
- firstChannelPromise.futureResult,
- secondChannelPromise.futureResult,
- ]
- var channelFutureIterator = channelFutures.makeIterator()
- let manager = ConnectionManager.testingOnly(configuration: configuration, logger: self.logger) {
- guard let next = channelFutureIterator.next() else {
- XCTFail("Too many channels requested")
- return self.loop.makeFailedFuture(DoomedChannelError())
- }
- return next
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Prepare the first channel
- let firstChannel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- firstChannelPromise.succeed(firstChannel)
- XCTAssertNoThrow(
- try firstChannel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a SETTINGS frame on the root stream.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try firstChannel.writeInbound(frame))
- }
- // Channel should now be ready.
- XCTAssertNoThrow(try readyChannel.wait())
- // Kill the first channel. But first ensure there's an active RPC, otherwise we'll idle.
- let streamCreated = NIOHTTP2StreamCreatedEvent(
- streamID: 1,
- localInitialWindowSize: nil,
- remoteInitialWindowSize: nil
- )
- firstChannel.pipeline.fireUserInboundEventTriggered(streamCreated)
- try self.waitForStateChange(from: .ready, to: .transientFailure) {
- XCTAssertNoThrow(try firstChannel.close().wait())
- }
- // Run to start connecting again.
- self.waitForStateChange(from: .transientFailure, to: .connecting) {
- self.loop.advanceTime(by: .seconds(1))
- }
- // Prepare the second channel
- let secondChannel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- secondChannelPromise.succeed(secondChannel)
- XCTAssertNoThrow(
- try secondChannel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a SETTINGS frame on the root stream.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try secondChannel.writeInbound(frame))
- }
- // Now shutdown
- try self.waitForStateChange(from: .ready, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testGoAwayWhenReady() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the channel.
- let channel = EmbeddedChannel(
- handler: GRPCIdleHandler(
- mode: .client(manager),
- logger: self.logger,
- idleTimeout: .minutes(5)
- ),
- loop: self.loop
- )
- channelPromise.succeed(channel)
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- try self.waitForStateChange(from: .connecting, to: .ready) {
- // Write a SETTINGS frame on the root stream.
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // Wait for the channel, it _must_ be ready now.
- XCTAssertNoThrow(try readyChannel.wait())
- // Send a GO_AWAY; the details don't matter. This will cause the connection to go idle and the
- // channel to close.
- try self.waitForStateChange(from: .ready, to: .idle) {
- let goAway = HTTP2Frame(
- streamID: .rootStream,
- payload: .goAway(lastStreamID: 1, errorCode: .noError, opaqueData: nil)
- )
- XCTAssertNoThrow(try channel.writeInbound(goAway))
- }
- self.loop.run()
- XCTAssertNoThrow(try channel.closeFuture.wait())
- // Now shutdown
- try self.waitForStateChange(from: .idle, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- }
- func testDoomedOptimisticChannelFromIdle() {
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- self.loop.makeFailedFuture(DoomedChannelError())
- }
- let candidate = manager.getOptimisticChannel()
- self.loop.run()
- XCTAssertThrowsError(try candidate.wait())
- }
- func testDoomedOptimisticChannelFromConnecting() throws {
- let promise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- promise.futureResult
- }
- self.waitForStateChange(from: .idle, to: .connecting) {
- // Trigger channel creation, and a connection attempt, we don't care about the channel.
- _ = manager.getChannel()
- self.loop.run()
- }
- // We're connecting: get an optimistic channel.
- let optimisticChannel = manager.getOptimisticChannel()
- self.loop.run()
- // Fail the promise.
- promise.fail(DoomedChannelError())
- XCTAssertThrowsError(try optimisticChannel.wait())
- }
- func testOptimisticChannelFromTransientFailure() throws {
- var configuration = self.defaultConfiguration
- configuration.connectionBackoff = ConnectionBackoff()
- let manager = ConnectionManager.testingOnly(configuration: configuration, logger: self.logger) {
- self.loop.makeFailedFuture(DoomedChannelError())
- }
- self.waitForStateChanges([
- Change(from: .idle, to: .connecting),
- Change(from: .connecting, to: .transientFailure),
- ]) {
- // Trigger channel creation, and a connection attempt, we don't care about the channel.
- _ = manager.getChannel()
- self.loop.run()
- }
- // Now we're sitting in transient failure. Get a channel optimistically.
- let optimisticChannel = manager.getOptimisticChannel()
- self.loop.run()
- XCTAssertThrowsError(try optimisticChannel.wait())
- }
- func testOptimisticChannelFromShutdown() throws {
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- self.loop.makeFailedFuture(DoomedChannelError())
- }
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- // Get a channel optimistically. It'll fail, obviously.
- let channel = manager.getOptimisticChannel()
- self.loop.run()
- XCTAssertThrowsError(try channel.wait())
- }
- func testDoubleIdle() throws {
- class CloseDroppingHandler: ChannelOutboundHandler {
- typealias OutboundIn = Any
- func close(context: ChannelHandlerContext, mode: CloseMode,
- promise: EventLoopPromise<Void>?) {
- promise?
- .fail(GRPCStatus(code: .unavailable, message: "Purposefully dropping channel close"))
- }
- }
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- let readyChannel: EventLoopFuture<Channel> = self
- .waitForStateChange(from: .idle, to: .connecting) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the real channel and activate it.
- let channel = EmbeddedChannel(loop: self.loop)
- XCTAssertNoThrow(try channel.pipeline.addHandlers([
- CloseDroppingHandler(),
- GRPCIdleHandler(mode: .client(manager), logger: manager.logger, idleTimeout: .minutes(5)),
- ]).wait())
- channelPromise.succeed(channel)
- self.loop.run()
- XCTAssertNoThrow(
- try channel.connect(to: SocketAddress(unixDomainSocketPath: "/ignored"))
- .wait()
- )
- // Write a SETTINGS frame on the root stream.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // The channel should now be ready.
- XCTAssertNoThrow(try readyChannel.wait())
- // Send a GO_AWAY; the details don't matter. This will cause the connection to go idle and the
- // channel to close.
- try self.waitForStateChange(from: .ready, to: .idle) {
- let goAway = HTTP2Frame(
- streamID: .rootStream,
- payload: .goAway(lastStreamID: 1, errorCode: .noError, opaqueData: nil)
- )
- XCTAssertNoThrow(try channel.writeInbound(goAway))
- }
- // We dropped the close; now wait for the scheduled idle to fire.
- //
- // Previously doing this this would fail a precondition.
- self.loop.advanceTime(by: .minutes(5))
- }
- func testForceIdleAfterInactive() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- let readyChannel: EventLoopFuture<Channel> = self.waitForStateChange(
- from: .idle,
- to: .connecting
- ) {
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the real channel and activate it.
- let channel = EmbeddedChannel(loop: self.loop)
- XCTAssertNoThrow(try channel.pipeline.addHandlers([
- GRPCIdleHandler(mode: .client(manager), logger: manager.logger, idleTimeout: .minutes(5)),
- ]).wait())
- channelPromise.succeed(channel)
- self.loop.run()
- let connect = channel.connect(to: try SocketAddress(unixDomainSocketPath: "/ignored"))
- XCTAssertNoThrow(try connect.wait())
- // Write a SETTINGS frame on the root stream.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // The channel should now be ready.
- XCTAssertNoThrow(try readyChannel.wait())
- // Now drop the connection.
- try self.waitForStateChange(from: .ready, to: .shutdown) {
- let shutdown = manager.shutdown()
- self.loop.run()
- XCTAssertNoThrow(try shutdown.wait())
- }
- // Fire a connection idled event, i.e. keepalive timeout has fired. This should be a no-op.
- // Previously this would hit a precondition failure.
- channel.pipeline.fireUserInboundEventTriggered(ConnectionIdledEvent())
- }
- func testCloseWithoutActiveRPCs() throws {
- let channelPromise = self.loop.makePromise(of: Channel.self)
- let manager = ConnectionManager.testingOnly(
- configuration: self.defaultConfiguration,
- logger: self.logger
- ) {
- channelPromise.futureResult
- }
- // Start the connection.
- let readyChannel = self.waitForStateChange(
- from: .idle,
- to: .connecting
- ) { () -> EventLoopFuture<Channel> in
- let readyChannel = manager.getChannel()
- self.loop.run()
- return readyChannel
- }
- // Setup the actual channel and activate it.
- let channel = EmbeddedChannel(loop: self.loop)
- XCTAssertNoThrow(try channel.pipeline.addHandlers([
- GRPCIdleHandler(mode: .client(manager), logger: manager.logger, idleTimeout: .minutes(5)),
- ]).wait())
- channelPromise.succeed(channel)
- self.loop.run()
- let connect = channel.connect(to: try SocketAddress(unixDomainSocketPath: "/ignored"))
- XCTAssertNoThrow(try connect.wait())
- // "ready" the connection.
- try self.waitForStateChange(from: .connecting, to: .ready) {
- let frame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([])))
- XCTAssertNoThrow(try channel.writeInbound(frame))
- }
- // The channel should now be ready.
- XCTAssertNoThrow(try readyChannel.wait())
- // Close the channel. There are no active RPCs so we should idle rather than be in the transient
- // failure state.
- self.waitForStateChange(from: .ready, to: .idle) {
- channel.pipeline.fireChannelInactive()
- }
- }
- }
- internal struct Change: Hashable, CustomStringConvertible {
- var from: ConnectivityState
- var to: ConnectivityState
- var description: String {
- return "\(self.from) → \(self.to)"
- }
- }
- internal class RecordingConnectivityDelegate: ConnectivityStateDelegate {
- private let serialQueue = DispatchQueue(label: "io.grpc.testing")
- private let semaphore = DispatchSemaphore(value: 0)
- private var expectation: Expectation = .noExpectation
- private enum Expectation {
- /// We have no expectation of any changes. We'll just ignore any changes.
- case noExpectation
- /// We expect one change.
- case one((Change) -> Void)
- /// We expect 'count' changes.
- case some(count: Int, recorded: [Change], ([Change]) -> Void)
- var count: Int {
- switch self {
- case .noExpectation:
- return 0
- case .one:
- return 1
- case let .some(count, _, _):
- return count
- }
- }
- }
- func connectivityStateDidChange(from oldState: ConnectivityState,
- to newState: ConnectivityState) {
- self.serialQueue.async {
- switch self.expectation {
- case let .one(verify):
- // We don't care about future changes.
- self.expectation = .noExpectation
- // Verify and notify.
- verify(Change(from: oldState, to: newState))
- self.semaphore.signal()
- case .some(let count, var recorded, let verify):
- recorded.append(Change(from: oldState, to: newState))
- if recorded.count == count {
- // We don't care about future changes.
- self.expectation = .noExpectation
- // Verify and notify.
- verify(recorded)
- self.semaphore.signal()
- } else {
- // Still need more responses.
- self.expectation = .some(count: count, recorded: recorded, verify)
- }
- case .noExpectation:
- // Ignore any changes.
- ()
- }
- }
- }
- func expectChanges(_ count: Int, verify: @escaping ([Change]) -> Void) {
- self.serialQueue.async {
- self.expectation = .some(count: count, recorded: [], verify)
- }
- }
- func expectChange(verify: @escaping (Change) -> Void) {
- self.serialQueue.async {
- self.expectation = .one(verify)
- }
- }
- func waitForExpectedChanges(
- timeout: DispatchTimeInterval,
- file: StaticString = #file,
- line: UInt = #line
- ) {
- let result = self.semaphore.wait(timeout: .now() + timeout)
- switch result {
- case .success:
- ()
- case .timedOut:
- XCTFail(
- "Timed out before verifying \(self.expectation.count) change(s)",
- file: file, line: line
- )
- }
- }
- }
- private extension ConnectionBackoff {
- static let oneSecondFixed = ConnectionBackoff(
- initialBackoff: 1.0,
- maximumBackoff: 1.0,
- multiplier: 1.0,
- jitter: 0.0
- )
- }
- private struct DoomedChannelError: Error {}
|