2
0

InterceptorsAsyncTests.swift 5.6 KB

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