HTTP2DelegateTests.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 XCTest
  21. final class HTTP2ConnectionDelegateTests: GRPCTestCase {
  22. private var group: EventLoopGroup!
  23. private var server: Server!
  24. private var connection: ClientConnection!
  25. private let queue = DispatchQueue(label: "io.grpc.testing")
  26. override func setUp() {
  27. super.setUp()
  28. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 2)
  29. self.server = try! Server.insecure(group: self.group)
  30. .withLogger(self.serverLogger)
  31. .withServiceProviders([EchoProvider()])
  32. .bind(host: "127.0.0.1", port: 0)
  33. .wait()
  34. self.connection = ClientConnection.insecure(group: self.group)
  35. .withBackgroundActivityLogger(self.clientLogger)
  36. // The http/2 delegate is internal but uses the same queue as the connectivity state delegate,
  37. // so this looks odd but is fine.
  38. .withConnectivityStateDelegate(nil, executingOn: self.queue)
  39. .connect(host: "127.0.0.1", port: self.server!.channel.localAddress!.port!)
  40. }
  41. override func tearDown() {
  42. XCTAssertNoThrow(try self.connection.close().wait())
  43. XCTAssertNoThrow(try self.server.close().wait())
  44. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  45. super.tearDown()
  46. }
  47. func testDelegate() {
  48. let http2Delegate = RecordingHTTP2Delegate()
  49. self.connection.connectivity.http2Delegate = http2Delegate
  50. let echo = Echo_EchoClient(channel: self.connection)
  51. // Fire off a handful of RPCs.
  52. for _ in 0 ..< 10 {
  53. let get = echo.get(.with { $0.text = "" })
  54. XCTAssertNoThrow(try get.status.wait())
  55. }
  56. // 10 RPCs, 10 streams closed.
  57. XCTAssertEqual(self.queue.sync { http2Delegate.streamsClosed }, 10)
  58. XCTAssertEqual(self.queue.sync { http2Delegate.maxConcurrentStreamsChanges }, [100])
  59. }
  60. }