| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- /*
- * 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.
- */
- import NIOCore
- import NIOEmbedded
- import NIOHTTP2
- import XCTest
- @testable import GRPC
- class GRPCPingHandlerTests: GRPCTestCase {
- var pingHandler: PingHandler!
- func testClosingStreamWithoutPermitCalls() {
- // Do not allow pings without calls
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1))
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(1), timeout: .seconds(1)))
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- }
- func testClosingStreamWithPermitCalls() {
- // Allow pings without calls (since `minimumReceivedPingIntervalWithoutData` and `maximumPingStrikes` are not set, ping strikes should not have any effect)
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1), permitWithoutCalls: true)
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(1), timeout: .seconds(1)))
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- }
- func testIntervalWithCallInFlight() {
- // Do not allow pings without calls
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1))
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(1), timeout: .seconds(1)))
- // Move time to 1 second in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(1)
- // Send ping, which is valid
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Received valid pong, scheduled timeout should be cancelled
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: true)
- XCTAssertEqual(response, .cancelScheduledTimeout)
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- }
- func testIntervalWithoutCallsInFlight() {
- // Do not allow pings without calls
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1))
- // Send ping, which is invalid
- let response: PingHandler.Action = self.pingHandler.pingFired()
- XCTAssertEqual(response, .none)
- }
- func testIntervalWithCallNoLongerInFlight() {
- // Do not allow pings without calls
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1))
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(1), timeout: .seconds(1)))
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- // Move time to 1 second in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(1)
- // Send ping, which is invalid
- response = self.pingHandler.pingFired()
- XCTAssertEqual(response, .none)
- }
- func testIntervalWithoutCallsInFlightButPermitted() {
- // Allow pings without calls (since `minimumReceivedPingIntervalWithoutData` and `maximumPingStrikes` are not set, ping strikes should not have any effect)
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1), permitWithoutCalls: true)
- // Send ping, which is valid
- var response: PingHandler.Action = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Received valid pong, scheduled timeout should be cancelled
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: true)
- XCTAssertEqual(response, .cancelScheduledTimeout)
- }
- func testIntervalWithCallNoLongerInFlightButPermitted() {
- // Allow pings without calls (since `minimumReceivedPingIntervalWithoutData` and `maximumPingStrikes` are not set, ping strikes should not have any effect)
- self.setupPingHandler(interval: .seconds(1), timeout: .seconds(1), permitWithoutCalls: true)
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(1), timeout: .seconds(1)))
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- // Move time to 1 second in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(1)
- // Send ping, which is valid
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Received valid pong, scheduled timeout should be cancelled
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: true)
- XCTAssertEqual(response, .cancelScheduledTimeout)
- }
- func testIntervalTooEarlyWithCallInFlight() {
- // Do not allow pings without calls
- self.setupPingHandler(interval: .seconds(2), timeout: .seconds(1))
- // New stream created
- var response: PingHandler.Action = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(2), timeout: .seconds(1)))
- // Send first ping
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Move time to 1 second in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(1)
- // Send another ping, which is valid since client do not check ping strikes
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- }
- func testIntervalTooEarlyWithoutCallsInFlight() {
- // Allow pings without calls with a maximum pings of 2
- self.setupPingHandler(
- interval: .seconds(2),
- timeout: .seconds(1),
- permitWithoutCalls: true,
- maximumPingsWithoutData: 2,
- minimumSentPingIntervalWithoutData: .seconds(5)
- )
- // Send first ping
- var response: PingHandler.Action = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Move time to 1 second in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(1)
- // Send another ping, but since `now` is less than the ping interval, response should be no action
- response = self.pingHandler.pingFired()
- XCTAssertEqual(response, .none)
- // Move time to 5 seconds in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(5)
- // Send another ping, which is valid since we waited `minimumSentPingIntervalWithoutData`
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Move time to 10 seconds in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(10)
- // Send another ping, which is valid since we waited `minimumSentPingIntervalWithoutData`
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Send another ping, but we've exceeded `maximumPingsWithoutData` so response should be no action
- response = self.pingHandler.pingFired()
- XCTAssertEqual(response, .none)
- // New stream created
- response = self.pingHandler.streamCreated()
- XCTAssertEqual(response, .schedulePing(delay: .seconds(2), timeout: .seconds(1)))
- // Send another ping, now that there is call, ping is valid
- response = self.pingHandler.pingFired()
- XCTAssertEqual(
- response,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: false))
- )
- // Stream closed
- response = self.pingHandler.streamClosed()
- XCTAssertEqual(response, .none)
- }
- func testPingStrikesOnClientShouldHaveNoEffect() {
- // Allow pings without calls (since `minimumReceivedPingIntervalWithoutData` and `maximumPingStrikes` are not set, ping strikes should not have any effect)
- self.setupPingHandler(interval: .seconds(2), timeout: .seconds(1), permitWithoutCalls: true)
- // Received first ping, response should be a pong
- var response: PingHandler.Action = self.pingHandler.read(
- pingData: HTTP2PingData(withInteger: 1),
- ack: false
- )
- XCTAssertEqual(response, .ack)
- // Received another ping, response should be a pong (ping strikes not in effect)
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(response, .ack)
- // Received another ping, response should be a pong (ping strikes not in effect)
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(response, .ack)
- }
- func testPingWithoutDataResultsInPongForClient() {
- // Don't allow _sending_ pings when no calls are active (receiving pings should be tolerated).
- self.setupPingHandler(permitWithoutCalls: false)
- let action = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(action, .ack)
- }
- func testPingWithoutDataResultsInPongForServer() {
- // Don't allow _sending_ pings when no calls are active (receiving pings should be tolerated).
- // Set 'minimumReceivedPingIntervalWithoutData' and 'maximumPingStrikes' so that we enable
- // support for ping strikes.
- self.setupPingHandler(
- permitWithoutCalls: false,
- minimumReceivedPingIntervalWithoutData: .seconds(5),
- maximumPingStrikes: 1
- )
- let action = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(action, .ack)
- }
- func testPingStrikesOnServer() {
- // Set a maximum ping strikes of 1 without a minimum of 1 second between pings
- self.setupPingHandler(
- interval: .seconds(2),
- timeout: .seconds(1),
- permitWithoutCalls: true,
- minimumReceivedPingIntervalWithoutData: .seconds(1),
- maximumPingStrikes: 1
- )
- // Received first ping, response should be a pong
- var response: PingHandler.Action = self.pingHandler.read(
- pingData: HTTP2PingData(withInteger: 1),
- ack: false
- )
- XCTAssertEqual(response, .ack)
- // Received another ping, which is invalid (ping strike), response should be no action
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(response, .none)
- // Move time to 2 seconds in the future
- self.pingHandler._testingOnlyNow = .now() + .seconds(2)
- // Received another ping, which is valid now, response should be a pong
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(response, .ack)
- // Received another ping, which is invalid (ping strike), response should be no action
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(response, .none)
- // Received another ping, which is invalid (ping strike), since number of ping strikes is over the limit, response should be go away
- response = self.pingHandler.read(pingData: HTTP2PingData(withInteger: 1), ack: false)
- XCTAssertEqual(
- response,
- .reply(
- HTTP2Frame.FramePayload.goAway(
- lastStreamID: .rootStream,
- errorCode: .enhanceYourCalm,
- opaqueData: nil
- )
- )
- )
- }
- func testPongWithGoAwayPingData() {
- self.setupPingHandler()
- let response = self.pingHandler.read(pingData: self.pingHandler.pingDataGoAway, ack: true)
- XCTAssertEqual(response, .ratchetDownLastSeenStreamID)
- }
- private func setupPingHandler(
- pingCode: UInt64 = 1,
- interval: TimeAmount = .seconds(15),
- timeout: TimeAmount = .seconds(5),
- permitWithoutCalls: Bool = false,
- maximumPingsWithoutData: UInt = 2,
- minimumSentPingIntervalWithoutData: TimeAmount = .seconds(5),
- minimumReceivedPingIntervalWithoutData: TimeAmount? = nil,
- maximumPingStrikes: UInt? = nil
- ) {
- self.pingHandler = PingHandler(
- pingCode: pingCode,
- interval: interval,
- timeout: timeout,
- permitWithoutCalls: permitWithoutCalls,
- maximumPingsWithoutData: maximumPingsWithoutData,
- minimumSentPingIntervalWithoutData: minimumSentPingIntervalWithoutData,
- minimumReceivedPingIntervalWithoutData: minimumReceivedPingIntervalWithoutData,
- maximumPingStrikes: maximumPingStrikes
- )
- }
- }
- #if compiler(>=6.0)
- extension PingHandler.Action: @retroactive Equatable {}
- #else
- extension PingHandler.Action: Equatable {}
- #endif
- extension PingHandler.Action {
- public static func == (lhs: PingHandler.Action, rhs: PingHandler.Action) -> Bool {
- switch (lhs, rhs) {
- case (.none, .none):
- return true
- case (.ack, .ack):
- return true
- case (let .schedulePing(lhsDelay, lhsTimeout), let .schedulePing(rhsDelay, rhsTimeout)):
- return lhsDelay == rhsDelay && lhsTimeout == rhsTimeout
- case (.cancelScheduledTimeout, .cancelScheduledTimeout):
- return true
- case (.ratchetDownLastSeenStreamID, .ratchetDownLastSeenStreamID):
- return true
- case let (.reply(lhsPayload), .reply(rhsPayload)):
- switch (lhsPayload, rhsPayload) {
- case (let .ping(lhsData, ack: lhsAck), let .ping(rhsData, ack: rhsAck)):
- return lhsData == rhsData && lhsAck == rhsAck
- case (let .goAway(_, lhsErrorCode, _), let .goAway(_, rhsErrorCode, _)):
- return lhsErrorCode == rhsErrorCode
- default:
- return false
- }
- default:
- return false
- }
- }
- }
- extension GRPCPingHandlerTests {
- func testSingleAckIsEmittedOnPing() throws {
- let client = EmbeddedChannel()
- _ = try client.configureHTTP2Pipeline(mode: .client) { _ in
- fatalError("Unexpected inbound stream")
- }.wait()
- let server = EmbeddedChannel()
- let serverMux = try server.configureHTTP2Pipeline(mode: .server) { _ in
- fatalError("Unexpected inbound stream")
- }.wait()
- let idleHandler = GRPCIdleHandler(
- idleTimeout: .minutes(5),
- keepalive: .init(),
- logger: self.serverLogger
- )
- try server.pipeline.syncOperations.addHandler(idleHandler, position: .before(serverMux))
- try server.connect(to: .init(unixDomainSocketPath: "/ignored")).wait()
- try client.connect(to: .init(unixDomainSocketPath: "/ignored")).wait()
- func interact(client: EmbeddedChannel, server: EmbeddedChannel) throws {
- var didRead = true
- while didRead {
- didRead = false
- if let data = try client.readOutbound(as: ByteBuffer.self) {
- didRead = true
- try server.writeInbound(data)
- }
- if let data = try server.readOutbound(as: ByteBuffer.self) {
- didRead = true
- try client.writeInbound(data)
- }
- }
- }
- try interact(client: client, server: server)
- // Settings.
- let f1 = try XCTUnwrap(client.readInbound(as: HTTP2Frame.self))
- f1.payload.assertSettings(ack: false)
- // Settings ack.
- let f2 = try XCTUnwrap(client.readInbound(as: HTTP2Frame.self))
- f2.payload.assertSettings(ack: true)
- // Send a ping.
- let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 42), ack: false))
- try client.writeOutbound(ping)
- try interact(client: client, server: server)
- // Ping ack.
- let f3 = try XCTUnwrap(client.readInbound(as: HTTP2Frame.self))
- f3.payload.assertPing(ack: true)
- XCTAssertNil(try client.readInbound(as: HTTP2Frame.self))
- }
- }
- extension HTTP2Frame.FramePayload {
- func assertSettings(ack: Bool, file: StaticString = #file, line: UInt = #line) {
- switch self {
- case let .settings(settings):
- switch settings {
- case .ack:
- XCTAssertTrue(ack, file: file, line: line)
- case .settings:
- XCTAssertFalse(ack, file: file, line: line)
- }
- default:
- XCTFail("Expected .settings got \(self)", file: file, line: line)
- }
- }
- func assertPing(ack: Bool, file: StaticString = #file, line: UInt = #line) {
- switch self {
- case let .ping(_, ack: pingAck):
- XCTAssertEqual(pingAck, ack, file: file, line: line)
- default:
- XCTFail("Expected .ping got \(self)", file: file, line: line)
- }
- }
- }
|