@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<>(); final List<Point> locationsDelivered = new ArrayList<>(); final AtomicReference<StreamObserver<RouteNote>> responseObserverRef = new AtomicReference<StreamObserver<RouteNote>>(); final CountDownLatch allRequestsDelivered = new CountDownLatch(1); 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); CountDownLatch latch = client.routeChat(); 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); responseObserverRef.get().onNext(fakeResponse1); verify(testHelper).onMessage(fakeResponse1); responseObserverRef.get().onNext(fakeResponse2); verify(testHelper).onMessage(fakeResponse2); responseObserverRef.get().onCompleted(); assertTrue(latch.await(1, TimeUnit.SECONDS)); verify(testHelper, never()).onRpcError(any(Throwable.class)); }
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) { requestObserver.onError(e); throw e; } requestObserver.onCompleted(); return finishLatch; }
RouteGuideClient { 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) { requestObserver.onError(e); throw e; } requestObserver.onCompleted(); return finishLatch; } }
RouteGuideClient { 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) { requestObserver.onError(e); throw e; } requestObserver.onCompleted(); return finishLatch; } RouteGuideClient(String host, int port);  RouteGuideClient(ManagedChannelBuilder<?> channelBuilder); }
RouteGuideClient { 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) { requestObserver.onError(e); throw e; } requestObserver.onCompleted(); return finishLatch; } RouteGuideClient(String host, int port);  RouteGuideClient(ManagedChannelBuilder<?> channelBuilder); void shutdown(); void getFeature(int lat, int lon); void listFeatures(int lowLat, int lowLon, int hiLat, int hiLon); void recordRoute(List<Feature> features, int numPoints); CountDownLatch routeChat(); static void main(String[] args); }
RouteGuideClient { 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) { requestObserver.onError(e); throw e; } requestObserver.onCompleted(); return finishLatch; } RouteGuideClient(String host, int port);  RouteGuideClient(ManagedChannelBuilder<?> channelBuilder); void shutdown(); void getFeature(int lat, int lon); void listFeatures(int lowLat, int lowLon, int hiLat, int hiLon); void recordRoute(List<Feature> features, int numPoints); CountDownLatch routeChat(); static void main(String[] args);  }