DebugChannelInitializerTests.swift 2.3 KB

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