ClientEventLoopPreferenceTests.swift 5.1 KB

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