|
| 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( |
| 28 | + wrapping: BufferedStream.Source( |
| 29 | + storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))) |
| 30 | + ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + |
| 34 | + let streamSequence = transport.listen() |
| 35 | + var streamSequenceInterator = streamSequence.makeAsyncIterator() |
| 36 | + |
| 37 | + try transport.acceptStream(stream) |
| 38 | + |
| 39 | + let testStream = try await streamSequenceInterator.next() |
| 40 | + let messages = try await testStream?.inbound.collect() |
| 41 | + XCTAssertEqual(messages, [.message([42])]) |
| 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: .elements([.message([42])]), |
| 51 | + outbound: .init( |
| 52 | + wrapping: BufferedStream.Source( |
| 53 | + storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))) |
| 54 | + ) |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + let streamSequence = transport.listen() |
| 59 | + var streamSequenceInterator = streamSequence.makeAsyncIterator() |
| 60 | + |
| 61 | + try transport.acceptStream(firstStream) |
| 62 | + |
| 63 | + let firstTestStream = try await streamSequenceInterator.next() |
| 64 | + let firstStreamMessages = try await firstTestStream?.inbound.collect() |
| 65 | + XCTAssertEqual(firstStreamMessages, [.message([42])]) |
| 66 | + |
| 67 | + transport.stopListening() |
| 68 | + |
| 69 | + let secondStream = RPCStream< |
| 70 | + RPCAsyncSequence<RPCRequestPart>, RPCWriter<RPCResponsePart>.Closable |
| 71 | + >( |
| 72 | + descriptor: .init(service: "testService1", method: "testMethod1"), |
| 73 | + inbound: .elements([.message([42])]), |
| 74 | + outbound: .init( |
| 75 | + wrapping: BufferedStream.Source( |
| 76 | + storage: .init(backPressureStrategy: .watermark(.init(low: 1, high: 1))) |
| 77 | + ) |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + XCTAssertThrowsRPCError(try transport.acceptStream(secondStream)) { error in |
| 82 | + XCTAssertEqual(error.code, .failedPrecondition) |
| 83 | + } |
| 84 | + |
| 85 | + let secondTestStream = try await streamSequenceInterator.next() |
| 86 | + XCTAssertNil(secondTestStream) |
| 87 | + } |
| 88 | +} |
0 commit comments