InterceptorsAsyncTests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #if compiler(>=5.5)
  17. import EchoImplementation
  18. import EchoModel
  19. import GRPC
  20. import HelloWorldModel
  21. import NIOCore
  22. import NIOHPACK
  23. import NIOPosix
  24. import SwiftProtobuf
  25. import XCTest
  26. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  27. class InterceptorsAsyncTests: GRPCTestCase {
  28. private var group: EventLoopGroup!
  29. private var server: Server!
  30. private var connection: ClientConnection!
  31. private var echo: Echo_EchoAsyncClient!
  32. override func setUp() {
  33. super.setUp()
  34. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  35. self.group = group
  36. let server = try! Server.insecure(group: group)
  37. .withServiceProviders([EchoProvider()])
  38. .withLogger(self.serverLogger)
  39. .bind(host: "127.0.0.1", port: 0)
  40. .wait()
  41. self.server = server
  42. let connection = ClientConnection.insecure(group: group)
  43. .withBackgroundActivityLogger(self.clientLogger)
  44. .connect(host: "127.0.0.1", port: server.channel.localAddress!.port!)
  45. self.connection = connection
  46. self.echo = Echo_EchoAsyncClient(
  47. channel: connection,
  48. defaultCallOptions: CallOptions(logger: self.clientLogger),
  49. interceptors: ReversingInterceptors()
  50. )
  51. }
  52. override func tearDown() {
  53. if let connection = self.connection {
  54. XCTAssertNoThrow(try connection.close().wait())
  55. }
  56. if let server = self.server {
  57. XCTAssertNoThrow(try server.close().wait())
  58. }
  59. if let group = self.group {
  60. XCTAssertNoThrow(try group.syncShutdownGracefully())
  61. }
  62. super.tearDown()
  63. }
  64. func testUnaryCall() { XCTAsyncTest {
  65. let get = try await self.echo.get(.with { $0.text = "hello" })
  66. await assertThat(get, .is(.with { $0.text = "hello :teg ohce tfiwS" }))
  67. } }
  68. func testMakingUnaryCall() { XCTAsyncTest {
  69. let call = self.echo.makeGetCall(.with { $0.text = "hello" })
  70. await assertThat(try await call.response, .is(.with { $0.text = "hello :teg ohce tfiwS" }))
  71. } }
  72. func testClientStreamingSequence() { XCTAsyncTest {
  73. let requests = ["1 2", "3 4"].map { item in
  74. Echo_EchoRequest.with { $0.text = item }
  75. }
  76. let response = try await self.echo.collect(requests, callOptions: .init())
  77. await assertThat(response, .is(.with { $0.text = "3 4 1 2 :tcelloc ohce tfiwS" }))
  78. } }
  79. func testClientStreamingAsyncSequence() { XCTAsyncTest {
  80. let stream = AsyncStream<Echo_EchoRequest> { continuation in
  81. continuation.yield(.with { $0.text = "1 2" })
  82. continuation.yield(.with { $0.text = "3 4" })
  83. continuation.finish()
  84. }
  85. let response = try await self.echo.collect(stream, callOptions: .init())
  86. await assertThat(response, .is(.with { $0.text = "3 4 1 2 :tcelloc ohce tfiwS" }))
  87. } }
  88. func testMakingCallClientStreaming() { XCTAsyncTest {
  89. let call = self.echo.makeCollectCall(callOptions: .init())
  90. try await call.requestStream.send(.with { $0.text = "1 2" })
  91. try await call.requestStream.send(.with { $0.text = "3 4" })
  92. try await call.requestStream.finish()
  93. await assertThat(
  94. try await call.response,
  95. .is(.with { $0.text = "3 4 1 2 :tcelloc ohce tfiwS" })
  96. )
  97. } }
  98. func testServerStreaming() { XCTAsyncTest {
  99. let responses = self.echo.expand(.with { $0.text = "hello" }, callOptions: .init())
  100. for try await response in responses {
  101. // Expand splits on spaces, so we only expect one response.
  102. await assertThat(response, .is(.with { $0.text = "hello :)0( dnapxe ohce tfiwS" }))
  103. }
  104. } }
  105. func testMakingCallServerStreaming() { XCTAsyncTest {
  106. let call = self.echo.makeExpandCall(.with { $0.text = "hello" }, callOptions: .init())
  107. for try await response in call.responses {
  108. // Expand splits on spaces, so we only expect one response.
  109. await assertThat(response, .is(.with { $0.text = "hello :)0( dnapxe ohce tfiwS" }))
  110. }
  111. } }
  112. func testBidirectionalStreaming() { XCTAsyncTest {
  113. let requests = ["1 2", "3 4"].map { item in
  114. Echo_EchoRequest.with { $0.text = item }
  115. }
  116. let responses = self.echo.update(requests, callOptions: .init())
  117. var count = 0
  118. for try await response in responses {
  119. switch count {
  120. case 0:
  121. await assertThat(response, .is(.with { $0.text = "1 2 :)0( etadpu ohce tfiwS" }))
  122. case 1:
  123. await assertThat(response, .is(.with { $0.text = "3 4 :)1( etadpu ohce tfiwS" }))
  124. default:
  125. XCTFail("Got more than 2 responses")
  126. }
  127. count += 1
  128. }
  129. } }
  130. func testMakingCallBidirectionalStreaming() { XCTAsyncTest {
  131. let call = self.echo.makeUpdateCall(callOptions: .init())
  132. try await call.requestStream.send(.with { $0.text = "1 2" })
  133. try await call.requestStream.send(.with { $0.text = "3 4" })
  134. try await call.requestStream.finish()
  135. var count = 0
  136. for try await response in call.responses {
  137. switch count {
  138. case 0:
  139. await assertThat(response, .is(.with { $0.text = "1 2 :)0( etadpu ohce tfiwS" }))
  140. case 1:
  141. await assertThat(response, .is(.with { $0.text = "3 4 :)1( etadpu ohce tfiwS" }))
  142. default:
  143. XCTFail("Got more than 2 responses")
  144. }
  145. count += 1
  146. }
  147. } }
  148. }
  149. #endif