use of com.hry.spring.grpc.stream.RouteNote in project spring_boot by hryou0922.
the class RouteGuideServerTest method routeChat.
@Test
public void routeChat() {
Point p1 = Point.newBuilder().setLongitude(1).setLatitude(1).build();
Point p2 = Point.newBuilder().setLongitude(2).setLatitude(2).build();
RouteNote n1 = RouteNote.newBuilder().setLocation(p1).setMessage("m1").build();
RouteNote n2 = RouteNote.newBuilder().setLocation(p2).setMessage("m2").build();
RouteNote n3 = RouteNote.newBuilder().setLocation(p1).setMessage("m3").build();
RouteNote n4 = RouteNote.newBuilder().setLocation(p2).setMessage("m4").build();
RouteNote n5 = RouteNote.newBuilder().setLocation(p1).setMessage("m5").build();
RouteNote n6 = RouteNote.newBuilder().setLocation(p1).setMessage("m6").build();
int timesOnNext = 0;
@SuppressWarnings("unchecked") StreamObserver<RouteNote> responseObserver = (StreamObserver<RouteNote>) mock(StreamObserver.class);
RouteGuideGrpc.RouteGuideStub stub = RouteGuideGrpc.newStub(inProcessChannel);
StreamObserver<RouteNote> requestObserver = stub.routeChat(responseObserver);
verify(responseObserver, never()).onNext(any(RouteNote.class));
requestObserver.onNext(n1);
verify(responseObserver, never()).onNext(any(RouteNote.class));
requestObserver.onNext(n2);
verify(responseObserver, never()).onNext(any(RouteNote.class));
requestObserver.onNext(n3);
ArgumentCaptor<RouteNote> routeNoteCaptor = ArgumentCaptor.forClass(RouteNote.class);
verify(responseObserver, timeout(100).times(++timesOnNext)).onNext(routeNoteCaptor.capture());
RouteNote result = routeNoteCaptor.getValue();
assertEquals(p1, result.getLocation());
assertEquals("m1", result.getMessage());
requestObserver.onNext(n4);
routeNoteCaptor = ArgumentCaptor.forClass(RouteNote.class);
verify(responseObserver, timeout(100).times(++timesOnNext)).onNext(routeNoteCaptor.capture());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 1);
assertEquals(p2, result.getLocation());
assertEquals("m2", result.getMessage());
requestObserver.onNext(n5);
routeNoteCaptor = ArgumentCaptor.forClass(RouteNote.class);
timesOnNext += 2;
verify(responseObserver, timeout(100).times(timesOnNext)).onNext(routeNoteCaptor.capture());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 2);
assertEquals(p1, result.getLocation());
assertEquals("m1", result.getMessage());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 1);
assertEquals(p1, result.getLocation());
assertEquals("m3", result.getMessage());
requestObserver.onNext(n6);
routeNoteCaptor = ArgumentCaptor.forClass(RouteNote.class);
timesOnNext += 3;
verify(responseObserver, timeout(100).times(timesOnNext)).onNext(routeNoteCaptor.capture());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 3);
assertEquals(p1, result.getLocation());
assertEquals("m1", result.getMessage());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 2);
assertEquals(p1, result.getLocation());
assertEquals("m3", result.getMessage());
result = routeNoteCaptor.getAllValues().get(timesOnNext - 1);
assertEquals(p1, result.getLocation());
assertEquals("m5", result.getMessage());
requestObserver.onCompleted();
verify(responseObserver, timeout(100)).onCompleted();
verify(responseObserver, never()).onError(any(Throwable.class));
}
use of com.hry.spring.grpc.stream.RouteNote in project spring_boot by hryou0922.
the class RouteGuideClientTest method routeChat_errorResponse.
/**
* Example for testing bi-directional call.
*/
@Test
public void routeChat_errorResponse() throws Exception {
final List<RouteNote> notesDelivered = new ArrayList<RouteNote>();
final StatusRuntimeException fakeError = new StatusRuntimeException(Status.PERMISSION_DENIED);
// implement the fake service
RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {
@Override
public void onNext(RouteNote value) {
notesDelivered.add(value);
responseObserver.onError(fakeError);
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
return requestObserver;
}
};
serviceRegistry.addService(routeChatImpl);
client.routeChat().await(1, TimeUnit.SECONDS);
assertEquals("First message", notesDelivered.get(0).getMessage());
verify(testHelper, never()).onMessage(any(Message.class));
ArgumentCaptor<Throwable> errorCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(testHelper).onRpcError(errorCaptor.capture());
assertEquals(fakeError.getStatus(), Status.fromThrowable(errorCaptor.getValue()));
}
use of com.hry.spring.grpc.stream.RouteNote in project spring_boot by hryou0922.
the class RouteGuideClientTest method routeChat_simpleResponse.
/**
* Example for testing bi-directional call.
*/
@Test
public void routeChat_simpleResponse() throws Exception {
RouteNote fakeResponse1 = RouteNote.newBuilder().setMessage("dummy msg1").build();
RouteNote fakeResponse2 = RouteNote.newBuilder().setMessage("dummy msg2").build();
final List<String> messagesDelivered = new ArrayList<String>();
final List<Point> locationsDelivered = new ArrayList<Point>();
final AtomicReference<StreamObserver<RouteNote>> responseObserverRef = new AtomicReference<StreamObserver<RouteNote>>();
final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
// implement the fake service
RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<RouteNote> routeChat(StreamObserver<RouteNote> responseObserver) {
responseObserverRef.set(responseObserver);
StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {
@Override
public void onNext(RouteNote value) {
messagesDelivered.add(value.getMessage());
locationsDelivered.add(value.getLocation());
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
allRequestsDelivered.countDown();
}
};
return requestObserver;
}
};
serviceRegistry.addService(routeChatImpl);
// start routeChat
CountDownLatch latch = client.routeChat();
// request message sent and delivered for four times
assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
assertEquals(Arrays.asList("First message", "Second message", "Third message", "Fourth message"), messagesDelivered);
assertEquals(Arrays.asList(Point.newBuilder().setLatitude(0).setLongitude(0).build(), Point.newBuilder().setLatitude(0).setLongitude(1).build(), Point.newBuilder().setLatitude(1).setLongitude(0).build(), Point.newBuilder().setLatitude(1).setLongitude(1).build()), locationsDelivered);
// Let the server send out two simple2 response messages
// and verify that the client receives them.
// Allow some timeout for verify() if not using directExecutor
responseObserverRef.get().onNext(fakeResponse1);
verify(testHelper).onMessage(fakeResponse1);
responseObserverRef.get().onNext(fakeResponse2);
verify(testHelper).onMessage(fakeResponse2);
// let server complete.
responseObserverRef.get().onCompleted();
assertTrue(latch.await(1, TimeUnit.SECONDS));
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
use of com.hry.spring.grpc.stream.RouteNote in project spring_boot by hryou0922.
the class RouteGuideClientTest method routeChat_echoResponse.
/**
* Example for testing bi-directional call.
*/
@Test
public void routeChat_echoResponse() throws Exception {
final List<RouteNote> notesDelivered = new ArrayList<RouteNote>();
// implement the fake service
RouteGuideImplBase routeChatImpl = new RouteGuideImplBase() {
@Override
public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
StreamObserver<RouteNote> requestObserver = new StreamObserver<RouteNote>() {
@Override
public void onNext(RouteNote value) {
notesDelivered.add(value);
responseObserver.onNext(value);
}
@Override
public void onError(Throwable t) {
responseObserver.onError(t);
}
@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
return requestObserver;
}
};
serviceRegistry.addService(routeChatImpl);
client.routeChat().await(1, TimeUnit.SECONDS);
String[] messages = { "First message", "Second message", "Third message", "Fourth message" };
for (int i = 0; i < 4; i++) {
verify(testHelper).onMessage(notesDelivered.get(i));
assertEquals(messages[i], notesDelivered.get(i).getMessage());
}
verify(testHelper, never()).onRpcError(any(Throwable.class));
}
use of com.hry.spring.grpc.stream.RouteNote in project spring_boot by hryou0922.
the class RouteGuideClient method routeChat.
/**
* Bi-directional example, which can only be asynchronous. Send some chat messages, and print any
* chat messages that are sent from the server.
*/
public CountDownLatch routeChat() {
info("*** RouteChat");
final CountDownLatch finishLatch = new CountDownLatch(1);
StreamObserver<RouteNote> requestObserver = asyncStub.routeChat(new StreamObserver<RouteNote>() {
@Override
public void onNext(RouteNote note) {
info("Got message \"{0}\" at {1}, {2}", note.getMessage(), note.getLocation().getLatitude(), note.getLocation().getLongitude());
if (testHelper != null) {
testHelper.onMessage(note);
}
}
@Override
public void onError(Throwable t) {
warning("RouteChat Failed: {0}", Status.fromThrowable(t));
if (testHelper != null) {
testHelper.onRpcError(t);
}
finishLatch.countDown();
}
@Override
public void onCompleted() {
info("Finished RouteChat");
finishLatch.countDown();
}
});
try {
RouteNote[] requests = { newNote("First message", 0, 0), newNote("Second message", 0, 1), newNote("Third message", 1, 0), newNote("Fourth message", 1, 1) };
for (RouteNote request : requests) {
info("Sending message \"{0}\" at {1}, {2}", request.getMessage(), request.getLocation().getLatitude(), request.getLocation().getLongitude());
requestObserver.onNext(request);
}
} catch (RuntimeException e) {
// Cancel RPC
requestObserver.onError(e);
throw e;
}
// Mark the end of requests
requestObserver.onCompleted();
// return the latch while receiving happens asynchronously
return finishLatch;
}
Aggregations