InProcessServerTransportTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  20. final class InProcessServerTransportTests: XCTestCase {
  21. func testStartListening() async throws {
  22. let transport = InProcessServerTransport()
  23. let outbound = AsyncThrowingStream.makeStream(of: RPCResponsePart.self)
  24. let stream = RPCStream<
  25. RPCAsyncSequence<RPCRequestPart, any Error>,
  26. RPCWriter<RPCResponsePart>.Closable
  27. >(
  28. descriptor: .init(service: "testService", method: "testMethod"),
  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 in
  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 = InProcessServerTransport()
  50. let firstStreamOutbound = AsyncThrowingStream.makeStream(of: RPCResponsePart.self)
  51. let firstStream = RPCStream<
  52. RPCAsyncSequence<RPCRequestPart, any Error>, RPCWriter<RPCResponsePart>.Closable
  53. >(
  54. descriptor: .init(service: "testService1", method: "testMethod1"),
  55. inbound: RPCAsyncSequence(
  56. wrapping: AsyncThrowingStream {
  57. $0.yield(.message([42]))
  58. $0.finish()
  59. }
  60. ),
  61. outbound: RPCWriter.Closable(wrapping: firstStreamOutbound.continuation)
  62. )
  63. try transport.acceptStream(firstStream)
  64. try await transport.listen { stream in
  65. let firstStreamMessages = try? await stream.inbound.reduce(into: []) {
  66. $0.append($1)
  67. }
  68. XCTAssertEqual(firstStreamMessages, [.message([42])])
  69. transport.beginGracefulShutdown()
  70. let secondStreamOutbound = AsyncThrowingStream.makeStream(of: RPCResponsePart.self)
  71. let secondStream = RPCStream<
  72. RPCAsyncSequence<RPCRequestPart, any Error>, RPCWriter<RPCResponsePart>.Closable
  73. >(
  74. descriptor: .init(service: "testService1", method: "testMethod1"),
  75. inbound: RPCAsyncSequence(
  76. wrapping: AsyncThrowingStream {
  77. $0.yield(.message([42]))
  78. $0.finish()
  79. }
  80. ),
  81. outbound: RPCWriter.Closable(wrapping: secondStreamOutbound.continuation)
  82. )
  83. XCTAssertThrowsError(ofType: RPCError.self) {
  84. try transport.acceptStream(secondStream)
  85. } errorHandler: { error in
  86. XCTAssertEqual(error.code, .failedPrecondition)
  87. XCTAssertEqual(error.message, "The server transport is closed.")
  88. }
  89. }
  90. }
  91. }