ServerTests.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2025, 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 NIOTransportServices
  21. import XCTest
  22. #if canImport(Network)
  23. import Network
  24. #endif
  25. class ServerTests: GRPCTestCase {
  26. #if canImport(Network)
  27. func testParametersConfigurators() throws {
  28. let listenerCounter = NIOLockedValueBox(0)
  29. let childChannelsCounter = NIOLockedValueBox(0)
  30. let group = NIOTSEventLoopGroup()
  31. defer {
  32. try? group.syncShutdownGracefully()
  33. }
  34. var serverConfiguration = Server.Configuration.default(
  35. target: .hostAndPort("localhost", 0),
  36. eventLoopGroup: group,
  37. serviceProviders: []
  38. )
  39. serverConfiguration.listenerNWParametersConfigurator = { _ in
  40. listenerCounter.withLockedValue { $0 += 1 }
  41. }
  42. serverConfiguration.childChannelNWParametersConfigurator = { _ in
  43. childChannelsCounter.withLockedValue { $0 += 1 }
  44. }
  45. let server = try Server.start(configuration: serverConfiguration).wait()
  46. defer {
  47. try? server.close().wait()
  48. }
  49. // The listener channel should be up and running after starting the server
  50. XCTAssertEqual(1, listenerCounter.withLockedValue({ $0 }))
  51. // However we don't have any child channels set up as there are no active connections
  52. XCTAssertEqual(0, childChannelsCounter.withLockedValue({ $0 }))
  53. // Start a client and execute a request so that a connection is established.
  54. let channel = try GRPCChannelPool.with(
  55. target: .hostAndPort("localhost", server.channel.localAddress!.port!),
  56. transportSecurity: .plaintext,
  57. eventLoopGroup: group
  58. )
  59. defer {
  60. try? channel.close().wait()
  61. }
  62. let echo = Echo_EchoNIOClient(channel: channel)
  63. _ = try echo.get(.with { $0.text = "" }).status.wait()
  64. // Now the configurator should have run.
  65. XCTAssertEqual(1, childChannelsCounter.withLockedValue({ $0 }))
  66. }
  67. #endif
  68. }