InProcessServerTransportTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = InProcessTransport.Server()
  23. let outbound = GRPCAsyncThrowingStream.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, 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()
  51. let firstStreamOutbound = GRPCAsyncThrowingStream.makeStream(of: RPCResponsePart.self)
  52. let firstStream = RPCStream<
  53. RPCAsyncSequence<RPCRequestPart, any Error>, RPCWriter<RPCResponsePart>.Closable
  54. >(
  55. descriptor: .init(service: "testService1", method: "testMethod1"),
  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(of: RPCResponsePart.self)
  72. let secondStream = RPCStream<
  73. RPCAsyncSequence<RPCRequestPart, any Error>, RPCWriter<RPCResponsePart>.Closable
  74. >(
  75. descriptor: .init(service: "testService1", method: "testMethod1"),
  76. inbound: RPCAsyncSequence(
  77. wrapping: AsyncThrowingStream {
  78. $0.yield(.message([42]))
  79. $0.finish()
  80. }
  81. ),
  82. outbound: RPCWriter.Closable(wrapping: secondStreamOutbound.continuation)
  83. )
  84. XCTAssertThrowsError(ofType: RPCError.self) {
  85. try transport.acceptStream(secondStream)
  86. } errorHandler: { error in
  87. XCTAssertEqual(error.code, .failedPrecondition)
  88. XCTAssertEqual(error.message, "The server transport is closed.")
  89. }
  90. }
  91. }
  92. }