Search in sources :

Example 1 with Condition

use of org.assertj.core.api.Condition in project spring-boot by spring-projects.

the class ConfigFileApplicationListenerTests method matchingPropertySource.

private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
    return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {

        @Override
        public boolean matches(ConfigurableEnvironment value) {
            MutablePropertySources sources = new MutablePropertySources(value.getPropertySources());
            ConfigurationPropertySources.finishAndRelocate(sources);
            return sources.contains(sourceName);
        }
    };
}
Also used : Condition(org.assertj.core.api.Condition) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 2 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class FluxBufferWhenTest method timedOutBuffersDontLeak.

// see https://github.com/reactor/reactor-core/issues/969
@Test
public void timedOutBuffersDontLeak() throws InterruptedException {
    LongAdder created = new LongAdder();
    LongAdder finalized = new LongAdder();
    class Wrapper {

        final int i;

        Wrapper(int i) {
            created.increment();
            this.i = i;
        }

        @Override
        public String toString() {
            return "{i=" + i + '}';
        }

        @Override
        protected void finalize() {
            finalized.increment();
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final UnicastProcessor<Wrapper> processor = UnicastProcessor.create();
    Flux<Integer> emitter = Flux.range(1, 200).delayElements(Duration.ofMillis(25)).doOnNext(i -> processor.onNext(new Wrapper(i))).doOnError(processor::onError).doOnComplete(processor::onComplete);
    Mono<List<Tuple3<Long, String, Long>>> buffers = processor.buffer(Duration.ofMillis(500), Duration.ofMillis(250)).filter(b -> b.size() > 0).index().doOnNext(it -> System.gc()).map(t2 -> Tuples.of(t2.getT1(), String.format("from %s to %s", t2.getT2().get(0), t2.getT2().get(t2.getT2().size() - 1)), finalized.longValue())).doOnNext(v -> LOGGER.debug(v.toString())).doOnComplete(latch::countDown).collectList();
    emitter.subscribe();
    List<Tuple3<Long, String, Long>> finalizeStats = buffers.block(Duration.ofSeconds(30));
    Condition<? super Tuple3<Long, String, Long>> hasFinalized = new Condition<>(t3 -> t3.getT3() > 0, "has finalized");
    // at least 5 intermediate finalize
    assertThat(finalizeStats).areAtLeast(5, hasFinalized);
    assertThat(latch.await(1, TimeUnit.SECONDS)).as("buffers already blocked").isTrue();
    LOGGER.debug("final GC");
    System.gc();
    Thread.sleep(500);
    assertThat(finalized.longValue()).as("final GC collects all").isEqualTo(created.longValue());
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuple3(reactor.util.function.Tuple3) Tuples(reactor.util.function.Tuples) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Queues(reactor.util.concurrent.Queues) HashSet(java.util.HashSet) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Duration(java.time.Duration) Logger(reactor.util.Logger) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Condition(org.assertj.core.api.Condition) Assert(org.junit.Assert) Collections(java.util.Collections) Condition(org.assertj.core.api.Condition) CountDownLatch(java.util.concurrent.CountDownLatch) LongAdder(java.util.concurrent.atomic.LongAdder) Tuple3(reactor.util.function.Tuple3) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class TopicProcessorTest method testCustomRequestTaskThreadName.

@Test
public void testCustomRequestTaskThreadName() {
    String expectedName = "topicProcessorRequestTaskCreate";
    // NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed
    ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName));
    TopicProcessor<Object> processor = TopicProcessor.builder().executor(Executors.newCachedThreadPool()).requestTaskExecutor(customTaskExecutor).bufferSize(8).waitStrategy(WaitStrategy.liteBlocking()).autoCancel(true).build();
    processor.requestTask(Operators.cancelledSubscription());
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);
    // cleanup to avoid visibility in other tests
    customTaskExecutor.shutdownNow();
    processor.forceShutdown();
    Condition<Thread> customRequestTaskThread = new Condition<>(thread -> expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName);
    Assertions.assertThat(threads).haveExactly(1, customRequestTaskThread);
}
Also used : Condition(org.assertj.core.api.Condition) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 4 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class TopicProcessorTest method testCustomRequestTaskThreadShare.

