InProcessServerTransportTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  20. final class InProcessServerTransportTests: XCTestCase {
  21. func testStartListening() async throws {
  22. let transport = InProcessServerTransport()
  23. let stream = RPCStream<RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable>(
  24. descriptor: .init(service: "testService", method: "testMethod"),
  25. inbound: RPCAsyncSequence(
  26. wrapping: AsyncStream {
  27. $0.yield(.message([42]))
  28. $0.finish()
  29. }
  30. ),
  31. outbound: .init(
  32. wrapping: BufferedStream.Source(
  33. storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1)))
  34. )
  35. )
  36. )
  37. let messages = LockedValueBox<[RPCRequestPart]?>(nil)
  38. try await withThrowingTaskGroup(of: Void.self) { group in
  39. group.addTask {
  40. try await transport.listen { stream in
  41. let partValue = try? await stream.inbound.reduce(into: []) { $0.append($1) }
  42. messages.withLockedValue { $0 = partValue }
  43. transport.stopListening()
  44. }
  45. }
  46. try transport.acceptStream(stream)
  47. }
  48. XCTAssertEqual(messages.withLockedValue { $0 }, [.message([42])])
  49. }
  50. func testStopListening() async throws {
  51. let transport = InProcessServerTransport()
  52. let firstStream = RPCStream<
  53. RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable
  54. >(
  55. descriptor: .init(service: "testService1", method: "testMethod1"),
  56. inbound: RPCAsyncSequence(
  57. wrapping: AsyncStream {
  58. $0.yield(.message([42]))
  59. $0.finish()
  60. }
  61. ),
  62. outbound: .init(
  63. wrapping: BufferedStream.Source(
  64. storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1)))
  65. )
  66. )
  67. )
  68. try transport.acceptStream(firstStream)
  69. try await transport.listen { stream in
  70. let firstStreamMessages = try? await stream.inbound.reduce(into: []) {
  71. $0.append($1)
  72. }
  73. XCTAssertEqual(firstStreamMessages, [.message([42])])
  74. transport.stopListening()
  75. let secondStream = RPCStream<
  76. RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable
  77. >(
  78. descriptor: .init(service: "testService1", method: "testMethod1"),
  79. inbound: RPCAsyncSequence(
  80. wrapping: AsyncStream {
  81. $0.yield(.message([42]))
  82. $0.finish()
  83. }
  84. ),
  85. outbound: .init(
  86. wrapping: BufferedStream.Source(
  87. storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1)))
  88. )
  89. )
  90. )
  91. XCTAssertThrowsError(ofType: RPCError.self) {
  92. try transport.acceptStream(secondStream)
  93. } errorHandler: { error in
  94. XCTAssertEqual(error.code, .failedPrecondition)
  95. XCTAssertEqual(error.message, "The server transport is closed.")
  96. }
  97. }
  98. }
  99. }