|
| 1 | +/* |
| 2 | + * Copyright 2018, 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 Foundation |
| 17 | +@testable import SwiftGRPC |
| 18 | +import XCTest |
| 19 | + |
| 20 | +class ServiceClientTests: BasicEchoTestCase { |
| 21 | + private lazy var sharedChannel = Channel(address: address, secure: false) |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + super.setUp() |
| 25 | + sharedChannel = Channel(address: address, secure: false) |
| 26 | + } |
| 27 | + |
| 28 | + func testSharingChannelBetweenClientsUnaryAsync() { |
| 29 | + let firstCallExpectation = expectation(description: "First call completes successfully") |
| 30 | + let secondCallExpectation = expectation(description: "Second call completes successfully") |
| 31 | + |
| 32 | + do { |
| 33 | + let client1 = Echo_EchoServiceClient(channel: sharedChannel) |
| 34 | + try _ = client1.get(Echo_EchoRequest(text: "foo")) { _, callResult in |
| 35 | + XCTAssertEqual(.ok, callResult.statusCode) |
| 36 | + firstCallExpectation.fulfill() |
| 37 | + } |
| 38 | + |
| 39 | + let client2 = Echo_EchoServiceClient(channel: sharedChannel) |
| 40 | + try _ = client2.get(Echo_EchoRequest(text: "foo")) { _, callResult in |
| 41 | + XCTAssertEqual(.ok, callResult.statusCode) |
| 42 | + secondCallExpectation.fulfill() |
| 43 | + } |
| 44 | + } catch let error { |
| 45 | + XCTFail(error.localizedDescription) |
| 46 | + } |
| 47 | + |
| 48 | + waitForExpectations(timeout: defaultTimeout) |
| 49 | + } |
| 50 | + |
| 51 | + func testSharedChannelStillWorksAfterFirstUnaryClientCompletes() { |
| 52 | + do { |
| 53 | + let client1 = Echo_EchoServiceClient(channel: sharedChannel) |
| 54 | + let response1 = try client1.get(Echo_EchoRequest(text: "foo")).text |
| 55 | + XCTAssertEqual("Swift echo get: foo", response1) |
| 56 | + } catch let error { |
| 57 | + XCTFail(error.localizedDescription) |
| 58 | + } |
| 59 | + |
| 60 | + do { |
| 61 | + let client2 = Echo_EchoServiceClient(channel: sharedChannel) |
| 62 | + let response2 = try client2.get(Echo_EchoRequest(text: "foo")).text |
| 63 | + XCTAssertEqual("Swift echo get: foo", response2) |
| 64 | + } catch let error { |
| 65 | + XCTFail(error.localizedDescription) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments