use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.
the class DelayedStreamTest method listener_noQueued.
@Test
public void listener_noQueued() {
final Metadata headers = new Metadata();
final InputStream message = mock(InputStream.class);
final SingleMessageProducer producer = new SingleMessageProducer(message);
final Metadata trailers = new Metadata();
final Status status = Status.UNKNOWN.withDescription("unique status");
stream.start(listener);
callMeMaybe(stream.setStream(realStream));
verify(realStream).start(listenerCaptor.capture());
ClientStreamListener delayedListener = listenerCaptor.getValue();
delayedListener.onReady();
verify(listener).onReady();
delayedListener.headersRead(headers);
verify(listener).headersRead(headers);
delayedListener.messagesAvailable(producer);
verify(listener).messagesAvailable(producer);
delayedListener.closed(status, RpcProgress.PROCESSED, trailers);
verify(listener).closed(status, RpcProgress.PROCESSED, trailers);
}
use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.
the class ClientCallImplTest method cancelInOnMessageShouldInvokeStreamCancel.
@Test
public void cancelInOnMessageShouldInvokeStreamCancel() throws Exception {
final ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), baseCallOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector);
final Exception cause = new Exception();
ClientCall.Listener<Void> callListener = new ClientCall.Listener<Void>() {
@Override
public void onMessage(Void message) {
call.cancel("foo", cause);
}
};
call.start(callListener, new Metadata());
call.halfClose();
call.request(1);
verify(stream).start(listenerArgumentCaptor.capture());
ClientStreamListener streamListener = listenerArgumentCaptor.getValue();
streamListener.onReady();
streamListener.headersRead(new Metadata());
streamListener.messagesAvailable(new SingleMessageProducer(new ByteArrayInputStream(new byte[0])));
verify(stream).cancel(statusCaptor.capture());
Status status = statusCaptor.getValue();
assertEquals(Status.CANCELLED.getCode(), status.getCode());
assertEquals("foo", status.getDescription());
assertSame(cause, status.getCause());
}
use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.
the class ServerCallImplTest method streamListener_unexpectedRuntimeException.
@Test
public void streamListener_unexpectedRuntimeException() {
ServerStreamListenerImpl<Long> streamListener = new ServerCallImpl.ServerStreamListenerImpl<>(call, callListener, context);
doThrow(new RuntimeException("unexpected exception")).when(callListener).onMessage(any(Long.class));
InputStream inputStream = UNARY_METHOD.streamRequest(1234L);
thrown.expect(RuntimeException.class);
thrown.expectMessage("unexpected exception");
streamListener.messagesAvailable(new SingleMessageProducer(inputStream));
}
use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.
the class ServerCallImplTest method streamListener_messageRead.
@Test
public void streamListener_messageRead() {
ServerStreamListenerImpl<Long> streamListener = new ServerCallImpl.ServerStreamListenerImpl<>(call, callListener, context);
streamListener.messagesAvailable(new SingleMessageProducer(UNARY_METHOD.streamRequest(1234L)));
verify(callListener).onMessage(1234L);
}
use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.
the class ServerImplTest method testCallContextIsBoundInListenerCallbacks.
@Test
public void testCallContextIsBoundInListenerCallbacks() throws Exception {
createAndStartServer();
final AtomicBoolean onReadyCalled = new AtomicBoolean(false);
final AtomicBoolean onMessageCalled = new AtomicBoolean(false);
final AtomicBoolean onHalfCloseCalled = new AtomicBoolean(false);
final AtomicBoolean onCancelCalled = new AtomicBoolean(false);
mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", METHOD)).addMethod(METHOD, new ServerCallHandler<String, Integer>() {
@Override
public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
// Check that the current context is a descendant of SERVER_CONTEXT
final Context initial = Context.current();
assertEquals("yes", SERVER_ONLY.get(initial));
assertNotSame(SERVER_CONTEXT, initial);
assertFalse(initial.isCancelled());
return new ServerCall.Listener<String>() {
@Override
public void onReady() {
checkContext();
onReadyCalled.set(true);
}
@Override
public void onMessage(String message) {
checkContext();
onMessageCalled.set(true);
}
@Override
public void onHalfClose() {
checkContext();
onHalfCloseCalled.set(true);
}
@Override
public void onCancel() {
checkContext();
onCancelCalled.set(true);
}
@Override
public void onComplete() {
checkContext();
}
private void checkContext() {
// Check that the bound context is the same as the initial one.
assertSame(initial, Context.current());
}
};
}
}).build());
ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
transportListener.transportReady(Attributes.EMPTY);
Metadata requestHeaders = new Metadata();
StatsTraceContext statsTraceCtx = StatsTraceContext.newServerContext(streamTracerFactories, "Waitier/serve", requestHeaders);
when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
verify(stream).setListener(streamListenerCaptor.capture());
ServerStreamListener streamListener = streamListenerCaptor.getValue();
assertNotNull(streamListener);
streamListener.onReady();
assertEquals(1, executor.runDueTasks());
assertTrue(onReadyCalled.get());
streamListener.messagesAvailable(new SingleMessageProducer(new ByteArrayInputStream(new byte[0])));
assertEquals(1, executor.runDueTasks());
assertTrue(onMessageCalled.get());
streamListener.halfClosed();
assertEquals(1, executor.runDueTasks());
assertTrue(onHalfCloseCalled.get());
streamListener.closed(Status.CANCELLED);
assertEquals(1, executor.numPendingTasks(CONTEXT_CLOSER_TASK_FITLER));
assertEquals(2, executor.runDueTasks());
assertTrue(onCancelCalled.get());
// Close should never be called if asserts in listener pass.
verify(stream, times(0)).close(isA(Status.class), ArgumentMatchers.<Metadata>isNotNull());
}
Aggregations