Imported Upstream version 1.21.3
[platform/upstream/grpc.git] / src / objective-c / tests / InteropTestsCallOptions / InteropTestsCallOptions.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 <RemoteTest/Messages.pbobjc.h>
22 #import <RemoteTest/Test.pbobjc.h>
23 #import <RemoteTest/Test.pbrpc.h>
24 #import <RxLibrary/GRXBufferedPipe.h>
25 #import <RxLibrary/GRXWriter+Immediate.h>
26 #import <grpc/grpc.h>
27
28 #define NSStringize_helper(x) #x
29 #define NSStringize(x) @NSStringize_helper(x)
30 static NSString *kRemoteHost = NSStringize(HOST_PORT_REMOTE);
31 const int32_t kRemoteInteropServerOverhead = 12;
32
33 static const NSTimeInterval TEST_TIMEOUT = 16000;
34
35 @interface InteropTestsCallOptions : XCTestCase
36
37 @end
38
39 @implementation InteropTestsCallOptions {
40   RMTTestService *_service;
41 }
42
43 - (void)setUp {
44   self.continueAfterFailure = NO;
45   _service = [RMTTestService serviceWithHost:kRemoteHost];
46   _service.options = [[GRPCCallOptions alloc] init];
47 }
48
49 - (void)test4MBResponsesAreAccepted {
50   __weak XCTestExpectation *expectation = [self expectationWithDescription:@"MaxResponseSize"];
51
52   RMTSimpleRequest *request = [RMTSimpleRequest message];
53   const int32_t kPayloadSize =
54       4 * 1024 * 1024 - kRemoteInteropServerOverhead;  // 4MB - encoding overhead
55   request.responseSize = kPayloadSize;
56
57   [_service unaryCallWithRequest:request
58                          handler:^(RMTSimpleResponse *response, NSError *error) {
59                            XCTAssertNil(error, @"Finished with unexpected error: %@", error);
60                            XCTAssertEqual(response.payload.body.length, kPayloadSize);
61                            [expectation fulfill];
62                          }];
63
64   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
65 }
66
67 - (void)testResponsesOverMaxSizeFailWithActionableMessage {
68   __weak XCTestExpectation *expectation = [self expectationWithDescription:@"ResponseOverMaxSize"];
69
70   RMTSimpleRequest *request = [RMTSimpleRequest message];
71   const int32_t kPayloadSize =
72       4 * 1024 * 1024 - kRemoteInteropServerOverhead + 1;  // 1B over max size
73   request.responseSize = kPayloadSize;
74
75   [_service unaryCallWithRequest:request
76                          handler:^(RMTSimpleResponse *response, NSError *error) {
77                            XCTAssertEqualObjects(
78                                error.localizedDescription,
79                                @"Received message larger than max (4194305 vs. 4194304)");
80                            [expectation fulfill];
81                          }];
82
83   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
84 }
85
86 - (void)testResponsesOver4MBAreAcceptedIfOptedIn {
87   __weak XCTestExpectation *expectation =
88       [self expectationWithDescription:@"HigherResponseSizeLimit"];
89
90   RMTSimpleRequest *request = [RMTSimpleRequest message];
91   const size_t kPayloadSize = 5 * 1024 * 1024;  // 5MB
92   request.responseSize = kPayloadSize;
93
94   GRPCProtoCall *rpc = [_service
95       RPCToUnaryCallWithRequest:request
96                         handler:^(RMTSimpleResponse *response, NSError *error) {
97                           XCTAssertNil(error, @"Finished with unexpected error: %@", error);
98                           XCTAssertEqual(response.payload.body.length, kPayloadSize);
99                           [expectation fulfill];
100                         }];
101   GRPCCallOptions *options = rpc.options;
102   options.responseSizeLimit = 6 * 1024 * 1024;
103
104   [rpc start];
105
106   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
107 }
108
109 - (void)testPerformanceExample {
110   // This is an example of a performance test case.
111   [self measureBlock:^{
112       // Put the code you want to measure the time of here.
113   }];
114 }
115
116 @end