Skip to content

Commit

Permalink
Creates FT executor using our ThreadPoolSupplier to ensure context pr…
Browse files Browse the repository at this point in the history
…opagation. Adds new test. (helidon-io#9555)
  • Loading branch information
spericas authored and barchetta committed Dec 5, 2024
1 parent 2e4fd31 commit a2d5ca4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,11 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.configurable.ThreadPoolSupplier;
import io.helidon.config.Config;

import static java.lang.System.Logger.Level.ERROR;
Expand Down Expand Up @@ -54,9 +54,11 @@ public final class FaultTolerance {
private static final AtomicReference<Config> CONFIG = new AtomicReference<>(Config.empty());

static {
EXECUTOR.set(LazyValue.create(() -> Executors.newThreadPerTaskExecutor(Thread.ofVirtual()
.name("helidon-ft-", 0)
.factory())));
EXECUTOR.set(LazyValue.create(() -> ThreadPoolSupplier.builder()
.threadNamePrefix("helidon-ft-")
.virtualThreads(true)
.build()
.get()));
}

private FaultTolerance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.endsWith;
Expand Down Expand Up @@ -61,6 +64,24 @@ void testThreadName() throws Exception {
assertThat(threadName, endsWith(": async"));
}

@Test
void testContextPropagation() throws Exception {
Context context = Context.create();
CompletableFuture<Context> cf = new CompletableFuture<>();
Contexts.runInContext(context, () -> {
try {
Async async = Async.create();
async.invoke(() -> {
cf.complete(Contexts.context().orElse(null));
return null;
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
assertThat(cf.get(WAIT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), is(context));
}

private Thread testAsync(Async async) {
try {
CompletableFuture<Thread> cf = new CompletableFuture<>();
Expand Down

0 comments on commit a2d5ca4

Please sign in to comment.