|
| 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 | + |
| 17 | +import XCTest |
| 18 | + |
| 19 | +@testable import GRPCCore |
| 20 | + |
| 21 | +final class InProcessServerTransportTest: XCTestCase { |
| 22 | + func testStartListening() async throws { |
| 23 | + let transport = InProcessServerTransport() |
| 24 | + let stream = RPCStream<RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable>( |
| 25 | + descriptor: .init(service: "testService", method: "testMethod"), |
| 26 | + inbound: .elements([.message([42])]), |
| 27 | + outbound: .init(wrapping: BufferedStream.Source(storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))))) |
| 28 | + ) |
| 29 | + |
| 30 | + let streamSequence = transport.listen() |
| 31 | + var streamSequenceInterator = streamSequence.makeAsyncIterator() |
| 32 | + |
| 33 | + transport.acceptStream(stream) |
| 34 | + |
| 35 | + let testStream = try await streamSequenceInterator.next() |
| 36 | + var inboundIterator = testStream?.inbound.makeAsyncIterator() |
| 37 | + let rpcRequestPart = try await inboundIterator?.next() |
| 38 | + XCTAssertEqual(rpcRequestPart, .message([42])) |
| 39 | + } |
| 40 | + |
| 41 | + func testStopListening() async throws { |
| 42 | + let transport = InProcessServerTransport() |
| 43 | + let firstStream = RPCStream<RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable>( |
| 44 | + descriptor: .init(service: "testService1", method: "testMethod1"), |
| 45 | + inbound: .elements([.message([42])]), |
| 46 | + outbound: .init(wrapping: BufferedStream.Source(storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))))) |
| 47 | + ) |
| 48 | + |
| 49 | + let streamSequence = transport.listen() |
| 50 | + var streamSequenceInterator = streamSequence.makeAsyncIterator() |
| 51 | + |
| 52 | + transport.acceptStream(firstStream) |
| 53 | + |
| 54 | + let firstTestStream = try await streamSequenceInterator.next() |
| 55 | + var inboundIterator = firstTestStream?.inbound.makeAsyncIterator() |
| 56 | + let rpcRequestPart = try await inboundIterator?.next() |
| 57 | + XCTAssertEqual(rpcRequestPart, .message([42])) |
| 58 | + |
| 59 | + transport.stopListening() |
| 60 | + |
| 61 | + let secondStream = RPCStream<RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable>( |
| 62 | + descriptor: .init(service: "testService1", method: "testMethod1"), |
| 63 | + inbound: .elements([.message([42])]), |
| 64 | + outbound: .init(wrapping: BufferedStream.Source(storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))))) |
| 65 | + ) |
| 66 | + |
| 67 | + transport.acceptStream(secondStream) |
| 68 | + let secondTestStream = try await streamSequenceInterator.next() |
| 69 | + XCTAssertNil(secondTestStream) |
| 70 | + } |
| 71 | +} |
0 commit comments