PendingJavaScriptInvocation implements PendingJavaScriptResult { public void complete(JsonValue value) { assert isSubscribed(); successHandler.accept(value); } PendingJavaScriptInvocation(StateNode owner,
            JavaScriptInvocation invocation); StateNode getOwner(); boolean isSubscribed(); void complete(JsonValue value); void completeExceptionally(JsonValue value); @Override boolean cancelExecution(); @Override boolean isSentToBrowser(); @Override void then(SerializableConsumer<JsonValue> successHandler,
            SerializableConsumer<String> errorHandler); boolean isCanceled(); JavaScriptInvocation getInvocation();  }
@Test public void untypedFuture_success() { invocation.toCompletableFuture().whenComplete(jsonFutureHandler); invocation.complete(fooJsonString); assertJsonSuccess(); }
@Test public void typedFuture_success() { invocation.toCompletableFuture(String.class) .whenComplete(stringFutureHandler); invocation.complete(fooJsonString); assertStringSuccess(); }
@Test public void blockFromSessionThreadAfterCompleting_doesNotThrow() throws Exception { MockVaadinSession session = new MockVaadinSession(); session.runWithLock(() -> { CompletableFuture<JsonValue> completableFuture = invocation .toCompletableFuture(); JsonObject value = Json.createObject(); invocation.complete(value); for (Callable<JsonValue> action : createBlockingActions( completableFuture)) { JsonValue actionValue = action.call(); Assert.assertSame(value, actionValue); } return null; }); }
@Test public void blockFromOtherThread_doesNotThrow() throws Exception { MockVaadinSession session = new MockVaadinSession(); VaadinSession.setCurrent(session); ExecutorService executor = Executors.newFixedThreadPool(3); session.lock(); try { CompletableFuture<JsonValue> completableFuture = invocation .toCompletableFuture(); List<Future<JsonValue>> futures = createBlockingActions( completableFuture).stream().map(executor::submit) .collect(Collectors.toList()); Assert.assertEquals("All futures should be pending", 0, futures.stream().filter(Future::isDone).count()); JsonObject value = Json.createObject(); invocation.complete(value); executor.shutdown(); executor.awaitTermination(100, TimeUnit.MILLISECONDS); Assert.assertEquals("All futures should be done", futures.size(), futures.stream().filter(Future::isDone).count()); futures.forEach(future -> { try { JsonValue futureValue = future.get(); Assert.assertSame(value, futureValue); } catch (Exception e) { throw new RuntimeException(e); } }); } finally { session.unlock(); executor.shutdown(); } }