3 * Copyright 2018 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #import <XCTest/XCTest.h>
21 #import "../../GRPCClient/GRPCCallOptions.h"
22 #import "../../GRPCClient/private/GRPCChannel.h"
23 #import "../../GRPCClient/private/GRPCChannelPool+Test.h"
24 #import "../../GRPCClient/private/GRPCChannelPool.h"
25 #import "../../GRPCClient/private/GRPCCompletionQueue.h"
26 #import "../../GRPCClient/private/GRPCWrappedCall.h"
28 static NSString *kDummyHost = @"dummy.host";
29 static NSString *kDummyPath = @"/dummy/path";
31 @interface ChannelTests : XCTestCase
35 @implementation ChannelTests
41 - (void)testPooledChannelCreatingChannel {
42 GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
43 GRPCChannelConfiguration *config =
44 [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options];
45 GRPCPooledChannel *channel = [[GRPCPooledChannel alloc] initWithChannelConfiguration:config];
46 GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
47 GRPCWrappedCall *wrappedCall =
48 [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
49 XCTAssertNotNil(channel.wrappedChannel);
53 - (void)testTimedDestroyChannel {
54 const NSTimeInterval kDestroyDelay = 1.0;
55 GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
56 GRPCChannelConfiguration *config =
57 [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options];
58 GRPCPooledChannel *channel =
59 [[GRPCPooledChannel alloc] initWithChannelConfiguration:config destroyDelay:kDestroyDelay];
60 GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
61 GRPCWrappedCall *wrappedCall;
62 GRPCChannel *wrappedChannel;
64 wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
65 XCTAssertNotNil(channel.wrappedChannel);
67 // Unref and ref channel immediately; expect using the same raw channel.
68 wrappedChannel = channel.wrappedChannel;
71 wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
72 XCTAssertEqual(channel.wrappedChannel, wrappedChannel);
74 // Unref and ref channel after destroy delay; expect a new raw channel.
77 sleep(kDestroyDelay + 1);
78 XCTAssertNil(channel.wrappedChannel);
79 wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
80 XCTAssertNotEqual(channel.wrappedChannel, wrappedChannel);
83 - (void)testDisconnect {
84 const NSTimeInterval kDestroyDelay = 1.0;
85 GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
86 GRPCChannelConfiguration *config =
87 [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options];
88 GRPCPooledChannel *channel =
89 [[GRPCPooledChannel alloc] initWithChannelConfiguration:config destroyDelay:kDestroyDelay];
90 GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
91 GRPCWrappedCall *wrappedCall =
92 [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
93 XCTAssertNotNil(channel.wrappedChannel);
95 // Disconnect; expect wrapped channel to be dropped
97 XCTAssertNil(channel.wrappedChannel);
99 // Create a new call and unref the old call; confirm that destroy of the old call does not make
100 // the channel disconnect, even after the destroy delay.
101 GRPCWrappedCall *wrappedCall2 =
102 [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
103 XCTAssertNotNil(channel.wrappedChannel);
104 GRPCChannel *wrappedChannel = channel.wrappedChannel;
106 sleep(kDestroyDelay + 1);
107 XCTAssertNotNil(channel.wrappedChannel);
108 XCTAssertEqual(wrappedChannel, channel.wrappedChannel);