RequestIDTests.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 EchoModel
  17. import GRPC
  18. import NIOCore
  19. import NIOPosix
  20. import XCTest
  21. internal final class RequestIDTests: GRPCTestCase {
  22. private var server: Server!
  23. private var group: EventLoopGroup!
  24. override func setUp() {
  25. super.setUp()
  26. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  27. self.server = try! Server.insecure(group: self.group)
  28. .withServiceProviders([MetadataEchoProvider()])
  29. .withLogger(self.serverLogger)
  30. .bind(host: "127.0.0.1", port: 0)
  31. .wait()
  32. }
  33. override func tearDown() {
  34. XCTAssertNoThrow(try self.server.close().wait())
  35. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  36. super.tearDown()
  37. }
  38. func testRequestIDIsPopulatedClientConnection() throws {
  39. let channel = ClientConnection.insecure(group: self.group)
  40. .connect(host: "127.0.0.1", port: self.server.channel.localAddress!.port!)
  41. defer {
  42. let loop = group.next()
  43. let promise = loop.makePromise(of: Void.self)
  44. channel.closeGracefully(deadline: .now() + .seconds(30), promise: promise)
  45. XCTAssertNoThrow(try promise.futureResult.wait())
  46. }
  47. try self._testRequestIDIsPopulated(channel: channel)
  48. }
  49. func testRequestIDIsPopulatedChannelPool() throws {
  50. let channel = try! GRPCChannelPool.with(
  51. target: .host("127.0.0.1", port: self.server.channel.localAddress!.port!),
  52. transportSecurity: .plaintext,
  53. eventLoopGroup: self.group
  54. )
  55. defer {
  56. let loop = group.next()
  57. let promise = loop.makePromise(of: Void.self)
  58. channel.closeGracefully(deadline: .now() + .seconds(30), promise: promise)
  59. XCTAssertNoThrow(try promise.futureResult.wait())
  60. }
  61. try self._testRequestIDIsPopulated(channel: channel)
  62. }
  63. func _testRequestIDIsPopulated(channel: GRPCChannel) throws {
  64. let echo = Echo_EchoNIOClient(channel: channel)
  65. let options = CallOptions(
  66. requestIDProvider: .userDefined("foo"),
  67. requestIDHeader: "request-id-header"
  68. )
  69. let get = echo.get(.with { $0.text = "ignored" }, callOptions: options)
  70. let response = try get.response.wait()
  71. XCTAssert(response.text.contains("request-id-header: foo"))
  72. }
  73. }