public void halfDuplexCallShouldSucceed() throws Exception { // Build the request. Integer[] responseSizes = {50, 100, 150, 200}; final StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.responseParameters = new ResponseParameters[responseSizes.length]; request.responseType = Messages.COMPRESSABLE; for (int i = 0; i < responseSizes.length; ++i) { request.responseParameters[i] = new ResponseParameters(); request.responseParameters[i].size = responseSizes[i]; request.responseParameters[i].intervalUs = 0; } StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<StreamingOutputCallRequest> requestStream = asyncStub.halfDuplexCall(recorder); final int numRequests = 10; for (int ix = numRequests; ix > 0; --ix) { requestStream.onNext(request); } requestStream.onCompleted(); assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertSuccess(recorder); assertEquals(responseSizes.length * numRequests, recorder.getValues().size()); for (int ix = 0; ix < recorder.getValues().size(); ++ix) { StreamingOutputCallResponse response = recorder.getValues().get(ix); assertEquals(Messages.COMPRESSABLE, response.payload.type); int length = response.payload.body.length; int expectedSize = responseSizes[ix % responseSizes.length]; assertEquals("comparison failed at index " + ix, expectedSize, length); } }
public void serverStreaming() throws Exception { final Messages.StreamingOutputCallRequest request = new Messages.StreamingOutputCallRequest(); request.responseType = Messages.COMPRESSABLE; request.responseParameters = new Messages.ResponseParameters[4]; for (int i = 0; i < 4; i++) { request.responseParameters[i] = new Messages.ResponseParameters(); } request.responseParameters[0].size = 31415; request.responseParameters[1].size = 9; request.responseParameters[2].size = 2653; request.responseParameters[3].size = 58979; final Messages.StreamingOutputCallResponse[] goldenResponses = new Messages.StreamingOutputCallResponse[4]; for (int i = 0; i < 4; i++) { goldenResponses[i] = new Messages.StreamingOutputCallResponse(); goldenResponses[i].payload = new Payload(); goldenResponses[i].payload.type = Messages.COMPRESSABLE; } goldenResponses[0].payload.body = new byte[31415]; goldenResponses[1].payload.body = new byte[9]; goldenResponses[2].payload.body = new byte[2653]; goldenResponses[3].payload.body = new byte[58979]; StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create(); asyncStub.streamingOutputCall(request, recorder); assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertSuccess(recorder); assertMessageEquals(Arrays.asList(goldenResponses), recorder.getValues()); }
public void cancelAfterBegin() throws Exception { StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create(); StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver); requestObserver.onError(new RuntimeException()); assertTrue(responseObserver.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertEquals(Arrays.<StreamingInputCallResponse>asList(), responseObserver.getValues()); assertCodeEquals( io.grpc.Status.CANCELLED, io.grpc.Status.fromThrowable(responseObserver.getError())); }
/** Start a fullDuplexCall which the server will not respond, and verify the deadline expires. */ public void timeoutOnSleepingServer() throws Exception { TestServiceGrpc.TestService stub = TestServiceGrpc.newStub(channel).withDeadlineAfter(1, TimeUnit.MILLISECONDS); StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<StreamingOutputCallRequest> requestObserver = stub.fullDuplexCall(recorder); try { StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.payload = new Messages.Payload(); request.payload.body = new byte[27182]; requestObserver.onNext(request); } catch (IllegalStateException expected) { // This can happen if the stream has already been terminated due to deadline exceeded. } assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertCodeEquals( io.grpc.Status.DEADLINE_EXCEEDED, io.grpc.Status.fromThrowable(recorder.getError())); }
public void deadlineExceededServerStreaming() throws Exception { // warm up the channel and JVM blockingStub.emptyCall(new EmptyProtos.Empty()); ResponseParameters responseParameters = new ResponseParameters(); responseParameters.size = 1; responseParameters.intervalUs = 10000; StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.responseType = Messages.COMPRESSABLE; request.responseParameters = new ResponseParameters[4]; request.responseParameters[0] = responseParameters; request.responseParameters[1] = responseParameters; request.responseParameters[2] = responseParameters; request.responseParameters[3] = responseParameters; StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create(); TestServiceGrpc.newStub(channel) .withDeadlineAfter(30, TimeUnit.MILLISECONDS) .streamingOutputCall(request, recorder); assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertCodeEquals( io.grpc.Status.DEADLINE_EXCEEDED, io.grpc.Status.fromThrowable(recorder.getError())); }