DebugChannelInitializerTests.swift 2.3 KB

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