Search in sources :

Example 1 with Satisfies

use of org.llorllale.cactoos.matchers.Satisfies in project cactoos by yegor256.

the class AsyncTest method runsInBackgroundWithoutFuture.

@Test
void runsInBackgroundWithoutFuture() {
    final CountDownLatch latch = new CountDownLatch(1);
    new Assertion<>("Must run in the background without us touching the Future", new Async<>(new FuncOf<>(input -> latch.countDown(), true)), new IsApplicable<>(true, new Satisfies<>(future -> latch.await(1L, TimeUnit.SECONDS)))).affirm();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) Assertion(org.llorllale.cactoos.matchers.Assertion) IsEqual(org.hamcrest.core.IsEqual) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) Satisfies(org.llorllale.cactoos.matchers.Satisfies) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with Satisfies

use of org.llorllale.cactoos.matchers.Satisfies in project cactoos by yegor256.

the class AsyncTest method runsInBackgroundWithExecutorService.

@Test
void runsInBackgroundWithExecutorService() {
    final String name = "secret name for thread executor";
    final ThreadFactory factory = r -> new Thread(r, name);
    final CountDownLatch latch = new CountDownLatch(1);
    new Assertion<>("Must run in the background with specific thread executor", 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 IsApplicable<>(name, new Satisfies<>(future -> {
        future.get();
        return latch.getCount() == 0;
    }))).affirm();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) Assertion(org.llorllale.cactoos.matchers.Assertion) IsEqual(org.hamcrest.core.IsEqual) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) Satisfies(org.llorllale.cactoos.matchers.Satisfies) ThreadFactory(java.util.concurrent.ThreadFactory) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with Satisfies

use of org.llorllale.cactoos.matchers.Satisfies in project cactoos by yegor256.

the class AsyncTest method runsInBackgroundWithThreadFactory.

@Test
void runsInBackgroundWithThreadFactory() {
    final String name = "secret name for thread factory";
    final ThreadFactory factory = r -> new Thread(r, name);
    final CountDownLatch latch = new CountDownLatch(1);
    new Assertion<>("Must run in the background with specific thread factory", new Async<>(new FuncOf<>(input -> {
        if (!input.equals(Thread.currentThread().getName())) {
            throw new IllegalStateException("Another thread factory was used");
        }
        latch.countDown();
    }, true), factory), new IsApplicable<>(name, new Satisfies<>(future -> {
        future.get();
        return latch.getCount() == 0;
    }))).affirm();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) Assertion(org.llorllale.cactoos.matchers.Assertion) IsEqual(org.hamcrest.core.IsEqual) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) Satisfies(org.llorllale.cactoos.matchers.Satisfies) ThreadFactory(java.util.concurrent.ThreadFactory) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with Satisfies

use of org.llorllale.cactoos.matchers.Satisfies in project cactoos by yegor256.

the class ScalarOfTest method worksWithRunnable.

@Test
void worksWithRunnable() {
    final Object obj = new Object();
    final Object result = new Object();
    final AtomicReference<Object> done = new AtomicReference<>();
    new Assertion<>("Must convert Runnable into Scalar", new ScalarOf<>(new RunnableOf(() -> {
        done.set(result);
    }), obj), new Satisfies<>(scalar -> {
        final Object res = scalar.value();
        return res.equals(obj) && done.get().equals(result);
    })).affirm();
}
Also used : Satisfies(org.llorllale.cactoos.matchers.Satisfies) AtomicReference(java.util.concurrent.atomic.AtomicReference) RunnableOf(org.cactoos.proc.RunnableOf) Test(org.junit.jupiter.api.Test)

Example 5 with Satisfies

use of org.llorllale.cactoos.matchers.Satisfies in project cactoos by yegor256.

the class InputOfTest method makesDataAvailable.

@Test
void makesDataAvailable() throws Exception {
    final String content = "Hello,חבר!";
    new Assertion<>("must show that data is available", new InputOf(content).stream(), new Satisfies<>(s -> s.available() > 0)).affirm();
}
Also used : Satisfies(org.llorllale.cactoos.matchers.Satisfies) HasString(org.llorllale.cactoos.matchers.HasString) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 Satisfies (org.llorllale.cactoos.matchers.Satisfies)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 TimeUnit (java.util.concurrent.TimeUnit)3 IsEqual (org.hamcrest.core.IsEqual)3 Assertion (org.llorllale.cactoos.matchers.Assertion)3 IsApplicable (org.llorllale.cactoos.matchers.IsApplicable)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RunnableOf (org.cactoos.proc.RunnableOf)1 HasString (org.llorllale.cactoos.matchers.HasString)1