InProcessServerTransportTests.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 10.15, iOS 13, tvOS 13, watchOS 6, *)
  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 streamSequence = try await transport.listen()
  38. var streamSequenceInterator = streamSequence.makeAsyncIterator()
  39. try transport.acceptStream(stream)
  40. let testStream = try await streamSequenceInterator.next()
  41. let messages = try await testStream?.inbound.reduce(into: []) { $0.append($1) }
  42. XCTAssertEqual(messages, [.message([42])])
  43. }
  44. func testStopListening() async throws {
  45. let transport = InProcessServerTransport()
  46. let firstStream = RPCStream<
  47. RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable
  48. >(
  49. descriptor: .init(service: "testService1", method: "testMethod1"),
  50. inbound: RPCAsyncSequence(
  51. wrapping: AsyncStream {
  52. $0.yield(.message([42]))
  53. $0.finish()
  54. }
  55. ),
  56. outbound: .init(
  57. wrapping: BufferedStream.Source(
  58. storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1)))
  59. )
  60. )
  61. )
  62. let streamSequence = try await transport.listen()
  63. var streamSequenceInterator = streamSequence.makeAsyncIterator()
  64. try transport.acceptStream(firstStream)
  65. let firstTestStream = try await streamSequenceInterator.next()
  66. let firstStreamMessages = try await firstTestStream?.inbound.reduce(into: []) { $0.append($1) }
  67. XCTAssertEqual(firstStreamMessages, [.message([42])])
  68. transport.stopListening()
  69. let secondStream = RPCStream<
  70. RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable
  71. >(
  72. descriptor: .init(service: "testService1", method: "testMethod1"),
  73. inbound: RPCAsyncSequence(
  74. wrapping: AsyncStream {
  75. $0.yield(.message([42]))
  76. $0.finish()
  77. }
  78. ),
  79. outbound: .init(
  80. wrapping: BufferedStream.Source(
  81. storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1)))
  82. )
  83. )
  84. )
  85. XCTAssertThrowsError(ofType: RPCError.self) {
  86. try transport.acceptStream(secondStream)
  87. } errorHandler: { error in
  88. XCTAssertEqual(error.code, .failedPrecondition)
  89. }
  90. let secondTestStream = try await streamSequenceInterator.next()
  91. XCTAssertNil(secondTestStream)
  92. }
  93. }