HTTP2MaxConcurrentStreamsTests.swift 3.3 KB

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