Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Jorge Canizales | 9195233 | 2015-07-31 17:50:49 -0700 | [diff] [blame] | 34 | #import "InteropTests.h" |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 35 | |
Jorge Canizales | 9195233 | 2015-07-31 17:50:49 -0700 | [diff] [blame] | 36 | #include <grpc/status.h> |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 37 | |
Makarand Dharmapurikar | de733bd | 2016-05-25 10:38:56 -0700 | [diff] [blame] | 38 | #import <Cronet/Cronet.h> |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 39 | #import <GRPCClient/GRPCCall+Tests.h> |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 40 | #import <GRPCClient/GRPCCall+Cronet.h> |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 41 | #import <ProtoRPC/ProtoRPC.h> |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 42 | #import <RemoteTest/Empty.pbobjc.h> |
| 43 | #import <RemoteTest/Messages.pbobjc.h> |
| 44 | #import <RemoteTest/Test.pbobjc.h> |
| 45 | #import <RemoteTest/Test.pbrpc.h> |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 46 | #import <RxLibrary/GRXBufferedPipe.h> |
| 47 | #import <RxLibrary/GRXWriter+Immediate.h> |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 48 | |
| 49 | // Convenience constructors for the generated proto messages: |
| 50 | |
| 51 | @interface RMTStreamingOutputCallRequest (Constructors) |
| 52 | + (instancetype)messageWithPayloadSize:(NSNumber *)payloadSize |
| 53 | requestedResponseSize:(NSNumber *)responseSize; |
| 54 | @end |
| 55 | |
| 56 | @implementation RMTStreamingOutputCallRequest (Constructors) |
| 57 | + (instancetype)messageWithPayloadSize:(NSNumber *)payloadSize |
| 58 | requestedResponseSize:(NSNumber *)responseSize { |
| 59 | RMTStreamingOutputCallRequest *request = [self message]; |
| 60 | RMTResponseParameters *parameters = [RMTResponseParameters message]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 61 | parameters.size = (int)responseSize.integerValue; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 62 | [request.responseParametersArray addObject:parameters]; |
| 63 | request.payload.body = [NSMutableData dataWithLength:payloadSize.unsignedIntegerValue]; |
| 64 | return request; |
| 65 | } |
| 66 | @end |
| 67 | |
| 68 | @interface RMTStreamingOutputCallResponse (Constructors) |
| 69 | + (instancetype)messageWithPayloadSize:(NSNumber *)payloadSize; |
| 70 | @end |
| 71 | |
| 72 | @implementation RMTStreamingOutputCallResponse (Constructors) |
| 73 | + (instancetype)messageWithPayloadSize:(NSNumber *)payloadSize { |
| 74 | RMTStreamingOutputCallResponse * response = [self message]; |
| 75 | response.payload.type = RMTPayloadType_Compressable; |
| 76 | response.payload.body = [NSMutableData dataWithLength:payloadSize.unsignedIntegerValue]; |
| 77 | return response; |
| 78 | } |
| 79 | @end |
| 80 | |
Jorge Canizales | b2bd067 | 2015-08-01 23:19:11 -0700 | [diff] [blame] | 81 | #pragma mark Tests |
| 82 | |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 83 | #ifdef GRPC_COMPILE_WITH_CRONET |
Makarand Dharmapurikar | aed1de9 | 2016-05-26 09:39:26 -0700 | [diff] [blame] | 84 | static cronet_engine *cronetEngine = NULL; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 85 | #endif |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 86 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 87 | @implementation InteropTests { |
| 88 | RMTTestService *_service; |
| 89 | } |
| 90 | |
Jorge Canizales | 945f72c | 2015-07-31 17:40:59 -0700 | [diff] [blame] | 91 | + (NSString *)host { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 92 | return nil; |
Jorge Canizales | 945f72c | 2015-07-31 17:40:59 -0700 | [diff] [blame] | 93 | } |
Jorge Canizales | ce8ee61 | 2015-07-24 20:52:49 -0700 | [diff] [blame] | 94 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 95 | - (void)setUp { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 96 | _service = self.class.host ? [RMTTestService serviceWithHost:self.class.host] : nil; |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 97 | #ifdef GRPC_COMPILE_WITH_CRONET |
Makarand Dharmapurikar | aed1de9 | 2016-05-26 09:39:26 -0700 | [diff] [blame] | 98 | if (cronetEngine == NULL) { |
| 99 | // Cronet setup |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 100 | [Cronet setHttp2Enabled:YES]; |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 101 | [Cronet start]; |
Makarand Dharmapurikar | aed1de9 | 2016-05-26 09:39:26 -0700 | [diff] [blame] | 102 | cronetEngine = [Cronet getGlobalEngine]; |
| 103 | [GRPCCall useCronetWithEngine:cronetEngine]; |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 104 | } |
| 105 | #endif |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 108 | - (void)testEmptyUnaryRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 109 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 110 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyUnary"]; |
| 111 | |
| 112 | RMTEmpty *request = [RMTEmpty message]; |
| 113 | |
| 114 | [_service emptyCallWithRequest:request handler:^(RMTEmpty *response, NSError *error) { |
| 115 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 116 | |
| 117 | id expectedResponse = [RMTEmpty message]; |
| 118 | XCTAssertEqualObjects(response, expectedResponse); |
| 119 | |
| 120 | [expectation fulfill]; |
| 121 | }]; |
| 122 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 123 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | - (void)testLargeUnaryRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 127 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 128 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"LargeUnary"]; |
| 129 | |
| 130 | RMTSimpleRequest *request = [RMTSimpleRequest message]; |
| 131 | request.responseType = RMTPayloadType_Compressable; |
| 132 | request.responseSize = 314159; |
| 133 | request.payload.body = [NSMutableData dataWithLength:271828]; |
| 134 | |
| 135 | [_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) { |
| 136 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 137 | |
| 138 | RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message]; |
| 139 | expectedResponse.payload.type = RMTPayloadType_Compressable; |
| 140 | expectedResponse.payload.body = [NSMutableData dataWithLength:314159]; |
| 141 | XCTAssertEqualObjects(response, expectedResponse); |
| 142 | |
| 143 | [expectation fulfill]; |
| 144 | }]; |
| 145 | |
Jorge Canizales | 83c57cb | 2015-08-09 16:36:49 -0700 | [diff] [blame] | 146 | [self waitForExpectationsWithTimeout:16 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | - (void)testClientStreamingRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 150 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 151 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"ClientStreaming"]; |
| 152 | |
| 153 | RMTStreamingInputCallRequest *request1 = [RMTStreamingInputCallRequest message]; |
| 154 | request1.payload.body = [NSMutableData dataWithLength:27182]; |
| 155 | |
| 156 | RMTStreamingInputCallRequest *request2 = [RMTStreamingInputCallRequest message]; |
| 157 | request2.payload.body = [NSMutableData dataWithLength:8]; |
| 158 | |
| 159 | RMTStreamingInputCallRequest *request3 = [RMTStreamingInputCallRequest message]; |
| 160 | request3.payload.body = [NSMutableData dataWithLength:1828]; |
| 161 | |
| 162 | RMTStreamingInputCallRequest *request4 = [RMTStreamingInputCallRequest message]; |
| 163 | request4.payload.body = [NSMutableData dataWithLength:45904]; |
| 164 | |
Jorge Canizales | a8c5d96 | 2015-07-16 18:55:31 -0700 | [diff] [blame] | 165 | GRXWriter *writer = [GRXWriter writerWithContainer:@[request1, request2, request3, request4]]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 166 | |
| 167 | [_service streamingInputCallWithRequestsWriter:writer |
| 168 | handler:^(RMTStreamingInputCallResponse *response, |
| 169 | NSError *error) { |
| 170 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 171 | |
| 172 | RMTStreamingInputCallResponse *expectedResponse = [RMTStreamingInputCallResponse message]; |
| 173 | expectedResponse.aggregatedPayloadSize = 74922; |
| 174 | XCTAssertEqualObjects(response, expectedResponse); |
| 175 | |
| 176 | [expectation fulfill]; |
| 177 | }]; |
| 178 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 179 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | - (void)testServerStreamingRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 183 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 184 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"ServerStreaming"]; |
| 185 | |
| 186 | NSArray *expectedSizes = @[@31415, @9, @2653, @58979]; |
| 187 | |
| 188 | RMTStreamingOutputCallRequest *request = [RMTStreamingOutputCallRequest message]; |
| 189 | for (NSNumber *size in expectedSizes) { |
| 190 | RMTResponseParameters *parameters = [RMTResponseParameters message]; |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 191 | parameters.size = (int)[size integerValue]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 192 | [request.responseParametersArray addObject:parameters]; |
| 193 | } |
| 194 | |
| 195 | __block int index = 0; |
| 196 | [_service streamingOutputCallWithRequest:request |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 197 | eventHandler:^(BOOL done, |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 198 | RMTStreamingOutputCallResponse *response, |
| 199 | NSError *error){ |
| 200 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 201 | XCTAssertTrue(done || response, @"Event handler called without an event."); |
| 202 | |
| 203 | if (response) { |
| 204 | XCTAssertLessThan(index, 4, @"More than 4 responses received."); |
| 205 | id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:expectedSizes[index]]; |
| 206 | XCTAssertEqualObjects(response, expected); |
| 207 | index += 1; |
| 208 | } |
| 209 | |
| 210 | if (done) { |
| 211 | XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index); |
| 212 | [expectation fulfill]; |
| 213 | } |
| 214 | }]; |
| 215 | |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 216 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | - (void)testPingPongRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 220 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 221 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"PingPong"]; |
| 222 | |
| 223 | NSArray *requests = @[@27182, @8, @1828, @45904]; |
| 224 | NSArray *responses = @[@31415, @9, @2653, @58979]; |
| 225 | |
| 226 | GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; |
| 227 | |
| 228 | __block int index = 0; |
| 229 | |
| 230 | id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] |
| 231 | requestedResponseSize:responses[index]]; |
| 232 | [requestsBuffer writeValue:request]; |
| 233 | |
| 234 | [_service fullDuplexCallWithRequestsWriter:requestsBuffer |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 235 | eventHandler:^(BOOL done, |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 236 | RMTStreamingOutputCallResponse *response, |
| 237 | NSError *error) { |
| 238 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 239 | XCTAssertTrue(done || response, @"Event handler called without an event."); |
| 240 | |
| 241 | if (response) { |
| 242 | XCTAssertLessThan(index, 4, @"More than 4 responses received."); |
| 243 | id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]]; |
| 244 | XCTAssertEqualObjects(response, expected); |
| 245 | index += 1; |
| 246 | if (index < 4) { |
| 247 | id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] |
| 248 | requestedResponseSize:responses[index]]; |
| 249 | [requestsBuffer writeValue:request]; |
| 250 | } else { |
| 251 | [requestsBuffer writesFinishedWithError:nil]; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (done) { |
| 256 | XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index); |
| 257 | [expectation fulfill]; |
| 258 | } |
| 259 | }]; |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 260 | [self waitForExpectationsWithTimeout:4 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 263 | #ifndef GRPC_COMPILE_WITH_CRONET |
| 264 | // TODO(makdharma@): Fix this test |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 265 | - (void)testEmptyStreamRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 266 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 267 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyStream"]; |
| 268 | [_service fullDuplexCallWithRequestsWriter:[GRXWriter emptyWriter] |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 269 | eventHandler:^(BOOL done, |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 270 | RMTStreamingOutputCallResponse *response, |
| 271 | NSError *error) { |
| 272 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 273 | XCTAssert(done, @"Unexpected response: %@", response); |
| 274 | [expectation fulfill]; |
| 275 | }]; |
| 276 | [self waitForExpectationsWithTimeout:2 handler:nil]; |
| 277 | } |
Makarand Dharmapurikar | 9b5f640 | 2016-05-20 15:57:34 -0700 | [diff] [blame] | 278 | #endif |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 279 | |
| 280 | - (void)testCancelAfterBeginRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 281 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 282 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAfterBegin"]; |
| 283 | |
| 284 | // A buffered pipe to which we never write any value acts as a writer that just hangs. |
| 285 | GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; |
| 286 | |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 287 | GRPCProtoCall *call = [_service RPCToStreamingInputCallWithRequestsWriter:requestsBuffer |
Yuchen Zeng | 8d9e838 | 2016-06-21 11:21:48 -0700 | [diff] [blame] | 288 | handler:^(RMTStreamingInputCallResponse *response, |
| 289 | NSError *error) { |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 290 | XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED); |
| 291 | [expectation fulfill]; |
| 292 | }]; |
Jorge Canizales | 47b80bc | 2016-04-30 10:32:49 -0700 | [diff] [blame] | 293 | XCTAssertEqual(call.state, GRXWriterStateNotStarted); |
| 294 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 295 | [call start]; |
Jorge Canizales | 47b80bc | 2016-04-30 10:32:49 -0700 | [diff] [blame] | 296 | XCTAssertEqual(call.state, GRXWriterStateStarted); |
| 297 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 298 | [call cancel]; |
Jorge Canizales | 47b80bc | 2016-04-30 10:32:49 -0700 | [diff] [blame] | 299 | XCTAssertEqual(call.state, GRXWriterStateFinished); |
| 300 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 301 | [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 302 | } |
| 303 | |
| 304 | - (void)testCancelAfterFirstResponseRPC { |
Jorge Canizales | 91db5ac | 2015-10-28 14:53:38 -0700 | [diff] [blame] | 305 | XCTAssertNotNil(self.class.host); |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 306 | __weak XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAfterFirstResponse"]; |
Nate Kibler | 2d32771 | 2015-09-25 11:56:05 -0700 | [diff] [blame] | 307 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 308 | // A buffered pipe to which we write a single value but never close |
| 309 | GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; |
Nate Kibler | 2d32771 | 2015-09-25 11:56:05 -0700 | [diff] [blame] | 310 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 311 | __block BOOL receivedResponse = NO; |
Nate Kibler | 2d32771 | 2015-09-25 11:56:05 -0700 | [diff] [blame] | 312 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 313 | id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:@21782 |
| 314 | requestedResponseSize:@31415]; |
Nate Kibler | 2d32771 | 2015-09-25 11:56:05 -0700 | [diff] [blame] | 315 | |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 316 | [requestsBuffer writeValue:request]; |
Nate Kibler | 2d32771 | 2015-09-25 11:56:05 -0700 | [diff] [blame] | 317 | |
Yuchen Zeng | dbe2b9e | 2016-06-15 20:23:04 -0700 | [diff] [blame] | 318 | __block GRPCProtoCall *call = |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 319 | [_service RPCToFullDuplexCallWithRequestsWriter:requestsBuffer |
| 320 | eventHandler:^(BOOL done, |
| 321 | RMTStreamingOutputCallResponse *response, |
| 322 | NSError *error) { |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 323 | if (receivedResponse) { |
| 324 | XCTAssert(done, @"Unexpected extra response %@", response); |
| 325 | XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED); |
| 326 | [expectation fulfill]; |
| 327 | } else { |
| 328 | XCTAssertNil(error, @"Finished with unexpected error: %@", error); |
| 329 | XCTAssertFalse(done, @"Finished without response"); |
| 330 | XCTAssertNotNil(response); |
| 331 | receivedResponse = YES; |
| 332 | [call cancel]; |
| 333 | } |
| 334 | }]; |
| 335 | [call start]; |
Jorge Canizales | 5580e5d | 2015-07-15 23:35:48 -0700 | [diff] [blame] | 336 | [self waitForExpectationsWithTimeout:8 handler:nil]; |
Jorge Canizales | 7307684 | 2015-05-30 12:04:43 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @end |