@Test
public void testCustomRequestTaskThreadShare() {
    String expectedName = "topicProcessorRequestTaskShare";
    // NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed
    ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName));
    TopicProcessor<Object> processor = TopicProcessor.builder().share(true).executor(Executors.newCachedThreadPool()).requestTaskExecutor(customTaskExecutor).bufferSize(8).waitStrategy(WaitStrategy.liteBlocking()).autoCancel(true).build();
    processor.requestTask(Operators.cancelledSubscription());
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);
    // cleanup to avoid visibility in other tests
    customTaskExecutor.shutdownNow();
    processor.forceShutdown();
    Condition<Thread> customRequestTaskThread = new Condition<>(thread -> expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName);
    Assertions.assertThat(threads).haveExactly(1, customRequestTaskThread);
}
Also used : Condition(org.assertj.core.api.Condition) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 5 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class FluxWindowWhenTest method noWindowRetained_gh975.

@Test
public void noWindowRetained_gh975() throws InterruptedException {
    LongAdder created = new LongAdder();
    LongAdder finalized = new LongAdder();
    class Wrapper {

        final int i;

        Wrapper(int i) {
            created.increment();
            this.i = i;
        }

        @Override
        public String toString() {
            return "{i=" + i + '}';
        }

        @Override
        protected void finalize() {
            finalized.increment();
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final UnicastProcessor<Wrapper> processor = UnicastProcessor.create();
    Flux<Integer> emitter = Flux.range(1, 400).delayElements(Duration.ofMillis(10)).doOnNext(i -> processor.onNext(new Wrapper(i))).doOnComplete(processor::onComplete);
    AtomicReference<FluxWindowWhen.WindowWhenMainSubscriber> startEndMain = new AtomicReference<>();
    AtomicReference<List> windows = new AtomicReference<>();
    Mono<List<Tuple3<Long, Integer, Long>>> buffers = processor.window(Duration.ofMillis(1000), Duration.ofMillis(500)).doOnSubscribe(s -> {
        FluxWindowWhen.WindowWhenMainSubscriber sem = (FluxWindowWhen.WindowWhenMainSubscriber) s;
        startEndMain.set(sem);
        windows.set(sem.windows);
    }).flatMap(f -> f.take(2)).index().doOnNext(it -> System.gc()).map(t2 -> Tuples.of(t2.getT1(), windows.get().size(), finalized.longValue())).doOnNext(v -> LOGGER.info(v.toString())).doOnComplete(latch::countDown).collectList();
    emitter.subscribe();
    List<Tuple3<Long, Integer, Long>> finalizeStats = buffers.block();
    // at least 5 intermediate finalize
    Condition<? super Tuple3<Long, Integer, Long>> hasFinalized = new Condition<>(t3 -> t3.getT3() > 0, "has finalized");
    assertThat(finalizeStats).areAtLeast(5, hasFinalized);
    assertThat(finalizeStats).allMatch(t3 -> t3.getT2() <= 3, "max 3 windows in flight");
    latch.await(10, TimeUnit.SECONDS);
    System.gc();
    Thread.sleep(500);
    assertThat(windows.get().size()).as("no window retained").isZero();
    assertThat(finalized.longValue()).as("final GC collects all").isEqualTo(created.longValue());
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuple3(reactor.util.function.Tuple3) Tuples(reactor.util.function.Tuples) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Queues(reactor.util.concurrent.Queues) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Duration(java.time.Duration) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Logger(reactor.util.Logger) Condition(org.assertj.core.api.Condition) Assertions(org.assertj.core.api.Assertions) Assert(org.junit.Assert) Condition(org.assertj.core.api.Condition) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LongAdder(java.util.concurrent.atomic.LongAdder) Tuple3(reactor.util.function.Tuple3) List(java.util.List) Test(org.junit.Test)

Aggregations

Condition (org.assertj.core.api.Condition)33 Test (org.junit.Test)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 List (java.util.List)9 Before (org.junit.Before)7 File (java.io.File)5 IOException (java.io.IOException)5 Arrays (java.util.Arrays)5 ExecutorService (java.util.concurrent.ExecutorService)4 Assertions (org.assertj.core.api.Assertions)4 Rule (org.junit.Rule)4 Mock (org.mockito.Mock)4 When (io.cucumber.java.en.When)3 SupportPage (io.syndesis.qe.pages.SupportPage)3 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Enumeration (java.util.Enumeration)3 TimeUnit (java.util.concurrent.TimeUnit)3 ZipEntry (java.util.zip.ZipEntry)3