use of io.grpc.internal.ClientStreamListener in project grpc-java by grpc.
the class AbstractTransportTest method clientCancelFromWithinMessageRead.
@Test
public void clientCancelFromWithinMessageRead() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
final SettableFuture<Boolean> closedCalled = SettableFuture.create();
final ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
clientStream.start(new ClientStreamListener() {
@Override
public void headersRead(Metadata headers) {
}
@Override
public void closed(Status status, Metadata trailers) {
assertEquals(Status.CANCELLED.getCode(), status.getCode());
assertEquals("nevermind", status.getDescription());
closedCalled.set(true);
}
@Override
public void messageRead(InputStream message) {
assertEquals("foo", methodDescriptor.parseResponse(message));
clientStream.cancel(Status.CANCELLED.withDescription("nevermind"));
}
@Override
public void onReady() {
}
});
clientStream.halfClose();
clientStream.request(1);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
assertEquals(methodDescriptor.getFullMethodName(), serverStreamCreation.method);
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).onReady();
assertTrue(serverStream.isReady());
serverStream.writeHeaders(new Metadata());
serverStream.writeMessage(methodDescriptor.streamRequest("foo"));
serverStream.flush();
// Block until closedCalled was set.
closedCalled.get(5, TimeUnit.SECONDS);
serverStream.close(Status.OK, new Metadata());
}
use of io.grpc.internal.ClientStreamListener in project grpc-java by grpc.
the class AbstractTransportTest method doPingPong.
/**
* Helper that simply does an RPC. It can be used similar to a sleep for negative testing: to give
* time for actions _not_ to happen. Since it is based on doing an actual RPC with actual
* callbacks, it generally provides plenty of time for Runnables to execute. But it is also faster
* on faster machines and more reliable on slower machines.
*/
private void doPingPong(MockServerListener serverListener) throws InterruptedException {
ManagedClientTransport client = newClientTransport(server);
runIfNotNull(client.start(mock(ManagedClientTransport.Listener.class)));
ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
ClientStreamListener mockClientStreamListener = mock(ClientStreamListener.class);
clientStream.start(mockClientStreamListener);
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
serverStream.close(Status.OK, new Metadata());
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class), any(Metadata.class));
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(any(Status.class));
client.shutdown();
}
use of io.grpc.internal.ClientStreamListener in project grpc-java by grpc.
the class AbstractTransportTest method newStream_duringShutdown.
@Test
public void newStream_duringShutdown() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
// Stream prevents termination
ClientStream stream = client.newStream(methodDescriptor, new Metadata());
stream.start(mockClientStreamListener);
client.shutdown();
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportShutdown(any(Status.class));
ClientStream stream2 = client.newStream(methodDescriptor, new Metadata());
ClientStreamListener mockClientStreamListener2 = mock(ClientStreamListener.class);
stream2.start(mockClientStreamListener2);
verify(mockClientStreamListener2, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
assertCodeEquals(Status.UNAVAILABLE, statusCaptor.getValue());
// Make sure earlier stream works.
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
// TODO(zdapeng): Increased timeout to 20 seconds to see if flakiness of #2328 persists. Take
// further action after sufficient observation.
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(20 * TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverStreamCreation.stream.close(Status.OK, new Metadata());
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
assertCodeEquals(Status.OK, statusCaptor.getValue());
}
use of io.grpc.internal.ClientStreamListener in project grpc-java by grpc.
the class AbstractTransportTest method frameAfterRstStreamShouldNotBreakClientChannel.
// TODO(ejona):
// multiple streams on same transport
// multiple client transports to same server
// halfClose to trigger flush (client and server)
// flow control pushes back (client and server)
// flow control provides precisely number of messages requested (client and server)
// onReady called when buffer drained (on server and client)
// test no start reentrancy (esp. during failure) (transport and call)
// multiple requests/responses (verifying contents received)
// server transport shutdown triggers client shutdown (via GOAWAY)
// queued message InputStreams are closed on stream cancel
// (and maybe exceptions handled)
/**
* Test for issue https://github.com/grpc/grpc-java/issues/1682
*/
@Test
public void frameAfterRstStreamShouldNotBreakClientChannel() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
// Try to create a sequence of frames so that the client receives a HEADERS or DATA frame
// after having sent a RST_STREAM to the server. Previously, this would have broken the
// Netty channel.
ClientStream stream = client.newStream(methodDescriptor, new Metadata());
stream.start(mockClientStreamListener);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
stream.flush();
stream.writeMessage(methodDescriptor.streamRequest("foo"));
stream.flush();
stream.cancel(Status.CANCELLED);
stream.flush();
serverStreamCreation.stream.writeHeaders(new Metadata());
serverStreamCreation.stream.flush();
serverStreamCreation.stream.writeMessage(methodDescriptor.streamResponse("bar"));
serverStreamCreation.stream.flush();
verify(mockClientStreamListener, timeout(250)).closed(eq(Status.CANCELLED), any(Metadata.class));
ClientStreamListener mockClientStreamListener2 = mock(ClientStreamListener.class);
// Test that the channel is still usable i.e. we can receive headers from the server on a
// new stream.
stream = client.newStream(methodDescriptor, new Metadata());
stream.start(mockClientStreamListener2);
serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverStreamCreation.stream.writeHeaders(new Metadata());
serverStreamCreation.stream.flush();
verify(mockClientStreamListener2, timeout(250)).headersRead(any(Metadata.class));
}
Aggregations