From b76370029fbfcc3fe72fb8492db6f444d1df3ef6 Mon Sep 17 00:00:00 2001 From: Vedran Date: Sat, 21 Apr 2018 16:01:13 +0200 Subject: [PATCH] future cancellation --- src/main/java/org/cactoos/func/TimedFunc.java | 27 +++++++++++++--- .../java/org/cactoos/func/TimedFuncTest.java | 31 +++++++++++++++++-- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/cactoos/func/TimedFunc.java b/src/main/java/org/cactoos/func/TimedFunc.java index 0f5dacb05f..42f330d379 100644 --- a/src/main/java/org/cactoos/func/TimedFunc.java +++ b/src/main/java/org/cactoos/func/TimedFunc.java @@ -23,13 +23,15 @@ */ package org.cactoos.func; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.cactoos.Func; import org.cactoos.Proc; /** * Function that gets interrupted after a certain time has passed. - * * @author Vedran Vatavuk (123vgv@gmail.com) * @version $Id$ * @param Type of input @@ -41,7 +43,7 @@ public final class TimedFunc implements Func { /** * Origin function. */ - private final Func origin; + private final Func> func; /** * Milliseconds. @@ -63,13 +65,28 @@ public TimedFunc(final Proc proc, final long milliseconds) { * @param milliseconds Milliseconds */ public TimedFunc(final Func function, final long milliseconds) { - this.origin = function; + this(milliseconds, new AsyncFunc<>(function)); + } + + /** + * Ctor. + * @param async Async function + * @param milliseconds Milliseconds + */ + public TimedFunc(final long milliseconds, final Func> async) { + this.func = async; this.time = milliseconds; } @Override public Y apply(final X input) throws Exception { - return new AsyncFunc<>(this.origin).apply(input) - .get(this.time, TimeUnit.MILLISECONDS); + final Future future = this.func.apply(input); + try { + return future.get(this.time, TimeUnit.MILLISECONDS); + } catch (final InterruptedException | ExecutionException + | TimeoutException exp) { + future.cancel(true); + throw exp; + } } } diff --git a/src/test/java/org/cactoos/func/TimedFuncTest.java b/src/test/java/org/cactoos/func/TimedFuncTest.java index 16dddb5310..82e71ecff7 100644 --- a/src/test/java/org/cactoos/func/TimedFuncTest.java +++ b/src/test/java/org/cactoos/func/TimedFuncTest.java @@ -23,6 +23,8 @@ */ package org.cactoos.func; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; import org.cactoos.iterable.Endless; import org.cactoos.scalar.And; @@ -32,7 +34,6 @@ /** * Test case for {@link TimedFunc}. - * * @author Vedran Vatavuk (123vgv@gmail.com) * @version $Id$ * @since 0.29.3 @@ -67,7 +68,33 @@ public void procGetsInterrupted() throws Exception { } @Test - public void functionGetsExecuted() throws Exception { + @SuppressWarnings("PMD.AvoidCatchingGenericException") + public void futureTaskIsCancelled() { + final long period = 50L; + final long time = 2000L; + final Future future = Executors.newSingleThreadExecutor() + .submit( + () -> { + Thread.sleep(time); + return true; + } + ); + try { + new TimedFunc( + period, + input -> future + ).apply(true); + // @checkstyle IllegalCatchCheck (1 line) + } catch (final Exception exp) { + MatcherAssert.assertThat( + future.isCancelled(), + Matchers.equalTo(true) + ); + } + } + + @Test + public void functionIsExecuted() throws Exception { final long period = 3000L; MatcherAssert.assertThat( new TimedFunc(