From a747497b247dceafaafc86b62de6cffa6c008e34 Mon Sep 17 00:00:00 2001 From: Dmitry Vyazelenko <696855+vyazelenko@users.noreply.github.com> Date: Tue, 8 Aug 2023 15:15:40 +0200 Subject: [PATCH] [Java] Simplify SigInt and add a test. --- .../java/org/agrona/concurrent/SigInt.java | 25 ++--------- .../org/agrona/concurrent/SigIntTest.java | 44 +++++++++++++------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/agrona/src/main/java/org/agrona/concurrent/SigInt.java b/agrona/src/main/java/org/agrona/concurrent/SigInt.java index b77433bf9..723ab73d9 100644 --- a/agrona/src/main/java/org/agrona/concurrent/SigInt.java +++ b/agrona/src/main/java/org/agrona/concurrent/SigInt.java @@ -15,7 +15,6 @@ */ package org.agrona.concurrent; -import org.agrona.LangUtil; import sun.misc.Signal; import java.util.Objects; @@ -23,34 +22,18 @@ /** * Utility to allow the registration of a SIGINT handler that hides the unsupported {@link Signal} class. */ -public class SigInt -{ +public class SigInt { /** * Register a task to be run when a SIGINT is received. * * @param task to run on reception of the signal. */ - public static void register(final Runnable task) - { + public static void register(final Runnable task) { register("INT", task); } - static void register(final String signalName, final Runnable task) - { + static void register(final String signalName, final Runnable task) { Objects.requireNonNull(task); - - Signal.handle( - new Signal(signalName), - (signal) -> - { - try - { - task.run(); - } - catch (final Throwable t) - { - LangUtil.rethrowUnchecked(t); - } - }); + Signal.handle(new Signal(signalName), (signal) -> task.run()); } } diff --git a/agrona/src/test/java/org/agrona/concurrent/SigIntTest.java b/agrona/src/test/java/org/agrona/concurrent/SigIntTest.java index d76cf6017..d7301603a 100644 --- a/agrona/src/test/java/org/agrona/concurrent/SigIntTest.java +++ b/agrona/src/test/java/org/agrona/concurrent/SigIntTest.java @@ -28,28 +28,24 @@ import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import static org.mockito.Mockito.*; -class SigIntTest -{ +class SigIntTest { @Test - void throwsNullPointerExceptionIfRunnableIsNull() - { + void throwsNullPointerExceptionIfRunnableIsNull() { assertThrowsExactly(NullPointerException.class, () -> SigInt.register(null)); } @ParameterizedTest - @ValueSource(strings = { "INT", "TERM" }) - void shouldReplaceExistingSignalHandler(final String name) throws InterruptedException - { + @ValueSource(strings = {"INT", "TERM"}) + void shouldReplaceExistingSignalHandler(final String name) throws InterruptedException { final Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = - Thread.getDefaultUncaughtExceptionHandler(); + Thread.getDefaultUncaughtExceptionHandler(); final Signal signal = new Signal(name); final SignalHandler originalHandler = Signal.handle(signal, sig -> {}); - try - { + try { final CountDownLatch executed = new CountDownLatch(1); final Thread.UncaughtExceptionHandler exceptionHandler = mock(Thread.UncaughtExceptionHandler.class); - doAnswer((Answer)invocation -> + doAnswer((Answer) invocation -> { executed.countDown(); return null; @@ -75,11 +71,31 @@ void shouldReplaceExistingSignalHandler(final String name) throws InterruptedExc inOrder.verifyNoMoreInteractions(); verifyNoInteractions(oldHandler); - } - finally - { + } finally { Thread.setDefaultUncaughtExceptionHandler(defaultUncaughtExceptionHandler); Signal.handle(signal, originalHandler); } } + + @ParameterizedTest + @ValueSource(strings = {"INT"}) + void shouldReplaceExistingSignalHandlerNoException(final String name) throws InterruptedException { + final Signal signal = new Signal(name); + final SignalHandler originalHandler = Signal.handle(signal, sig -> {}); + + try { + final SignalHandler oldHandler = mock(SignalHandler.class); + Signal.handle(signal, oldHandler); + + final CountDownLatch executed = new CountDownLatch(1); + SigInt.register(name, executed::countDown); + + Signal.raise(signal); + + executed.await(); + verifyNoInteractions(oldHandler); + } finally { + Signal.handle(signal, originalHandler); + } + } }