use of io.grpc.internal.ClientStream 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.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method zeroMessageStream.
@Test
public void zeroMessageStream() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
clientStream.start(mockClientStreamListener);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
clientStream.halfClose();
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
serverStream.writeHeaders(new Metadata());
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(any(Metadata.class));
Status status = Status.OK.withDescription("Nice talking to you");
serverStream.close(status, new Metadata());
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture());
assertCodeEquals(Status.OK, statusCaptor.getValue());
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
assertEquals(status.getCode(), statusCaptor.getValue().getCode());
assertEquals(status.getDescription(), statusCaptor.getValue().getDescription());
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method serverCancel.
@Test
public void serverCancel() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
clientStream.start(mockClientStreamListener);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
Status status = Status.DEADLINE_EXCEEDED.withDescription("It was bound to happen").withCause(new Exception());
serverStream.cancel(status);
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(Matchers.same(status));
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), any(Metadata.class));
// Presently we can't sent much back to the client in this case. Verify that is the current
// behavior for consistency between transports.
assertCodeEquals(Status.CANCELLED, statusCaptor.getValue());
// Cause should not be transmitted between server and client
assertNull(statusCaptor.getValue().getCause());
// Second cancellation shouldn't trigger additional callbacks
reset(mockServerStreamListener);
reset(mockClientStreamListener);
serverStream.cancel(status);
doPingPong(serverListener);
verify(mockServerStreamListener, never()).closed(any(Status.class));
verify(mockClientStreamListener, never()).closed(any(Status.class), any(Metadata.class));
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method transportInUse_clientCancel.
@Test
public void transportInUse_clientCancel() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
ClientStream stream1 = client.newStream(methodDescriptor, new Metadata());
stream1.start(mockClientStreamListener);
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
ClientStream stream2 = client.newStream(methodDescriptor, new Metadata());
stream2.start(mockClientStreamListener);
stream1.cancel(Status.CANCELLED);
verify(mockClientTransportListener, never()).transportInUse(false);
stream2.cancel(Status.CANCELLED);
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
// Verify that the callback has been called only once for true and false respectively
verify(mockClientTransportListener).transportInUse(true);
verify(mockClientTransportListener).transportInUse(false);
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method openStreamPreventsTermination.
@Test
public void openStreamPreventsTermination() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
ClientStream clientStream = client.newStream(methodDescriptor, new Metadata());
clientStream.start(mockClientStreamListener);
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(true);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
client.shutdown();
client = null;
server.shutdown();
serverTransport.shutdown();
serverTransport = null;
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportShutdown(any(Status.class));
assertTrue(serverListener.waitForShutdown(TIMEOUT_MS, TimeUnit.MILLISECONDS));
// A new server should be able to start listening, since the current server has given up
// resources. There may be cases this is impossible in the future, but for now it is a useful
// property.
serverListener = new MockServerListener();
server = newServer(server);
server.start(serverListener);
// Try to "flush" out any listener notifications on client and server. This also ensures that
// the stream still functions.
serverStream.writeHeaders(new Metadata());
clientStream.halfClose();
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(any(Metadata.class));
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
verify(mockClientTransportListener, never()).transportTerminated();
verify(mockClientTransportListener, never()).transportInUse(false);
assertFalse(serverTransportListener.isTerminated());
clientStream.cancel(Status.CANCELLED);
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportTerminated();
verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportInUse(false);
assertTrue(serverTransportListener.waitForTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS));
}
Aggregations