ClientDebugChannelInitializerTests.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2020, 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 GRPC
  17. import EchoModel
  18. import EchoImplementation
  19. import NIO
  20. import NIOConcurrencyHelpers
  21. import XCTest
  22. class ClientDebugChannelInitializerTests: GRPCTestCase {
  23. func testDebugChannelInitializerIsCalled() throws {
  24. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  25. defer {
  26. XCTAssertNoThrow(try group.syncShutdownGracefully())
  27. }
  28. let server = try Server.insecure(group: group)
  29. .withServiceProviders([EchoProvider()])
  30. .bind(host: "localhost", port: 0)
  31. .wait()
  32. defer {
  33. XCTAssertNoThrow(try server.close().wait())
  34. }
  35. let debugInitializerCalled = group.next().makePromise(of: Void.self)
  36. let connection = ClientConnection.insecure(group: group)
  37. .withBackgroundActivityLogger(self.clientLogger)
  38. .withDebugChannelInitializer { channel in
  39. debugInitializerCalled.succeed(())
  40. return channel.eventLoop.makeSucceededFuture(())
  41. }
  42. .connect(host: "localhost", port: server.channel.localAddress!.port!)
  43. defer {
  44. XCTAssertNoThrow(try connection.close().wait())
  45. }
  46. let echo = Echo_EchoClient(channel: connection)
  47. // Make an RPC to trigger channel creation.
  48. let get = echo.get(.with { $0.text = "Hello!" })
  49. XCTAssertTrue(try get.status.map { $0.isOk }.wait())
  50. // Check the initializer was called.
  51. XCTAssertNoThrow(try debugInitializerCalled.futureResult.wait())
  52. }
  53. }