| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- /*
- * 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 NIOCore
- import NIOHTTP2
- import XCTest
- 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- // 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- // 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- }
- 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- }
- 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- }
- 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- // 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,
- .reply(HTTP2Frame.FramePayload.ping(HTTP2PingData(withInteger: 1), ack: true))
- )
- // 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
- ))
- )
- }
- 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
- )
- }
- }
- extension PingHandler.Action: Equatable {
- public static func == (lhs: PingHandler.Action, rhs: PingHandler.Action) -> Bool {
- switch (lhs, rhs) {
- case (.none, .none):
- return true
- case (let .schedulePing(lhsDelay, lhsTimeout), let .schedulePing(rhsDelay, rhsTimeout)):
- return lhsDelay == rhsDelay && lhsTimeout == rhsTimeout
- case (.cancelScheduledTimeout, .cancelScheduledTimeout):
- 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
- }
- }
- }
|