2
0

InProcessServerTransportTests.swift 3.4 KB

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