Skip to content

Commit a591384

Browse files
authored
Merge pull request #203 from rebello95/channel-sharing-tests
Add ServiceClient tests for shared channels
2 parents 70adb86 + 1525c5a commit a591384

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

Tests/SwiftGRPCTests/BasicEchoTestCase.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,20 @@ extension Echo_EchoResponse {
3232

3333
class BasicEchoTestCase: XCTestCase {
3434
func makeProvider() -> Echo_EchoProvider { return EchoProvider() }
35-
36-
var defaultTimeout: TimeInterval { return 1.0 }
37-
35+
3836
var provider: Echo_EchoProvider!
3937
var server: Echo_EchoServer!
4038
var client: Echo_EchoServiceClient!
4139

40+
var defaultTimeout: TimeInterval { return 1.0 }
4241
var secure: Bool { return false }
43-
42+
var address: String { return "localhost:5050" }
43+
4444
override func setUp() {
4545
super.setUp()
4646

4747
provider = makeProvider()
4848

49-
let address = "localhost:5050"
5049
if secure {
5150
let certificateString = String(data: certificateForTests, encoding: .utf8)!
5251
server = Echo_EchoServer(address: address,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)