HTTP2MaxConcurrentStreamsTests.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. @testable import GRPC
  19. import NIOCore
  20. import NIOHTTP2
  21. import NIOPosix
  22. import XCTest
  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 = clientStreamingCalls
  53. .map { $0.sendMessage(.with { $0.text = "Hi!" }) }
  54. EventLoopFuture<Void>
  55. .whenAllSucceed(sendMessageFutures, on: eventLoopGroup.next())
  56. .assertSuccess(fulfill: allMessagesSentExpectation)
  57. self.wait(for: [allMessagesSentExpectation], timeout: Constants.testTimeout)
  58. let lastCall = clientStreamingCalls.popLast()!
  59. let lastCallCompletedExpectation = self.expectation(description: "last call completed")
  60. _ = lastCall.sendEnd()
  61. lastCall.status.assertSuccess(fulfill: lastCallCompletedExpectation)
  62. self.wait(for: [lastCallCompletedExpectation], timeout: Constants.testTimeout)
  63. let allCallsCompletedExpectation = self.expectation(description: "all calls completed")
  64. let endFutures = clientStreamingCalls.map { $0.sendEnd() }
  65. EventLoopFuture<Void>
  66. .whenAllSucceed(endFutures, on: eventLoopGroup.next())
  67. .assertSuccess(fulfill: allCallsCompletedExpectation)
  68. self.wait(for: [allCallsCompletedExpectation], timeout: Constants.testTimeout)
  69. }
  70. }