Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 26, 2019
2 parents 4a03806 + ee97241 commit a834cb7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 71 deletions.
32 changes: 0 additions & 32 deletions src/main/java/org/cactoos/func/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.10
* @todo #861:30min Avoid usage of null value in ctor(Proc, ExecutorService),
* ctor(Proc, ThreadFactory) and ctor(Proc) which is against design
* principles.
* Perhaps with a creation of AsyncProc or removal of this functionality?
* Please take a look on #551 and #843 for more details.
*/
public final class Async<X, Y> implements Func<X, Future<Y>>, Proc<X> {

Expand All @@ -63,14 +58,6 @@ public final class Async<X, Y> implements Func<X, Future<Y>>, Proc<X> {
*/
private final ExecutorService executor;

/**
* Ctor.
* @param proc The proc
*/
public Async(final Proc<X> proc) {
this(new FuncOf<>(proc, null));
}

/**
* Ctor.
* @param fnc The func
Expand All @@ -79,15 +66,6 @@ public Async(final Func<X, Y> fnc) {
this(fnc, Executors.defaultThreadFactory());
}

/**
* Ctor.
* @param proc The proc
* @param fct Factory
*/
public Async(final Proc<X> proc, final ThreadFactory fct) {
this(new FuncOf<>(proc, null), fct);
}

/**
* Ctor.
* @param fnc The func
Expand All @@ -97,16 +75,6 @@ public Async(final Func<X, Y> fnc, final ThreadFactory fct) {
this(fnc, Executors.newSingleThreadExecutor(fct));
}

/**
* Ctor.
* @param proc The proc
* @param exec Executor Service
* @since 0.17
*/
public Async(final Proc<X> proc, final ExecutorService exec) {
this(new FuncOf<>(proc, null), exec);
}

/**
* Ctor.
* @param fnc The func
Expand Down
82 changes: 43 additions & 39 deletions src/test/java/org/cactoos/func/AsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.cactoos.Proc;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.FuncApplies;
import org.llorllale.cactoos.matchers.MatcherOf;

Expand All @@ -39,13 +38,14 @@
*
* @since 0.10
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class AsyncTest {
@Test
public void runsInBackground() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't run in the background",
new Async<>(
() -> new Async<>(
input -> {
TimeUnit.DAYS.sleep(1L);
return "done!";
Expand All @@ -57,36 +57,34 @@ public void runsInBackground() {
future -> !future.isDone()
)
)
);
).affirm();
}

@Test
public void runsAsProcInBackground() {
MatcherAssert.assertThat(
new Assertion<>(
"Can't run proc in the background",
input -> {
() -> input -> {
final CountDownLatch latch = new CountDownLatch(1);
new Async<>(
(Proc<Boolean>) ipt -> latch.countDown()
new FuncOf<>(ipt -> latch.countDown(), true)
).exec(input);
latch.await();
return true;
},
new FuncApplies<>(
true, Matchers.equalTo(true)
true, new IsEqual<>(true)
)
);
).affirm();
}

@Test
public void runsInBackgroundWithoutFuture() {
final CountDownLatch latch = new CountDownLatch(1);
MatcherAssert.assertThat(
new Assertion<>(
"Can't run in the background without us touching the Future",
new Async<>(
input -> {
latch.countDown();
}
() -> new Async<>(
new FuncOf<>(input -> latch.countDown(), true)
),
new FuncApplies<>(
true,
Expand All @@ -96,25 +94,28 @@ public void runsInBackgroundWithoutFuture() {
}
)
)
);
).affirm();
}

@Test
public void runsInBackgroundWithThreadFactory() {
final String name = "secret name for thread factory";
final ThreadFactory factory = r -> new Thread(r, name);
final CountDownLatch latch = new CountDownLatch(1);
MatcherAssert.assertThat(
new Assertion<>(
"Can't run in the background with specific thread factory",
new Async<>(
input -> {
if (!input.equals(Thread.currentThread().getName())) {
throw new IllegalStateException(
"Another thread factory was used"
);
}
latch.countDown();
},
() -> new Async<>(
new FuncOf<>(
input -> {
if (!input.equals(Thread.currentThread().getName())) {
throw new IllegalStateException(
"Another thread factory was used"
);
}
latch.countDown();
},
true
),
factory
),
new FuncApplies<>(
Expand All @@ -126,25 +127,28 @@ public void runsInBackgroundWithThreadFactory() {
}
)
)
);
).affirm();
}

@Test
public void runsInBackgroundWithExecutorService() {
final String name = "secret name for thread executor";
final ThreadFactory factory = r -> new Thread(r, name);
final CountDownLatch latch = new CountDownLatch(1);
MatcherAssert.assertThat(
new Assertion<>(
"Can't run in the background with specific thread executor",
new Async<>(
input -> {
if (!input.equals(Thread.currentThread().getName())) {
throw new IllegalStateException(
"Another thread executor was used"
);
}
latch.countDown();
},
() -> new Async<>(
new FuncOf<>(
input -> {
if (!input.equals(Thread.currentThread().getName())) {
throw new IllegalStateException(
"Another thread executor was used"
);
}
latch.countDown();
},
true
),
Executors.newSingleThreadExecutor(factory)
),
new FuncApplies<>(
Expand All @@ -156,6 +160,6 @@ public void runsInBackgroundWithExecutorService() {
}
)
)
);
).affirm();
}
}

1 comment on commit a834cb7

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on a834cb7 Feb 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 861-7a51e9e5 disappeared from src/main/java/org/cactoos/func/AsyncFunc.java, that's why I closed #884. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.