Skip to content

Commit

Permalink
Additional tests to show context propagation.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas committed May 6, 2024
1 parent aa7c8e1 commit a2187a4
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package io.helidon.microprofile.cdi;

import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.se.SeContainerInitializer;
Expand Down Expand Up @@ -79,6 +82,16 @@ Thread onMyExecutor() {
return Thread.currentThread();
}

@OnNewThread
Optional<String> verifyContextVirtual() {
return Contexts.context().flatMap(context -> context.get("hello", String.class));
}

@OnNewThread(ThreadType.PLATFORM)
Optional<String> verifyContextPlatform() {
return Contexts.context().flatMap(context -> context.get("hello", String.class));
}

@Produces
@Named("my-executor")
ExecutorService myExecutor() {
Expand Down Expand Up @@ -120,4 +133,20 @@ void onMyExecutor() {
assertThat(thread.isVirtual(), is(false));
assertThat(thread.getName().startsWith("pool"), is(true));
}

@Test
void verifyContextVirtual() {
Context context = Contexts.globalContext();
context.register("hello", "world");
Optional<String> optional = Contexts.runInContext(context, bean::verifyContextVirtual);
assertThat(optional.orElseThrow(), is("world"));
}

@Test
void verifyContextPlatform() {
Context context = Contexts.globalContext();
context.register("hello", "world");
Optional<String> optional = Contexts.runInContext(context, bean::verifyContextPlatform);
assertThat(optional.orElseThrow(), is("world"));
}
}

0 comments on commit a2187a4

Please sign in to comment.