Skip to content

Commit

Permalink
future cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedran authored and Vedran committed Apr 21, 2018
1 parent 9e611e7 commit b763700
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/main/java/org/cactoos/func/TimedFunc.java
Expand Up @@ -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 <X> Type of input
Expand All @@ -41,7 +43,7 @@ public final class TimedFunc<X, Y> implements Func<X, Y> {
/**
* Origin function.
*/
private final Func<X, Y> origin;
private final Func<X, Future<Y>> func;

/**
* Milliseconds.
Expand All @@ -63,13 +65,28 @@ public TimedFunc(final Proc<X> proc, final long milliseconds) {
* @param milliseconds Milliseconds
*/
public TimedFunc(final Func<X, Y> 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<X, Future<Y>> 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<Y> future = this.func.apply(input);
try {
return future.get(this.time, TimeUnit.MILLISECONDS);
} catch (final InterruptedException | ExecutionException
| TimeoutException exp) {
future.cancel(true);
throw exp;
}
}
}
31 changes: 29 additions & 2 deletions src/test/java/org/cactoos/func/TimedFuncTest.java
Expand Up @@ -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;
Expand All @@ -32,7 +34,6 @@

/**
* Test case for {@link TimedFunc}.
*
* @author Vedran Vatavuk (123vgv@gmail.com)
* @version $Id$
* @since 0.29.3
Expand Down Expand Up @@ -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<Boolean> future = Executors.newSingleThreadExecutor()
.submit(
() -> {
Thread.sleep(time);
return true;
}
);
try {
new TimedFunc<Boolean, Boolean>(
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<Boolean, Boolean>(
Expand Down

0 comments on commit b763700

Please sign in to comment.