From 29f2c10a329d50e186f719f6b8f4622342878173 Mon Sep 17 00:00:00 2001 From: Gal Leibovici Date: Thu, 4 Jul 2024 18:17:32 +0300 Subject: [PATCH] test --- .../InstrumentedExecutorServiceTest.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/metrics-core/src/test/java/com/codahale/metrics/InstrumentedExecutorServiceTest.java b/metrics-core/src/test/java/com/codahale/metrics/InstrumentedExecutorServiceTest.java index 76e026b0b6..4cf8e6f23a 100644 --- a/metrics-core/src/test/java/com/codahale/metrics/InstrumentedExecutorServiceTest.java +++ b/metrics-core/src/test/java/com/codahale/metrics/InstrumentedExecutorServiceTest.java @@ -10,13 +10,15 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class InstrumentedExecutorServiceTest { @@ -167,6 +169,32 @@ public void reportsTasksInformationForThreadPoolExecutor() throws Exception { assertThat(poolSize.getValue()).isEqualTo(1); } + @Test + public void reportsRejectedTasksForThreadPoolExecutor() throws Exception { + executor = new ThreadPoolExecutor(1, 1, + 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1)); + instrumentedExecutorService = new InstrumentedExecutorService(executor, registry, "tp"); + final Counter rejected = registry.counter("tp.rejected"); + assertThat(rejected.getCount()).isEqualTo(0); + + final CountDownLatch latch = new CountDownLatch(1); + + Runnable runnable = () -> { + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }; + + Future executingFuture = instrumentedExecutorService.submit(runnable); + Future queuedFuture = instrumentedExecutorService.submit(runnable); + assertThatThrownBy(() -> instrumentedExecutorService.submit(runnable)) + .isInstanceOf(RejectedExecutionException.class); + latch.countDown(); + assertThat(rejected.getCount()).isEqualTo(1); + } + @Test @SuppressWarnings("unchecked") public void reportsTasksInformationForForkJoinPool() throws Exception {