InProcessServerTransportTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2023, 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 XCTest
  17. @testable import GRPCCore
  18. @testable import GRPCInProcessTransport
  19. @available(gRPCSwift 2.0, *)
  20. final class InProcessServerTransportTests: XCTestCase {
  21. func testStartListening() async throws {
  22. let transport = InProcessTransport.Server(peer: "in-process:1234")
  23. let outbound = GRPCAsyncThrowingStream.makeStream(of: RPCResponsePart<[UInt8]>.self)
  24. let stream = RPCStream<
  25. RPCAsyncSequence<RPCRequestPart<[UInt8]>, any Error>,
  26. RPCWriter<RPCResponsePart<[UInt8]>>.Closable
  27. >(
  28. descriptor: .testTest,
  29. inbound: RPCAsyncSequence<RPCRequestPart, any Error>(
  30. wrapping: AsyncThrowingStream {
  31. $0.yield(.message([42]))
  32. $0.finish()
  33. }
  34. ),
  35. outbound: RPCWriter.Closable(wrapping: outbound.continuation)
  36. )
  37. try await withThrowingTaskGroup(of: Void.self) { group in
  38. group.addTask {
  39. try await transport.listen { stream, context in
  40. XCTAssertEqual(context.descriptor, stream.descriptor)
  41. let partValue = try? await stream.inbound.reduce(into: []) { $0.append($1) }
  42. XCTAssertEqual(partValue, [.message([42])])
  43. transport.beginGracefulShutdown()
  44. }
  45. }
  46. try transport.acceptStream(stream)
  47. }
  48. }
  49. func testStopListening() async throws {
  50. let transport = InProcessTransport.Server(peer: "in-process:1234")
  51. let firstStreamOutbound = GRPCAsyncThrowingStream.makeStream(of: RPCResponsePart<[UInt8]>.self)
  52. let firstStream = RPCStream<
  53. RPCAsyncSequence<RPCRequestPart<[UInt8]>, any Error>,
  54. RPCWriter<RPCResponsePart<[UInt8]>>.Closable
  55. >(
  56. descriptor: .testTest,
  57. inbound: RPCAsyncSequence(
  58. wrapping: AsyncThrowingStream {
  59. $0.yield(.message([42]))
  60. $0.finish()
  61. }
  62. ),
  63. outbound: RPCWriter.Closable(wrapping: firstStreamOutbound.continuation)
  64. )
  65. try transport.acceptStream(firstStream)
  66. try await transport.listen { stream, context in
  67. let firstStreamMessages = try? await stream.inbound.reduce(into: []) {
  68. $0.append($1)
  69. }
  70. XCTAssertEqual(firstStreamMessages, [.message([42])])
  71. transport.beginGracefulShutdown()
  72. let secondStreamOutbound = GRPCAsyncThrowingStream.makeStream(
  73. of: RPCResponsePart<[UInt8]>.self
  74. )
  75. let secondStream = RPCStream<
  76. RPCAsyncSequence<RPCRequestPart<[UInt8]>, any Error>,
  77. RPCWriter<RPCResponsePart<[UInt8]>>.Closable
  78. >(
  79. descriptor: .testTest,
  80. inbound: RPCAsyncSequence(
  81. wrapping: AsyncThrowingStream {
  82. $0.yield(.message([42]))
  83. $0.finish()
  84. }
  85. ),
  86. outbound: RPCWriter.Closable(wrapping: secondStreamOutbound.continuation)
  87. )
  88. XCTAssertThrowsError(ofType: RPCError.self) {
  89. try transport.acceptStream(secondStream)
  90. } errorHandler: { error in
  91. XCTAssertEqual(error.code, .failedPrecondition)
  92. XCTAssertEqual(error.message, "The server transport is closed.")
  93. }
  94. }
  95. }
  96. }