diff options
author | Muxi Yan <mxyan@google.com> | 2017-04-06 11:52:55 -0700 |
---|---|---|
committer | Muxi Yan <mxyan@google.com> | 2017-04-06 11:52:55 -0700 |
commit | e1ed7712102363d5e6fdad31b1016119f95f0631 (patch) | |
tree | d2a16ca427fc45597d8ddbc1ecdb7e28a8f69c79 /src/objective-c | |
parent | 56369ea4a2c785747d656943f469b461085c3d8d (diff) |
Add test
Diffstat (limited to 'src/objective-c')
-rw-r--r-- | src/objective-c/tests/InteropTests.m | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m index 9105356869..4466d6810c 100644 --- a/src/objective-c/tests/InteropTests.m +++ b/src/objective-c/tests/InteropTests.m @@ -457,4 +457,99 @@ [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; } +- (void)testAlternateDispatchQueue { + XCTAssertNotNil(self.class.host); + __weak XCTestExpectation *expectation1 = [self expectationWithDescription:@"AlternateDispatchQueue1"]; + + NSArray *requests = @[@27182, @8, @1828, @45904]; + NSArray *responses = @[@31415, @9, @2653, @58979]; + + // Set the default dispatch queue + NSString *queue1_label = @"test.queue1"; + NSString *queue2_label = @"test.queue2"; + dispatch_queue_t queue1 = dispatch_queue_create([queue1_label UTF8String], DISPATCH_QUEUE_SERIAL); + dispatch_queue_t queue2 = dispatch_queue_create([queue2_label UTF8String], DISPATCH_QUEUE_SERIAL); + [_service setDefaultResponseDispatchQueue:queue1]; + GRXBufferedPipe *requestsBuffer1 = [[GRXBufferedPipe alloc] init]; + + __block int index = 0; + + id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [requestsBuffer1 writeValue:request]; + + [_service fullDuplexCallWithRequestsWriter:requestsBuffer1 + eventHandler:^(BOOL done, + RMTStreamingOutputCallResponse *response, + NSError *error) { + XCTAssertNil(error, @"Finished with unexpected error: %@", error); + XCTAssertTrue(done || response, @"Event handler called without an event."); + NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)]; + XCTAssert([label isEqualToString:queue1_label]); + + if (response) { + XCTAssertLessThan(index, 4, @"More than 4 responses received."); + id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]]; + XCTAssertEqualObjects(response, expected); + index += 1; + if (index < 4) { + id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [requestsBuffer1 writeValue:request]; + } else { + [requestsBuffer1 writesFinishedWithError:nil]; + } + } + + if (done) { + XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index); + [expectation1 fulfill]; + } + }]; + + [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; + + // Test overriding default queue with another queue + __weak XCTestExpectation *expectation2 = [self expectationWithDescription:@"AlternateDispatchQueue2"]; + GRXBufferedPipe *requestsBuffer2 = [[GRXBufferedPipe alloc] init]; + + index = 0; + + [requestsBuffer2 writeValue:request]; + + GRPCProtoCall *rpc = [_service RPCToFullDuplexCallWithRequestsWriter:requestsBuffer2 + eventHandler:^(BOOL done, + RMTStreamingOutputCallResponse *response, + NSError *error) { + XCTAssertNil(error, @"Finished with unexpected error: %@", error); + XCTAssertTrue(done || response, @"Event handler called without an event."); + NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)]; + XCTAssert([label isEqualToString:queue2_label]); + + if (response) { + XCTAssertLessThan(index, 4, @"More than 4 responses received."); + id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]]; + XCTAssertEqualObjects(response, expected); + index += 1; + if (index < 4) { + id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [requestsBuffer2 writeValue:request]; + } else { + [requestsBuffer2 writesFinishedWithError:nil]; + } + } + + if (done) { + XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index); + [expectation2 fulfill]; + } + }]; + [rpc setResponseDispatchQueue:queue2]; + [rpc start]; + + [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; + [_service setDefaultResponseDispatchQueue:dispatch_get_main_queue()]; +} + @end |