InProcessServerTransport.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  17. /// An in-process implementation of a ``ServerTransport``.
  18. ///
  19. /// This is useful when you're interested in testing your application without any actual networking layers
  20. /// involved, as the client and server will communicate directly with each other via in-process streams.
  21. ///
  22. /// To use this server, you call ``listen()`` and iterate over the returned `AsyncSequence` to get all
  23. /// RPC requests made from clients (as ``RPCStream``s).
  24. /// To stop listening to new requests, call ``stopListening()``.
  25. ///
  26. /// - SeeAlso: ``ClientTransport``
  27. public struct InProcessServerTransport: ServerTransport, Sendable {
  28. public typealias Inbound = RPCAsyncSequence<RPCRequestPart>
  29. public typealias Outbound = RPCWriter<RPCResponsePart>.Closable
  30. private let newStreams: AsyncStream<RPCStream<Inbound, Outbound>>
  31. private let newStreamsContinuation: AsyncStream<RPCStream<Inbound, Outbound>>.Continuation
  32. /// Creates a new instance of ``InProcessServerTransport``.
  33. public init() {
  34. (self.newStreams, self.newStreamsContinuation) = AsyncStream.makeStream()
  35. }
  36. /// Publish a new ``RPCStream``, which will be returned by the transport's ``RPCAsyncSequence``,
  37. /// returned when calling ``listen()``.
  38. ///
  39. /// - Parameter stream: The new ``RPCStream`` to publish.
  40. /// - Throws: ``RPCError`` with code ``RPCError/Code-swift.struct/failedPrecondition``
  41. /// if the server transport stopped listening to new streams (i.e., if ``stopListening()`` has been called).
  42. internal func acceptStream(_ stream: RPCStream<Inbound, Outbound>) throws {
  43. let yieldResult = self.newStreamsContinuation.yield(stream)
  44. if case .terminated = yieldResult {
  45. throw RPCError(
  46. code: .failedPrecondition,
  47. message: "The server transport is closed."
  48. )
  49. }
  50. }
  51. /// Return a new ``RPCAsyncSequence`` that will contain all published ``RPCStream``s published
  52. /// to this transport using the ``acceptStream(_:)`` method.
  53. ///
  54. /// - Returns: An ``RPCAsyncSequence`` of all published ``RPCStream``s.
  55. public func listen() -> RPCAsyncSequence<RPCStream<Inbound, Outbound>> {
  56. RPCAsyncSequence(wrapping: self.newStreams)
  57. }
  58. /// Stop listening to any new ``RPCStream`` publications.
  59. ///
  60. /// All further calls to ``acceptStream(_:)`` will not produce any new elements on the
  61. /// ``RPCAsyncSequence`` returned by ``listen()``.
  62. ///
  63. /// - SeeAlso: ``ServerTransport``
  64. public func stopListening() {
  65. self.newStreamsContinuation.finish()
  66. }
  67. }