Java Code Examples for org.eclipse.microprofile.context.ManagedExecutor#supplyAsync()
The following examples show how to use
org.eclipse.microprofile.context.ManagedExecutor#supplyAsync() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CDIContextTest.java From microprofile-context-propagation with Apache License 2.0 | 5 votes |
private void checkCDIPropagation(boolean expectPropagate, String stateToPropagate, ManagedExecutor me, AbstractBean bean) throws Exception { bean.setState(stateToPropagate); CompletableFuture<String> cf = me.supplyAsync(() -> { String state = bean.getState(); return state; }); assertEquals(cf.get(TIMEOUT_MIN, TimeUnit.MINUTES), expectPropagate ? stateToPropagate : AbstractBean.UNINITIALIZED); }
Example 2
Source File: ManagedExecutorTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * When an already-contextualized Supplier or BiFunction is specified as the action/task, * the action/task runs with its already-captured context rather than * capturing and applying context per the configuration of the managed executor. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test public void contextOfContextualSuppplierAndBiConsumerOverrideContextOfManagedExecutor() throws ExecutionException, InterruptedException, TimeoutException { ThreadContext bufferContext = ThreadContext.builder() .propagated(Buffer.CONTEXT_NAME) .unchanged() .cleared(ThreadContext.ALL_REMAINING) .build(); ManagedExecutor executor = ManagedExecutor.builder() .propagated(Label.CONTEXT_NAME) .cleared(ThreadContext.ALL_REMAINING) .build(); try { Supplier<String> getBuffer = () -> { Assert.assertEquals(Label.get(), "", "Context type not cleared from thread."); return Buffer.get().toString(); }; Buffer.set(new StringBuffer("contextualSupplierOverride-buffer-1")); Label.set("contextualSupplierOverride-label-1"); Supplier<String> precontextualizedSupplier1 = bufferContext.contextualSupplier(getBuffer); Buffer.set(new StringBuffer("contextualSupplierOverride-buffer-2")); Label.set("contextualSupplierOverride-label-2"); Supplier<String> precontextualizedSupplier2 = bufferContext.contextualSupplier(getBuffer); Buffer.set(new StringBuffer("contextualBiConsumerOverride-buffer-3")); Label.set("contextualBiConsumerOverride-label-3"); BiConsumer<String, String> precontextualizedConsumer3 = bufferContext.contextualConsumer((b1, b2) -> { Assert.assertEquals(Buffer.get().toString(), "contextualBiConsumerOverride-buffer-3", "Previously captured context type not found on thread."); Assert.assertEquals(Label.get(), "", "Context type not cleared from thread."); Assert.assertEquals(b1, "contextualSupplierOverride-buffer-1", "Previously captured context type not found on Supplier's thread."); Assert.assertEquals(b2, "contextualSupplierOverride-buffer-2", "Previously captured context type not found on Supplier's thread."); }); Buffer.set(new StringBuffer("contextualBiConsumerOverride-buffer-4")); Label.set("contextualBiConsumerOverride-label-4"); BiConsumer<Void, Throwable> precontextualizedConsumer4 = bufferContext.contextualConsumer((unused, failure) -> { Assert.assertEquals(Buffer.get().toString(), "contextualBiConsumerOverride-buffer-4", "Previously captured context type not found on thread."); Assert.assertEquals(Label.get(), "", "Context type not cleared from thread."); }); Buffer.set(new StringBuffer("contextualSupplierAndBiConsumerOverride-buffer-5")); Label.set("contextualSupplierAndBiConsumerOverride-label-5"); CompletableFuture<String> stage1 = executor.supplyAsync(precontextualizedSupplier1); CompletableFuture<String> stage2 = executor.supplyAsync(precontextualizedSupplier2); CompletableFuture<Void> stage3 = stage1.thenAcceptBoth(stage2, precontextualizedConsumer3); CompletableFuture<Void> stage4 = stage3.whenCompleteAsync(precontextualizedConsumer4); CompletableFuture<Void> stage5 = stage4.whenComplete((unused, failure) -> { Assert.assertEquals(Label.get(), "contextualSupplierAndBiConsumerOverride-label-5", "Context type captured by managed executor not found on thread."); Assert.assertEquals(Buffer.get().toString(), "", "Context type not cleared from thread."); }); stage5.join(); } finally { executor.shutdownNow(); // Restore original values Buffer.set(null); Label.set(null); } }
Example 3
Source File: ManagedExecutorTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that the ManagedExecutor implementation starts 2 async tasks/actions, and no more, * when maxAsync is configured to 2. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test public void maxAsync2() throws ExecutionException, InterruptedException, TimeoutException { ManagedExecutor executor = ManagedExecutor.builder() .maxAsync(2) .propagated() .cleared(ThreadContext.ALL_REMAINING) .build(); Phaser barrier = new Phaser(2); try { // Use up both maxAsync slots on blocking operations and wait for them to start Future<Integer> future1 = executor.submit(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance())); CompletableFuture<Integer> future2 = executor.supplyAsync(() -> barrier.awaitAdvance(barrier.arriveAndAwaitAdvance())); barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS); // This data structure holds the results of tasks which shouldn't be able to run yet LinkedBlockingQueue<String> results = new LinkedBlockingQueue<String>(); // Submit additional tasks/actions for async execution. // These should queue, but otherwise be unable to start yet due to maxAsync=2. CompletableFuture<Void> future3 = executor.runAsync(() -> results.offer("Result3")); CompletableFuture<Boolean> future4 = executor.supplyAsync(() -> results.offer("Result4")); Future<Boolean> future5 = executor.submit(() -> results.offer("Result5")); CompletableFuture<Boolean> future6 = executor.completedFuture("6") .thenApplyAsync(s -> results.offer("Result" + s)); // Detect whether any of the above tasks/actions run within the next 5 seconds Assert.assertNull(results.poll(5, TimeUnit.SECONDS), "Should not be able start more than 2 async tasks when maxAsync is 2."); // unblock and allow tasks to finish barrier.arrive(); barrier.arrive(); // there are 2 parties in each phase Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "None of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 1 of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 2 of the queued tasks ran."); Assert.assertNotNull(results.poll(MAX_WAIT_NS, TimeUnit.SECONDS), "Only 3 of the queued tasks ran."); Assert.assertEquals(future1.get(), Integer.valueOf(2), "Unexpected result of first task."); Assert.assertEquals(future2.get(), Integer.valueOf(2), "Unexpected result of second task."); Assert.assertNull(future3.join(), "Unexpected result of third task."); Assert.assertEquals(future4.join(), Boolean.TRUE, "Unexpected result of fourth task."); Assert.assertEquals(future5.get(), Boolean.TRUE, "Unexpected result of fifth task."); Assert.assertEquals(future6.get(), Boolean.TRUE, "Unexpected result of sixth task."); } finally { barrier.forceTermination(); executor.shutdownNow(); } }
Example 4
Source File: MPConfigTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that the maxAsync and maxQueued attributes of a ManagedExecutor are defaulted * according to the defaults specified by the application in MicroProfile Config. * * @throws ExecutionException indicates test failure * @throws InterruptedException indicates test failure * @throws TimeoutException indicates test failure */ @Test(dependsOnMethods = "beanInjected") public void defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig() throws ExecutionException, InterruptedException, TimeoutException { // Expected config is maxAsync=1, maxQueued=4; propagated=Buffer; cleared=Remaining ManagedExecutor executor = ManagedExecutor.builder().propagated(Buffer.CONTEXT_NAME).build(); Phaser barrier = new Phaser(1); try { // Set non-default values Buffer.set(new StringBuffer("defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-buffer")); Label.set("defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-label"); // First, use up the single maxAsync slot with a blocking task and wait for it to start executor.submit(() -> barrier.awaitAdvanceInterruptibly(barrier.arrive() + 1)); barrier.awaitAdvanceInterruptibly(0, MAX_WAIT_NS, TimeUnit.NANOSECONDS); // Use up queue positions CompletableFuture<String> cf1 = executor.supplyAsync(() -> Buffer.get().toString()); CompletableFuture<String> cf2 = executor.supplyAsync(() -> Label.get()); CompletableFuture<String> cf3 = executor.supplyAsync(() -> "III"); CompletableFuture<String> cf4 = executor.supplyAsync(() -> "IV"); // Fifth attempt to queue a task must be rejected try { CompletableFuture<String> cf5 = executor.supplyAsync(() -> "V"); // CompletableFuture interface does not provide detail on precisely how to report rejection, // so tolerate both possibilities: exception raised or stage returned with exceptional completion. Assert.assertTrue(cf5.isDone() && cf5.isCompletedExceptionally(), "Exceeded maxQueued of 4. Future for 5th queued task/action is " + cf5); } catch (RejectedExecutionException x) { // test passes } // unblock and allow tasks to finish barrier.arrive(); Assert.assertEquals(cf1.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "defaultMaxAsyncAndMaxQueuedForManagedExecutorViaMPConfig-test-buffer", "First task: Context not propagated as configured on the builder."); Assert.assertEquals(cf2.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "", "Second task: Context not cleared as defaulted by MicroProfile Config property."); Assert.assertEquals(cf3.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "III", "Unexpected result of third task."); Assert.assertEquals(cf4.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "IV", "Unexpected result of fourth task."); } finally { barrier.forceTermination(); // Restore original values Buffer.set(null); Label.set(null); } }