InProcessServerTransportTests.swift 3.5 KB

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