Imported Upstream version 1.21.3
[platform/upstream/grpc.git] / src / objective-c / tests / UnitTests / ChannelTests.m
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #import <XCTest/XCTest.h>
20
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"
27
28 static NSString *kDummyHost = @"dummy.host";
29 static NSString *kDummyPath = @"/dummy/path";
30
31 @interface ChannelTests : XCTestCase
32
33 @end
34
35 @implementation ChannelTests
36
37 + (void)setUp {
38   grpc_init();
39 }
40
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);
50   (void)wrappedCall;
51 }
52
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;
63   @autoreleasepool {
64     wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
65     XCTAssertNotNil(channel.wrappedChannel);
66
67     // Unref and ref channel immediately; expect using the same raw channel.
68     wrappedChannel = channel.wrappedChannel;
69
70     wrappedCall = nil;
71     wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
72     XCTAssertEqual(channel.wrappedChannel, wrappedChannel);
73
74     // Unref and ref channel after destroy delay; expect a new raw channel.
75     wrappedCall = nil;
76   }
77   sleep(kDestroyDelay + 1);
78   XCTAssertNil(channel.wrappedChannel);
79   wrappedCall = [channel wrappedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
80   XCTAssertNotEqual(channel.wrappedChannel, wrappedChannel);
81 }
82
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);
94
95   // Disconnect; expect wrapped channel to be dropped
96   [channel disconnect];
97   XCTAssertNil(channel.wrappedChannel);
98
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;
105   wrappedCall = nil;
106   sleep(kDestroyDelay + 1);
107   XCTAssertNotNil(channel.wrappedChannel);
108   XCTAssertEqual(wrappedChannel, channel.wrappedChannel);
109   (void)wrappedCall2;
110 }
111
112 @end