diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-09-15 15:11:15 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-09-15 15:14:02 +0200 |
commit | f303d9e78873d6916634865517dc9744cf7f99c3 (patch) | |
tree | 063fc1fab0b51e4fea1b9338d31e518a89ca327f /yolean | |
parent | c9cd995047022d19e416109a21d6ef45eaa33eaf (diff) |
Move Sleeper to yolean and simplify
Diffstat (limited to 'yolean')
4 files changed, 125 insertions, 0 deletions
diff --git a/yolean/abi-spec.json b/yolean/abi-spec.json index 85aaaf5f64e..45ba75d736d 100644 --- a/yolean/abi-spec.json +++ b/yolean/abi-spec.json @@ -12,6 +12,19 @@ ], "fields": [] }, + "com.yahoo.yolean.Exceptions$RunnableThrowingInterruptedException": { + "superClass": "java.lang.Object", + "interfaces": [], + "attributes": [ + "public", + "interface", + "abstract" + ], + "methods": [ + "public abstract void run()" + ], + "fields": [] + }, "com.yahoo.yolean.Exceptions$SupplierThrowingIOException": { "superClass": "java.lang.Object", "interfaces": [], @@ -36,6 +49,8 @@ "public static java.lang.String toMessageString(java.lang.Throwable)", "public static java.util.Optional findCause(java.lang.Throwable, java.lang.Class)", "public static void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingIOException)", + "public static void uncheckInterrupted(com.yahoo.yolean.Exceptions$RunnableThrowingInterruptedException)", + "public static void uncheckInterruptedAndRestoreFlag(com.yahoo.yolean.Exceptions$RunnableThrowingInterruptedException)", "public static varargs void uncheck(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.String, java.lang.String[])", "public static void uncheckAndIgnore(com.yahoo.yolean.Exceptions$RunnableThrowingIOException, java.lang.Class)", "public static java.lang.Object uncheck(com.yahoo.yolean.Exceptions$SupplierThrowingIOException)", @@ -45,6 +60,23 @@ ], "fields": [] }, + "com.yahoo.yolean.UncheckedInterruptedException": { + "superClass": "java.lang.RuntimeException", + "interfaces": [], + "attributes": [ + "public" + ], + "methods": [ + "public void <init>(java.lang.String, java.lang.InterruptedException, boolean)", + "public void <init>(java.lang.InterruptedException, boolean)", + "public void <init>(java.lang.String, boolean)", + "public void <init>(java.lang.String, java.lang.InterruptedException)", + "public void <init>(java.lang.InterruptedException)", + "public java.lang.InterruptedException getCause()", + "public bridge synthetic java.lang.Throwable getCause()" + ], + "fields": [] + }, "com.yahoo.yolean.chain.After": { "superClass": "java.lang.Object", "interfaces": [ @@ -232,6 +264,25 @@ ], "fields": [] }, + "com.yahoo.yolean.concurrent.Sleeper": { + "superClass": "java.lang.Object", + "interfaces": [], + "attributes": [ + "public", + "interface", + "abstract" + ], + "methods": [ + "public void sleep(java.time.Duration)", + "public void sleepChecked(java.time.Duration)", + "public void sleep(long)", + "public abstract void sleepChecked(long)" + ], + "fields": [ + "public static final com.yahoo.yolean.concurrent.Sleeper DEFAULT", + "public static final com.yahoo.yolean.concurrent.Sleeper NOOP" + ] + }, "com.yahoo.yolean.concurrent.ThreadRobustList": { "superClass": "java.lang.Object", "interfaces": [ diff --git a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java index c377ee3ac37..b2c6d782eb4 100644 --- a/yolean/src/main/java/com/yahoo/yolean/Exceptions.java +++ b/yolean/src/main/java/com/yahoo/yolean/Exceptions.java @@ -70,6 +70,22 @@ public class Exceptions { } } + public static void uncheckInterrupted(RunnableThrowingInterruptedException runnable) { + try { + runnable.run(); + } catch (InterruptedException e) { + throw new UncheckedInterruptedException(e, false); + } + } + + public static void uncheckInterruptedAndRestoreFlag(RunnableThrowingInterruptedException runnable) { + try { + runnable.run(); + } catch (InterruptedException e) { + throw new UncheckedInterruptedException(e, true); + } + } + /** * Wraps any IOException thrown from a runnable in an UncheckedIOException w/message. */ @@ -110,6 +126,8 @@ public class Exceptions { void run() throws IOException; } + @FunctionalInterface public interface RunnableThrowingInterruptedException { void run() throws InterruptedException; } + /** * Wraps any IOException thrown from a supplier in an UncheckedIOException. */ diff --git a/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java b/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java new file mode 100644 index 00000000000..d3317b5fb26 --- /dev/null +++ b/yolean/src/main/java/com/yahoo/yolean/UncheckedInterruptedException.java @@ -0,0 +1,27 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.yolean; + +/** + * Wraps an {@link InterruptedException} with an unchecked exception. + * + * @author bjorncs + */ +public class UncheckedInterruptedException extends RuntimeException { + + public UncheckedInterruptedException(String message, InterruptedException cause, boolean restoreInterruptFlag) { + super(message, cause); + if (restoreInterruptFlag) Thread.currentThread().interrupt(); + } + + public UncheckedInterruptedException(InterruptedException cause, boolean restoreInterruptFlags) { + this(cause.toString(), cause, restoreInterruptFlags); + } + + public UncheckedInterruptedException(String message, boolean restoreInterruptFlag) { this(message, null, false); } + + public UncheckedInterruptedException(String message, InterruptedException cause) { this(message, cause, false); } + + public UncheckedInterruptedException(InterruptedException cause) { this(cause.toString(), cause, false); } + + @Override public InterruptedException getCause() { return (InterruptedException) super.getCause(); } +} diff --git a/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java b/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java new file mode 100644 index 00000000000..530be935bc1 --- /dev/null +++ b/yolean/src/main/java/com/yahoo/yolean/concurrent/Sleeper.java @@ -0,0 +1,29 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.yolean.concurrent; + +import com.yahoo.yolean.UncheckedInterruptedException; + +import java.time.Duration; + +import static com.yahoo.yolean.Exceptions.uncheckInterrupted; + +/** + * An abstraction used for mocking {@link Thread#sleep(long)} in unit tests. + * + * @author bjorncs + */ +public interface Sleeper { + default void sleep(Duration duration) throws UncheckedInterruptedException { + uncheckInterrupted(() -> sleepChecked(duration.toMillis())); + } + + default void sleepChecked(Duration duration) throws InterruptedException { sleepChecked(duration.toMillis()); } + + default void sleep(long millis) throws UncheckedInterruptedException { uncheckInterrupted(() -> sleepChecked(millis)); } + + void sleepChecked(long millis) throws InterruptedException; + + Sleeper DEFAULT = Thread::sleep; + Sleeper NOOP = millis -> {}; + +} |