use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method basicStream.
@Test
@SuppressWarnings("deprecation")
public void basicStream() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
serverTransport = serverTransportListener.transport;
Metadata clientHeaders = new Metadata();
clientHeaders.put(asciiKey, "client");
clientHeaders.put(asciiKey, "dupvalue");
clientHeaders.put(asciiKey, "dupvalue");
clientHeaders.put(binaryKey, "äbinaryclient");
Metadata clientHeadersCopy = new Metadata();
clientHeadersCopy.merge(clientHeaders);
ClientStream clientStream = client.newStream(methodDescriptor, clientHeaders);
clientStream.start(mockClientStreamListener);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
assertEquals(methodDescriptor.getFullMethodName(), serverStreamCreation.method);
assertEquals(Lists.newArrayList(clientHeadersCopy.getAll(asciiKey)), Lists.newArrayList(serverStreamCreation.headers.getAll(asciiKey)));
assertEquals(Lists.newArrayList(clientHeadersCopy.getAll(binaryKey)), Lists.newArrayList(serverStreamCreation.headers.getAll(binaryKey)));
ServerStream serverStream = serverStreamCreation.stream;
ServerStreamListener mockServerStreamListener = serverStreamCreation.listener;
assertEquals("additional attribute value", serverStream.getAttributes().get(ADDITIONAL_TRANSPORT_ATTR_KEY));
assertNotNull(serverStream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR));
serverStream.request(1);
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).onReady();
assertTrue(clientStream.isReady());
clientStream.writeMessage(methodDescriptor.streamRequest("Hello!"));
clientStream.flush();
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).messageRead(inputStreamCaptor.capture());
assertEquals("Hello!", methodDescriptor.parseRequest(inputStreamCaptor.getValue()));
inputStreamCaptor.getValue().close();
clientStream.halfClose();
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).halfClosed();
Metadata serverHeaders = new Metadata();
serverHeaders.put(asciiKey, "server");
serverHeaders.put(asciiKey, "dupvalue");
serverHeaders.put(asciiKey, "dupvalue");
serverHeaders.put(binaryKey, "äbinaryserver");
Metadata serverHeadersCopy = new Metadata();
serverHeadersCopy.merge(serverHeaders);
serverStream.writeHeaders(serverHeaders);
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).headersRead(metadataCaptor.capture());
assertEquals(Lists.newArrayList(serverHeadersCopy.getAll(asciiKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(asciiKey)));
assertEquals(Lists.newArrayList(serverHeadersCopy.getAll(binaryKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(binaryKey)));
clientStream.request(1);
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).onReady();
assertTrue(serverStream.isReady());
serverStream.writeMessage(methodDescriptor.streamResponse("Hi. Who are you?"));
serverStream.flush();
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).messageRead(inputStreamCaptor.capture());
assertEquals("Hi. Who are you?", methodDescriptor.parseResponse(inputStreamCaptor.getValue()));
inputStreamCaptor.getValue().close();
Status status = Status.OK.withDescription("That was normal");
Metadata trailers = new Metadata();
trailers.put(asciiKey, "trailers");
trailers.put(asciiKey, "dupvalue");
trailers.put(asciiKey, "dupvalue");
trailers.put(binaryKey, "äbinarytrailers");
serverStream.close(status, trailers);
verify(mockServerStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture());
assertCodeEquals(Status.OK, statusCaptor.getValue());
verify(mockClientStreamListener, timeout(TIMEOUT_MS)).closed(statusCaptor.capture(), metadataCaptor.capture());
assertEquals(status.getCode(), statusCaptor.getValue().getCode());
assertEquals(status.getDescription(), statusCaptor.getValue().getDescription());
assertEquals(Lists.newArrayList(trailers.getAll(asciiKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(asciiKey)));
assertEquals(Lists.newArrayList(trailers.getAll(binaryKey)), Lists.newArrayList(metadataCaptor.getValue().getAll(binaryKey)));
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method authorityPropagation.
@Test
@SuppressWarnings("deprecation")
public void authorityPropagation() throws Exception {
server.start(serverListener);
client = newClientTransport(server);
runIfNotNull(client.start(mockClientTransportListener));
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
Metadata clientHeaders = new Metadata();
ClientStream clientStream = client.newStream(methodDescriptor, clientHeaders);
clientStream.start(mockClientStreamListener);
StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ServerStream serverStream = serverStreamCreation.stream;
assertEquals(testAuthority(server), serverStream.getAuthority());
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method ping_duringShutdown.
@Test
public void ping_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));
ClientTransport.PingCallback mockPingCallback = mock(ClientTransport.PingCallback.class);
try {
client.ping(mockPingCallback, MoreExecutors.directExecutor());
} catch (UnsupportedOperationException ex) {
// Transport doesn't support ping, so this neither passes nor fails.
assumeTrue(false);
}
verify(mockPingCallback, timeout(TIMEOUT_MS)).onSuccess(Matchers.anyInt());
stream.cancel(Status.CANCELLED);
}
use of io.grpc.internal.ClientStream in project grpc-java by grpc.
the class AbstractTransportTest method transportInUse_normalClose.
@Test
public void transportInUse_normalClose() 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);
MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
StreamCreation serverStreamCreation1 = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
ClientStream stream2 = client.newStream(methodDescriptor, new Metadata());
stream2.start(mockClientStreamListener);
StreamCreation serverStreamCreation2 = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
stream1.halfClose();
serverStreamCreation1.stream.close(Status.OK, new Metadata());
stream2.halfClose();
verify(mockClientTransportListener, never()).transportInUse(false);
serverStreamCreation2.stream.close(Status.OK, new Metadata());
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 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());
}
Aggregations