Java Code Examples for org.eclipse.microprofile.context.ThreadContext#withContextCapture()

The following examples show how to use org.eclipse.microprofile.context.ThreadContext#withContextCapture() . 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: ThreadContextTest.java    From microprofile-context-propagation with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that dependent stages created via withContextCapture can be completed independently
 * of the original stage.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 */
@Test
public void withContextCaptureDependentStageForcedCompletion() throws ExecutionException, InterruptedException {
    ThreadContext contextPropagator = ThreadContext.builder()
            .propagated()
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();

    CompletableFuture<String> stage1 = new CompletableFuture<String>();
    CompletableFuture<String> stage2 = contextPropagator.withContextCapture(stage1);

    Assert.assertTrue(stage2.complete("stage_2_done"),
            "It should be possible to complete a CompletableFuture created via withContextCapture without completing the original stage.");

    Assert.assertFalse(stage1.isDone(),
            "Completion of the dependent stage must not imply completion of the original stage.");

    Assert.assertTrue(stage1.complete("stage_1_done"),
            "It should be possible to complete the original stage with a different result after dependent stage was forcibly completed.");

    Assert.assertEquals(stage1.get(), "stage_1_done",
            "Completion stage result does not match the result with which it was forcibly completed.");

    Assert.assertEquals(stage2.get(), "stage_2_done",
            "Completion stage result does not match the result with which it was forcibly completed.");
}
 
Example 2
Source File: ThreadContextTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * The withContextCapture method should be able to create multiple dependent stages
 * having a single parent stage, where each of the dependent stages propagates a
 * different set of thread context.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 * @throws TimeoutException indicates test failure
 */
@Test
public void withContextCaptureMultipleThreadContexts()
        throws ExecutionException, InterruptedException, TimeoutException {
    ThreadContext bufferContext = ThreadContext.builder()
            .propagated(Buffer.CONTEXT_NAME)
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();
    ThreadContext labelContext = ThreadContext.builder()
            .propagated(Label.CONTEXT_NAME)
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();

    try {
        Function<Character, String> getContext = c -> Buffer.get().append(c).append(Label.get()).toString();

        // Set non-default values
        Buffer.set(new StringBuffer("withContextCaptureMultipleThreadContexts-test-buffer-A"));
        Label.set("withContextCaptureMultipleThreadContexts-test-label-A");

        CompletableFuture<Character> unmanagedStage1 = new CompletableFuture<Character>();

        CompletableFuture<Character> stage2 = bufferContext.withContextCapture(unmanagedStage1);
        CompletableFuture<Character> stage3 = labelContext.withContextCapture(unmanagedStage1);

        CompletableFuture<String> stage4 = stage2.thenApply(getContext);
        CompletableFuture<String> stage5 = stage3.thenApply(getContext);

        // change context
        Buffer.set(new StringBuffer("withContextCaptureMultipleThreadContexts-test-buffer-B"));
        Label.set("withContextCaptureMultipleThreadContexts-test-label-B");

        unmanagedStage1.complete(';');

        Assert.assertEquals(stage4.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "withContextCaptureMultipleThreadContexts-test-buffer-A;",
                "Context was incorrectly established on contextual function.");

        Assert.assertEquals(stage5.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS), ";withContextCaptureMultipleThreadContexts-test-label-A",
                "Context was incorrectly established on contextual function.");
    }
    finally {
        // Restore original values
        Buffer.set(null);
        Label.set(null);
    }
}
 
Example 3
Source File: ThreadContextTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * The withContextCapture method should be able to create a dependent stage that is
 * associated with a thread context even if its parent stage is already associated with
 * a different thread context. Both stages must honor their respective thread context.
 *
 * @throws ExecutionException indicates test failure
 * @throws InterruptedException indicates test failure
 * @throws TimeoutException indicates test failure
 */
@Test
public void withContextCaptureSwitchThreadContext()
        throws ExecutionException, InterruptedException, TimeoutException {
    ThreadContext bufferContext = ThreadContext.builder()
            .propagated(Buffer.CONTEXT_NAME)
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();
    ThreadContext labelContext = ThreadContext.builder()
            .propagated(Label.CONTEXT_NAME)
            .unchanged()
            .cleared(ThreadContext.ALL_REMAINING)
            .build();

    try {
        // Set non-default values
        Buffer.set(new StringBuffer("withContextCaptureSwitchThreadContext-test-buffer-A"));
        Label.set("withContextCaptureSwitchThreadContext-test-label-A");

        CompletableFuture<Character> unmanagedStage1 = new CompletableFuture<Character>();

        CompletableFuture<Character> stage2 = bufferContext.withContextCapture(unmanagedStage1);
        CompletableFuture<String> stage3 = stage2.thenApply(c -> Buffer.get().append(c).append(Label.get()).toString());

        // change context
        Buffer.set(new StringBuffer("withContextCaptureSwitchThreadContext-test-buffer-B"));
        Label.set("withContextCaptureSwitchThreadContext-test-label-B");

        CompletableFuture<String> stage4 = labelContext.withContextCapture(stage3);
        CompletableFuture<String> stage5 = stage4.thenApply(s -> s + ';' + Buffer.get().toString() + ';' + Label.get());

        // change context again
        Buffer.set(new StringBuffer("withContextCaptureSwitchThreadContext-test-buffer-C"));
        Label.set("withContextCaptureSwitchThreadContext-test-label-C");

        unmanagedStage1.complete(';');

        Assert.assertEquals(stage5.get(MAX_WAIT_NS, TimeUnit.NANOSECONDS),
                "withContextCaptureSwitchThreadContext-test-buffer-A;;;withContextCaptureSwitchThreadContext-test-label-B",
                "Context was incorrectly established on contextual function.");
    }
    finally {
        // Restore original values
        Buffer.set(null);
        Label.set(null);
    }
}