Search in sources :

Example 6 with MatcherOf

use of org.cactoos.matchers.MatcherOf in project cactoos by yegor256.

the class AsyncFuncTest method runsInBackgroundWithoutFuture.

@Test
public void runsInBackgroundWithoutFuture() {
    final CountDownLatch latch = new CountDownLatch(1);
    MatcherAssert.assertThat("Can't run in the background without us touching the Future", new AsyncFunc<>(input -> {
        latch.countDown();
    }), new FuncApplies<>(true, new MatcherOf<Future<String>>(future -> {
        return latch.await(1L, TimeUnit.SECONDS);
    })));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Future(java.util.concurrent.Future) MatcherAssert(org.hamcrest.MatcherAssert) FuncApplies(org.cactoos.matchers.FuncApplies) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Proc(org.cactoos.Proc) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) MatcherOf(org.cactoos.matchers.MatcherOf) MatcherOf(org.cactoos.matchers.MatcherOf) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with MatcherOf

use of org.cactoos.matchers.MatcherOf in project cactoos by yegor256.

the class RunnableOfTest method convertsProcIntoRunnable.

@Test
public void convertsProcIntoRunnable() {
    final AtomicBoolean done = new AtomicBoolean();
    MatcherAssert.assertThat("Can't execute Runnable with ProcOf", new RunnableOf<>(new ProcOf<>(input -> {
        done.set(true);
        return 1;
    })), new MatcherOf<Runnable>(input -> {
        input.run();
        return done.get();
    }));
}
Also used : MatcherAssert(org.hamcrest.MatcherAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) MatcherOf(org.cactoos.matchers.MatcherOf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 8 with MatcherOf

use of org.cactoos.matchers.MatcherOf in project cactoos by yegor256.

the class RunnableOfTest method convertsFuncIntoRunnable.

@Test
public void convertsFuncIntoRunnable() {
    final AtomicBoolean done = new AtomicBoolean();
    MatcherAssert.assertThat("Can't execute Runnable with Func", new RunnableOf<>(input -> {
        done.set(true);
        return 1;
    }), new MatcherOf<Runnable>(input -> {
        input.run();
        return done.get();
    }));
}
Also used : MatcherAssert(org.hamcrest.MatcherAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) MatcherOf(org.cactoos.matchers.MatcherOf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 9 with MatcherOf

use of org.cactoos.matchers.MatcherOf in project cactoos by yegor256.

the class RunnableOfTest method convertsCallableIntoRunnable.

@Test
public void convertsCallableIntoRunnable() {
    final AtomicBoolean done = new AtomicBoolean();
    MatcherAssert.assertThat("Can't execute Runnable with Callable", new RunnableOf<>(() -> {
        done.set(true);
        return null;
    }), new MatcherOf<Runnable>(input -> {
        input.run();
        return done.get();
    }));
}
Also used : MatcherAssert(org.hamcrest.MatcherAssert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) MatcherOf(org.cactoos.matchers.MatcherOf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 10 with MatcherOf

use of org.cactoos.matchers.MatcherOf in project cactoos by yegor256.

the class AsyncFuncTest method runsInBackgroundWithThreadFactory.

@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("Can't run in the background with specific thread factory", new AsyncFunc<>(input -> {
        if (!input.equals(Thread.currentThread().getName())) {
            throw new IllegalStateException("Another thread factory was used");
        }
        latch.countDown();
    }, factory), new FuncApplies<>(name, new MatcherOf<Future<Void>>(future -> {
        future.get();
        return latch.getCount() == 0;
    })));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Future(java.util.concurrent.Future) MatcherAssert(org.hamcrest.MatcherAssert) FuncApplies(org.cactoos.matchers.FuncApplies) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Proc(org.cactoos.Proc) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) MatcherOf(org.cactoos.matchers.MatcherOf) ThreadFactory(java.util.concurrent.ThreadFactory) MatcherOf(org.cactoos.matchers.MatcherOf) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MatcherOf (org.cactoos.matchers.MatcherOf)11 Test (org.junit.Test)11 MatcherAssert (org.hamcrest.MatcherAssert)8 TextOf (org.cactoos.text.TextOf)5 Matchers (org.hamcrest.Matchers)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 Future (java.util.concurrent.Future)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 TimeUnit (java.util.concurrent.TimeUnit)3 Proc (org.cactoos.Proc)3 FuncApplies (org.cactoos.matchers.FuncApplies)3 TextHasString (org.cactoos.matchers.TextHasString)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Path (java.nio.file.Path)2 SubText (org.cactoos.text.SubText)2 UpperText (org.cactoos.text.UpperText)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1