Provide custom BackgroundExecutorWrapper to pass thread locals

(cherry picked from commit 336accf447)
This commit is contained in:
Noemi Szemenyei committed 2022-03-11 13:31:50 +01:00
1 parent 75662aac5c
commit 3c7555de36
8 files changed
+307 -50

No files matched your search

@@ -4,17 +4,21 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
import io.ebean.config.MdcBackgroundExecutorWrapper;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultBackgroundExecutorTest {
@Test
public void submit_callable() throws Exception {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 2, "test");
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 2, "test", null);
final Future<String> future0 = es.submit(() -> "Hello");
final Future<String> future1 = es.submit(() -> "There");
@@ -39,7 +43,7 @@ public class DefaultBackgroundExecutorTest {
public void shutdown_slowCallable_expect_interrupted() throws Exception {
int shutdownWaitSecs = 1;
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, shutdownWaitSecs, "test");
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, shutdownWaitSecs, "test", null);
final Future<String> future2 = es.submit(() -> {
try {
@@ -61,7 +65,7 @@ public class DefaultBackgroundExecutorTest {
@Disabled("test takes long time")
public void shutdown_when_running_expect_waitAndNiceShutdown() {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 20, "test");
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 20, "test", null);
es.execute(new RunFor(3000, "a"));
es.execute(new RunFor(3000, "b"));
@@ -74,7 +78,7 @@ public class DefaultBackgroundExecutorTest {
@Disabled("test takes long time")
public void shutdown_when_rougeRunnable_expect_InterruptedException() {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test", null);
es.execute(new RunFor(300000, "a"));
es.execute(new RunFor(3000, "b"));
@@ -85,12 +89,12 @@ public class DefaultBackgroundExecutorTest {
@Test
public void wrapWithNoMDC() {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test", null);
assertThat(MDC.getCopyOfContextMap()).isNull();
es.wrapMDC(() -> {
es.wrap(() -> {
assertThat(MDC.getCopyOfContextMap()).isNull();
});
es.wrapMDC(() -> {
es.wrap(() -> {
assertThat(MDC.getCopyOfContextMap()).isNull();
return "Callable";
});
@@ -98,25 +102,44 @@ public class DefaultBackgroundExecutorTest {
}
@Test
public void wrapWithMDC_expect_() {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test");
public void wrapWithMDC_expect_() throws Exception {
DefaultBackgroundExecutor es = new DefaultBackgroundExecutor(1, 10, "test", new MdcBackgroundExecutorWrapper());
// MDC has a copyOnThread map. So we must pass different values to check if the test will work
MDC.clear();
es.submit(()->{
assertThat(MDC.get("hello")).isNull();
}).get();
MDC.put("hello", "there");
es.wrapMDC(() -> {
es.wrap(() -> {
assertThat(MDC.get("hello")).isEqualTo("there");
});
es.wrapMDC(() -> {
}).run(); // will clear the MDC. But this should be OK
MDC.put("hello", "there");
es.wrap(() -> {
assertThat(MDC.get("hello")).isEqualTo("there");
return "Callable";
});
}).call(); // will clear the MDC. But this should be OK
MDC.put("hello", "there");
CountDownLatch latch = new CountDownLatch(1);
es.execute(() -> {
// the assertion is executed async, so it will only logged on console
assertThat(MDC.get("hello")).isEqualTo("there");
latch.countDown();
});
assertTrue(latch.await(5, TimeUnit.SECONDS));
es.submit(() -> {
assertThat(MDC.get("hello")).isEqualTo("there");
return "Callable";
});
}).get();
MDC.clear();
es.execute(()->{
assertThat(MDC.get("hello")).isNull();
});
es.shutdown();
}