Search in sources :

Example 21 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class BatchingCollector method accept.

/* (non-Javadoc)
   * @see java.util.function.Consumer#accept(java.lang.Object)
   */
@Override
public void accept(final FastFuture<T> t) {
    active.add(t);
    if (active.size() > maxActive.getMaxActive()) {
        while (active.size() > maxActive.getReduceTo()) {
            final List<FastFuture<T>> toRemove = active.stream().filter(cf -> cf.isDone()).collect(Collectors.toList());
            active.removeAll(toRemove);
            results.addAll(toRemove);
            if (active.size() > maxActive.getReduceTo()) {
                final CompletableFuture promise = new CompletableFuture();
                FastFuture.xOf(active.size() - maxActive.getReduceTo(), () -> {
                    promise.complete(true);
                }, active.toArray(new FastFuture[0]));
                promise.join();
            }
        }
    }
}
Also used : List(java.util.List) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Getter(lombok.Getter) Collection(java.util.Collection) Builder(lombok.experimental.Builder) BlockingStream(com.oath.cyclops.types.futurestream.BlockingStream) Wither(lombok.experimental.Wither) CompletableFuture(java.util.concurrent.CompletableFuture) AllArgsConstructor(lombok.AllArgsConstructor) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture)

Example 22 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class EmptyCollector method accept.

/*
   *	@param t Result type
   * @see java.util.function.Consumer#accept(java.lang.Object)
   */
@Override
public void accept(final FastFuture<T> t) {
    active.add(t);
    if (active.size() > maxActive.getMaxActive()) {
        while (active.size() > maxActive.getReduceTo()) {
            final List<FastFuture> toRemove = active.stream().filter(cf -> cf.isDone()).peek(this::handleExceptions).collect(Collectors.toList());
            active.removeAll(toRemove);
            if (active.size() > maxActive.getReduceTo()) {
                final CompletableFuture promise = new CompletableFuture();
                FastFuture.xOf(active.size() - maxActive.getReduceTo(), () -> promise.complete(true), active.toArray(new FastFuture[0]));
                promise.join();
            }
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture)

Example 23 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class LazyStreamWrapper method injectFutures.

public Stream<FastFuture> injectFutures() {
    final FastFuture f = pipeline.build();
    final Function<Object, FastFuture> factory = v -> {
        final FastFuture next = pool != null ? pool.next(() -> new FastFuture<>(f.getPipeline(), fut -> pool.done(fut))) : new FastFuture<>(f.getPipeline(), 0);
        next.set(v);
        return next;
    };
    if (react.isStreamOfFutures())
        return convertCompletableFutures(f.getPipeline());
    final Stream<FastFuture> result = values.get().map(factory);
    return result;
}
Also used : FuturePool(com.oath.cyclops.internal.react.async.future.FuturePool) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) LazyReact(cyclops.futurestream.LazyReact) Wither(lombok.experimental.Wither) CompletableFuture(java.util.concurrent.CompletableFuture) FinalPipeline(com.oath.cyclops.internal.react.async.future.FinalPipeline) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) ManyToOneConcurrentArrayQueue(org.agrona.concurrent.ManyToOneConcurrentArrayQueue) AllArgsConstructor(lombok.AllArgsConstructor) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture)

Example 24 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class EmptyCollectorTest method testAcceptMock495.

@Test
public void testAcceptMock495() {
    collector = new EmptyCollector<>(new MaxActive(500, 5), cf -> cf.join());
    FastFuture cf = Mockito.mock(FastFuture.class);
    BDDMockito.given(cf.isDone()).willReturn(true);
    for (int i = 0; i < 1000; i++) {
        collector.accept(cf);
    }
    Mockito.verify(cf, Mockito.times(501)).isDone();
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) EmptyCollector(com.oath.cyclops.react.collectors.lazy.EmptyCollector) Mockito.verify(org.mockito.Mockito.verify) Assert.assertThat(org.junit.Assert.assertThat) BDDMockito(org.mockito.BDDMockito) Mockito(org.mockito.Mockito) BDDMockito.given(org.mockito.BDDMockito.given) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Test(org.junit.Test)

Example 25 with FastFuture

use of com.oath.cyclops.internal.react.async.future.FastFuture in project cyclops by aol.

the class FastFutureTest method firstRecover.

@Test
public void firstRecover() {
    FastFuture f = future.exceptionally(e -> "hello world").thenApply(v -> {
        throw new RuntimeException();
    }).build();
    f.set("boo!");
    assertThat(f.join(), equalTo("hello world"));
}
Also used : FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FinalPipeline(com.oath.cyclops.internal.react.async.future.FinalPipeline) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) PipelineBuilder(com.oath.cyclops.internal.react.async.future.PipelineBuilder) Before(org.junit.Before) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) Test(org.junit.Test)

Aggregations

FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)35 Test (org.junit.Test)30 FileNotFoundException (java.io.FileNotFoundException)21 IOException (java.io.IOException)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 ArrayList (java.util.ArrayList)19 Assert.assertThat (org.junit.Assert.assertThat)18 Assert.assertTrue (org.junit.Assert.assertTrue)18 Before (org.junit.Before)18 FinalPipeline (com.oath.cyclops.internal.react.async.future.FinalPipeline)17 PipelineBuilder (com.oath.cyclops.internal.react.async.future.PipelineBuilder)17 List (java.util.List)17 ForkJoinPool (java.util.concurrent.ForkJoinPool)16 Matchers.equalTo (org.hamcrest.Matchers.equalTo)16 Assert.assertFalse (org.junit.Assert.assertFalse)16 Assert.assertNotNull (org.junit.Assert.assertNotNull)16 Assert.assertNull (org.junit.Assert.assertNull)16 EmptyCollector (com.oath.cyclops.react.collectors.lazy.EmptyCollector)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Function (java.util.function.Function)2