HTTP2MaxConcurrentStreamsTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2021, 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 EchoImplementation
  17. import EchoModel
  18. import NIOCore
  19. import NIOHTTP2
  20. import NIOPosix
  21. import XCTest
  22. @testable import GRPC
  23. class HTTP2MaxConcurrentStreamsTests: GRPCTestCase {
  24. enum Constants {
  25. static let testTimeout: TimeInterval = 10
  26. static let defaultMaxNumberOfConcurrentStreams =
  27. nioDefaultSettings.first(where: { $0.parameter == .maxConcurrentStreams })!.value
  28. static let testNumberOfConcurrentStreams: Int = defaultMaxNumberOfConcurrentStreams + 20
  29. }
  30. func testHTTP2MaxConcurrentStreamsSetting() {
  31. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
  32. defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
  33. let server = try! Server.insecure(group: eventLoopGroup)
  34. .withLogger(self.serverLogger)
  35. .withHTTPMaxConcurrentStreams(Constants.testNumberOfConcurrentStreams)
  36. .withServiceProviders([EchoProvider()])
  37. .bind(host: "localhost", port: 0)
  38. .wait()
  39. defer { XCTAssertNoThrow(try server.initiateGracefulShutdown().wait()) }
  40. let clientConnection = ClientConnection.insecure(group: eventLoopGroup)
  41. .withBackgroundActivityLogger(self.clientLogger)
  42. .connect(host: "localhost", port: server.channel.localAddress!.port!)
  43. defer { XCTAssertNoThrow(try clientConnection.close().wait()) }
  44. let echoClient = Echo_EchoNIOClient(
  45. channel: clientConnection,
  46. defaultCallOptions: CallOptions(logger: self.clientLogger)
  47. )
  48. var clientStreamingCalls =
  49. (0 ..< Constants.testNumberOfConcurrentStreams)
  50. .map { _ in echoClient.collect() }
  51. let allMessagesSentExpectation = self.expectation(description: "all messages sent")
  52. let sendMessageFutures =
  53. clientStreamingCalls
  54. .map { $0.sendMessage(.with { $0.text = "Hi!" }) }
  55. EventLoopFuture<Void>
  56. .whenAllSucceed(sendMessageFutures, on: eventLoopGroup.next())
  57. .assertSuccess(fulfill: allMessagesSentExpectation)
  58. self.wait(for: [allMessagesSentExpectation], timeout: Constants.testTimeout)
  59. let lastCall = clientStreamingCalls.popLast()!
  60. let lastCallCompletedExpectation = self.expectation(description: "last call completed")
  61. _ = lastCall.sendEnd()
  62. lastCall.status.assertSuccess(fulfill: lastCallCompletedExpectation)
  63. self.wait(for: [lastCallCompletedExpectation], timeout: Constants.testTimeout)
  64. let allCallsCompletedExpectation = self.expectation(description: "all calls completed")
  65. let endFutures = clientStreamingCalls.map { $0.sendEnd() }
  66. EventLoopFuture<Void>
  67. .whenAllSucceed(endFutures, on: eventLoopGroup.next())
  68. .assertSuccess(fulfill: allCallsCompletedExpectation)
  69. self.wait(for: [allCallsCompletedExpectation], timeout: Constants.testTimeout)
  70. }
  71. }