InProcessServerTransport.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 GRPCCore
  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. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  28. public struct InProcessServerTransport: ServerTransport, Sendable {
  29. public typealias Inbound = RPCAsyncSequence<RPCRequestPart>
  30. public typealias Outbound = RPCWriter<RPCResponsePart>.Closable
  31. private let newStreams: AsyncStream<RPCStream<Inbound, Outbound>>
  32. private let newStreamsContinuation: AsyncStream<RPCStream<Inbound, Outbound>>.Continuation
  33. /// Creates a new instance of ``InProcessServerTransport``.
  34. public init() {
  35. (self.newStreams, self.newStreamsContinuation) = AsyncStream.makeStream()
  36. }
  37. /// Publish a new ``RPCStream``, which will be returned by the transport's ``RPCAsyncSequence``,
  38. /// returned when calling ``listen()``.
  39. ///
  40. /// - Parameter stream: The new ``RPCStream`` to publish.
  41. /// - Throws: ``RPCError`` with code ``RPCError/Code-swift.struct/failedPrecondition``
  42. /// if the server transport stopped listening to new streams (i.e., if ``stopListening()`` has been called).
  43. internal func acceptStream(_ stream: RPCStream<Inbound, Outbound>) throws {
  44. let yieldResult = self.newStreamsContinuation.yield(stream)
  45. if case .terminated = yieldResult {
  46. throw RPCError(
  47. code: .failedPrecondition,
  48. message: "The server transport is closed."
  49. )
  50. }
  51. }
  52. /// Return a new ``RPCAsyncSequence`` that will contain all published ``RPCStream``s published
  53. /// to this transport using the ``acceptStream(_:)`` method.
  54. ///
  55. /// - Returns: An ``RPCAsyncSequence`` of all published ``RPCStream``s.
  56. public func listen() async throws -> RPCAsyncSequence<RPCStream<Inbound, Outbound>> {
  57. RPCAsyncSequence(wrapping: self.newStreams)
  58. }
  59. /// Stop listening to any new ``RPCStream`` publications.
  60. ///
  61. /// All further calls to ``acceptStream(_:)`` will not produce any new elements on the
  62. /// ``RPCAsyncSequence`` returned by ``listen()``.
  63. ///
  64. /// - SeeAlso: ``ServerTransport``
  65. public func stopListening() {
  66. self.newStreamsContinuation.finish()
  67. }
  68. }