ClientEventLoopPreferenceTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 EchoImplementation
  17. import EchoModel
  18. import GRPC
  19. import NIO
  20. import XCTest
  21. final class ClientEventLoopPreferenceTests: GRPCTestCase {
  22. private var group: MultiThreadedEventLoopGroup!
  23. private var serverLoop: EventLoop!
  24. private var clientLoop: EventLoop!
  25. private var clientCallbackLoop: EventLoop!
  26. private var server: Server!
  27. private var connection: ClientConnection!
  28. private var echo: Echo_EchoClient {
  29. let options = CallOptions(
  30. eventLoopPreference: .exact(self.clientCallbackLoop),
  31. logger: self.clientLogger
  32. )
  33. return Echo_EchoClient(channel: self.connection, defaultCallOptions: options)
  34. }
  35. override func setUp() {
  36. super.setUp()
  37. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 3)
  38. self.serverLoop = self.group.next()
  39. self.clientLoop = self.group.next()
  40. self.clientCallbackLoop = self.group.next()
  41. XCTAssert(self.serverLoop !== self.clientLoop)
  42. XCTAssert(self.serverLoop !== self.clientCallbackLoop)
  43. XCTAssert(self.clientLoop !== self.clientCallbackLoop)
  44. self.server = try! Server.insecure(group: self.serverLoop)
  45. .withLogger(self.serverLogger)
  46. .withServiceProviders([EchoProvider()])
  47. .bind(host: "localhost", port: 0)
  48. .wait()
  49. self.connection = ClientConnection.insecure(group: self.clientLoop)
  50. .withBackgroundActivityLogger(self.clientLogger)
  51. .connect(host: "localhost", port: self.server.channel.localAddress!.port!)
  52. }
  53. override func tearDown() {
  54. XCTAssertNoThrow(try self.connection.close().wait())
  55. XCTAssertNoThrow(try self.server.close().wait())
  56. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  57. super.tearDown()
  58. }
  59. private func assertClientCallbackEventLoop(_ eventLoop: EventLoop, line: UInt = #line) {
  60. XCTAssert(eventLoop === self.clientCallbackLoop, line: line)
  61. }
  62. func testUnaryWithDifferentEventLoop() throws {
  63. let get = self.echo.get(.with { $0.text = "Hello!" })
  64. self.assertClientCallbackEventLoop(get.eventLoop)
  65. self.assertClientCallbackEventLoop(get.initialMetadata.eventLoop)
  66. self.assertClientCallbackEventLoop(get.response.eventLoop)
  67. self.assertClientCallbackEventLoop(get.trailingMetadata.eventLoop)
  68. self.assertClientCallbackEventLoop(get.status.eventLoop)
  69. assertThat(try get.response.wait(), .is(.with { $0.text = "Swift echo get: Hello!" }))
  70. assertThat(try get.status.wait(), .hasCode(.ok))
  71. }
  72. func testClientStreamingWithDifferentEventLoop() throws {
  73. let collect = self.echo.collect()
  74. self.assertClientCallbackEventLoop(collect.eventLoop)
  75. self.assertClientCallbackEventLoop(collect.initialMetadata.eventLoop)
  76. self.assertClientCallbackEventLoop(collect.response.eventLoop)
  77. self.assertClientCallbackEventLoop(collect.trailingMetadata.eventLoop)
  78. self.assertClientCallbackEventLoop(collect.status.eventLoop)
  79. XCTAssertNoThrow(try collect.sendMessage(.with { $0.text = "a" }).wait())
  80. XCTAssertNoThrow(try collect.sendEnd().wait())
  81. assertThat(try collect.response.wait(), .is(.with { $0.text = "Swift echo collect: a" }))
  82. assertThat(try collect.status.wait(), .hasCode(.ok))
  83. }
  84. func testServerStreamingWithDifferentEventLoop() throws {
  85. let response = self.clientCallbackLoop.makePromise(of: Void.self)
  86. let expand = self.echo.expand(.with { $0.text = "a" }) { _ in
  87. self.clientCallbackLoop.preconditionInEventLoop()
  88. response.succeed(())
  89. }
  90. self.assertClientCallbackEventLoop(expand.eventLoop)
  91. self.assertClientCallbackEventLoop(expand.initialMetadata.eventLoop)
  92. self.assertClientCallbackEventLoop(expand.trailingMetadata.eventLoop)
  93. self.assertClientCallbackEventLoop(expand.status.eventLoop)
  94. XCTAssertNoThrow(try response.futureResult.wait())
  95. assertThat(try expand.status.wait(), .hasCode(.ok))
  96. }
  97. func testBidirectionalStreamingWithDifferentEventLoop() throws {
  98. let response = self.clientCallbackLoop.makePromise(of: Void.self)
  99. let update = self.echo.update { _ in
  100. self.clientCallbackLoop.preconditionInEventLoop()
  101. response.succeed(())
  102. }
  103. self.assertClientCallbackEventLoop(update.eventLoop)
  104. self.assertClientCallbackEventLoop(update.initialMetadata.eventLoop)
  105. self.assertClientCallbackEventLoop(update.trailingMetadata.eventLoop)
  106. self.assertClientCallbackEventLoop(update.status.eventLoop)
  107. XCTAssertNoThrow(try update.sendMessage(.with { $0.text = "a" }).wait())
  108. XCTAssertNoThrow(try update.sendEnd().wait())
  109. XCTAssertNoThrow(try response.futureResult.wait())
  110. assertThat(try update.status.wait(), .hasCode(.ok))
  111. }
  112. }