Search in sources :

Example 6 with Consumer

use of java8.util.function.Consumer in project streamsupport by stefan-zobel.

the class Collection8Test method testDetectRaces.

/**
 * Motley crew of threads concurrently randomly hammer the collection.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testDetectRaces(String description, Supplier<CollectionImplementation> sci) throws Throwable {
    CollectionImplementation impl = sci.get();
    if (!impl.isConcurrent())
        return;
    if (HAS_JAVA8_SPLITERATOR_BUG && LinkedBlockingDeque.class.equals(impl.klazz())) {
        // https://bugs.openjdk.java.net/browse/JDK-8169739
        return;
    }
    if (CopyOnWriteArraySet.class.equals(impl.klazz())) {
        return;
    }
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final Collection c = impl.emptyCollection();
    final long testDurationMillis = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Object one = impl.makeElement(1);
    final Object two = impl.makeElement(2);
    final Consumer checkSanity = x -> assertTrue(x == one || x == two);
    final Consumer<Object[]> checkArraySanity = array -> {
        // assertTrue(array.length <= 2); // duplicates are permitted
        for (Object x : array) assertTrue(x == one || x == two);
    };
    final Object[] emptyArray = (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
    final List<Future<?>> futures;
    // register this thread
    final Phaser threadsStarted = new Phaser(1);
    final Runnable[] frobbers = { () -> Iterables.forEach(c, checkSanity), () -> StreamSupport.stream(c).forEach(checkSanity), () -> StreamSupport.parallelStream(c).forEach(checkSanity), () -> Spliterators.spliterator(c).trySplit(), () -> {
        Spliterator<?> s = Spliterators.spliterator(c);
        s.tryAdvance(checkSanity);
        s.trySplit();
    }, () -> {
        Spliterator<?> s = Spliterators.spliterator(c);
        do {
        } while (s.tryAdvance(checkSanity));
    }, () -> {
        for (Object x : c) checkSanity.accept(x);
    }, () -> checkArraySanity.accept(c.toArray()), () -> checkArraySanity.accept(c.toArray(emptyArray)), () -> {
        Object[] a = new Object[5];
        Object three = impl.makeElement(3);
        Arrays.fill(a, 0, a.length, three);
        Object[] x = c.toArray(a);
        if (x == a)
            for (int i = 0; i < a.length && a[i] != null; i++) checkSanity.accept(a[i]);
        else
            // A careful reading of the spec does not support:
            // for (i++; i < a.length; i++) assertSame(three, a[i]);
            checkArraySanity.accept(x);
    }, adderRemover(c, one), adderRemover(c, two) };
    final List<Runnable> tasks = J8Arrays.stream(frobbers).filter(// random subset
    task -> rnd.nextBoolean()).map(task -> (Runnable) () -> {
        threadsStarted.arriveAndAwaitAdvance();
        while (!done.get()) task.run();
    }).collect(Collectors.<Runnable>toList());
    final ExecutorService pool = Executors.newCachedThreadPool();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(pool, done);
        threadsStarted.bulkRegister(tasks.size());
        futures = StreamSupport.stream(tasks).map(pool::submit).collect(Collectors.toList());
        threadsStarted.arriveAndDeregister();
        Thread.sleep(testDurationMillis);
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
    for (Future<?> future : futures) assertNull(future.get(0L, MILLISECONDS));
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) PriorityQueue(java.util.PriorityQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) Iterables(java8.lang.Iterables) Deque(java.util.Deque) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) HashSet(java.util.HashSet) Phaser(java8.util.concurrent.Phaser) Iterators(java8.util.Iterators) Future(java.util.concurrent.Future) J8Arrays(java8.util.J8Arrays) Vector(java.util.Vector) Consumer(java8.util.function.Consumer) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) BlockingDeque(java.util.concurrent.BlockingDeque) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Spliterators(java8.util.Spliterators) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) StreamSupport(java8.util.stream.StreamSupport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Supplier(java8.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HOURS(java.util.concurrent.TimeUnit.HOURS) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConcurrentModificationException(java.util.ConcurrentModificationException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java8.util.function.Consumer) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) Future(java.util.concurrent.Future) Phaser(java8.util.concurrent.Phaser) Test(org.testng.annotations.Test)

Example 7 with Consumer

use of java8.util.function.Consumer in project streamsupport by stefan-zobel.

the class Collection8Test method testStreamForEach.

/**
 * stream().forEach returns elements in the collection
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testStreamForEach(String description, Supplier<CollectionImplementation> sci) throws Throwable {
    CollectionImplementation impl = sci.get();
    final Collection c = impl.emptyCollection();
    final Object x = impl.makeElement(1);
    final Object y = impl.makeElement(2);
    final ArrayList found = new ArrayList();
    Consumer<Object> spy = o -> found.add(o);
    StreamSupport.stream(c).forEach(spy);
    assertTrue(found.isEmpty());
    assertTrue(c.add(x));
    StreamSupport.stream(c).forEach(spy);
    assertEquals(Collections.singletonList(x), found);
    found.clear();
    assertTrue(c.add(y));
    StreamSupport.stream(c).forEach(spy);
    assertEquals(2, found.size());
    assertTrue(found.contains(x));
    assertTrue(found.contains(y));
    found.clear();
    c.clear();
    StreamSupport.stream(c).forEach(spy);
    assertTrue(found.isEmpty());
}
Also used : Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) PriorityQueue(java.util.PriorityQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) Iterables(java8.lang.Iterables) Deque(java.util.Deque) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) HashSet(java.util.HashSet) Phaser(java8.util.concurrent.Phaser) Iterators(java8.util.Iterators) Future(java.util.concurrent.Future) J8Arrays(java8.util.J8Arrays) Vector(java.util.Vector) Consumer(java8.util.function.Consumer) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) BlockingDeque(java.util.concurrent.BlockingDeque) Collection(java.util.Collection) Spliterator(java8.util.Spliterator) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Spliterators(java8.util.Spliterators) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) StreamSupport(java8.util.stream.StreamSupport) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) Supplier(java8.util.function.Supplier) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HOURS(java.util.concurrent.TimeUnit.HOURS) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConcurrentModificationException(java.util.ConcurrentModificationException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Collection(java.util.Collection) Test(org.testng.annotations.Test)

Example 8 with Consumer

use of java8.util.function.Consumer in project streamsupport by stefan-zobel.

the class StreamSpliteratorTest method testIntSplitting.

// 
public void testIntSplitting() {
    List<Consumer<IntStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
    }), s -> s.reduce(java8.lang.Integers::sum));
    List<UnaryOperator<IntStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
    s -> s.map(i -> i).parallel());
    for (int i = 0; i < terminalOps.size(); i++) {
        setContext("termOpIndex", i);
        Consumer<IntStream> terminalOp = terminalOps.get(i);
        for (int j = 0; j < intermediateOps.size(); j++) {
            setContext("intOpIndex", j);
            UnaryOperator<IntStream> intermediateOp = intermediateOps.get(j);
            for (boolean proxyEstimateSize : new boolean[] { false, true }) {
                setContext("proxyEstimateSize", proxyEstimateSize);
                // Size is assumed to be larger than the target size for no splitting
                // @@@ Need way to obtain the target size
                Spliterator.OfInt sp = intermediateOp.apply(IntStreams.range(0, 1000)).spliterator();
                ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
                IntStream s = StreamSupport.intStream(psp, true);
                terminalOp.accept(s);
                Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
            }
        }
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LambdaTestHelpers.irDoubler(java8.util.stream.LambdaTestHelpers.irDoubler) Arrays(java.util.Arrays) IntConsumer(java8.util.function.IntConsumer) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) LongStreamTestDataProvider(java8.util.stream.LongStreamTestDataProvider) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test) LambdaTestHelpers.ipEven(java8.util.stream.LambdaTestHelpers.ipEven) DoubleStreamTestDataProvider(java8.util.stream.DoubleStreamTestDataProvider) TestData(java8.util.stream.TestData) LambdaTestHelpers.lpEven(java8.util.stream.LambdaTestHelpers.lpEven) LambdaTestHelpers.dpEven(java8.util.stream.LambdaTestHelpers.dpEven) Function(java8.util.function.Function) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Assert(org.testng.Assert) Consumer(java8.util.function.Consumer) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LambdaTestHelpers.pEven(java8.util.stream.LambdaTestHelpers.pEven) DoubleStreams(java8.util.stream.DoubleStreams) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) SpliteratorTestHelper(java8.util.stream.SpliteratorTestHelper) Spliterators(java8.util.Spliterators) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) LambdaTestHelpers.permuteStreamFunctions(java8.util.stream.LambdaTestHelpers.permuteStreamFunctions) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) DoubleConsumer(java8.util.function.DoubleConsumer) Stream(java8.util.stream.Stream) LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) UnaryOperator(java8.util.function.UnaryOperator) IntStream(java8.util.stream.IntStream) Spliterator(java8.util.Spliterator)

Example 9 with Consumer

use of java8.util.function.Consumer in project streamsupport by stefan-zobel.

the class StreamSpliteratorTest method testLongSplitting.

// 
public void testLongSplitting() {
    List<Consumer<LongStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
    }), s -> s.reduce(java8.lang.Longs::sum));
    List<UnaryOperator<LongStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
    s -> s.map(i -> i).parallel());
    for (int i = 0; i < terminalOps.size(); i++) {
        Consumer<LongStream> terminalOp = terminalOps.get(i);
        setContext("termOpIndex", i);
        for (int j = 0; j < intermediateOps.size(); j++) {
            setContext("intOpIndex", j);
            UnaryOperator<LongStream> intermediateOp = intermediateOps.get(j);
            for (boolean proxyEstimateSize : new boolean[] { false, true }) {
                setContext("proxyEstimateSize", proxyEstimateSize);
                // Size is assumed to be larger than the target size for no splitting
                // @@@ Need way to obtain the target size
                Spliterator.OfLong sp = intermediateOp.apply(LongStreams.range(0, 1000)).spliterator();
                ProxyNoExactSizeSpliterator.OfLong psp = new ProxyNoExactSizeSpliterator.OfLong(sp, proxyEstimateSize);
                LongStream s = StreamSupport.longStream(psp, true);
                terminalOp.accept(s);
                Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
            }
        }
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LambdaTestHelpers.irDoubler(java8.util.stream.LambdaTestHelpers.irDoubler) Arrays(java.util.Arrays) IntConsumer(java8.util.function.IntConsumer) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) LongStreamTestDataProvider(java8.util.stream.LongStreamTestDataProvider) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test) LambdaTestHelpers.ipEven(java8.util.stream.LambdaTestHelpers.ipEven) DoubleStreamTestDataProvider(java8.util.stream.DoubleStreamTestDataProvider) TestData(java8.util.stream.TestData) LambdaTestHelpers.lpEven(java8.util.stream.LambdaTestHelpers.lpEven) LambdaTestHelpers.dpEven(java8.util.stream.LambdaTestHelpers.dpEven) Function(java8.util.function.Function) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Assert(org.testng.Assert) Consumer(java8.util.function.Consumer) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LambdaTestHelpers.pEven(java8.util.stream.LambdaTestHelpers.pEven) DoubleStreams(java8.util.stream.DoubleStreams) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) SpliteratorTestHelper(java8.util.stream.SpliteratorTestHelper) Spliterators(java8.util.Spliterators) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) LambdaTestHelpers.permuteStreamFunctions(java8.util.stream.LambdaTestHelpers.permuteStreamFunctions) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) DoubleConsumer(java8.util.function.DoubleConsumer) Stream(java8.util.stream.Stream) LongStream(java8.util.stream.LongStream) LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) UnaryOperator(java8.util.function.UnaryOperator) Spliterator(java8.util.Spliterator)

Example 10 with Consumer

use of java8.util.function.Consumer in project streamsupport by stefan-zobel.

the class WhileOpStatefulTest method testWhileMulti.

private void testWhileMulti(Map<String, Supplier<Stream<Integer>>> sources, Consumer<Stream<Integer>> mRef, Consumer<IntStream> mInt, Consumer<LongStream> mLong, Consumer<DoubleStream> mDouble) {
    Map<String, Function<Stream<Integer>, Stream<Integer>>> transforms = new HashMap<>();
    transforms.put("Stream.sequential()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    transforms.put("Stream.parallel()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.parallel().peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    Map<String, Consumer<Stream<Integer>>> actions = new HashMap<>();
    actions.put("Ref", mRef);
    actions.put("Int", s -> mInt.accept(s.mapToInt(e -> e)));
    actions.put("Long", s -> mLong.accept(s.mapToLong(e -> e)));
    actions.put("Double", s -> mDouble.accept(s.mapToDouble(e -> e)));
    actions.put("Ref using defaults", s -> mRef.accept(DefaultMethodStreams.delegateTo(s)));
    actions.put("Int using defaults", s -> mInt.accept(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))));
    actions.put("Long using defaults", s -> mLong.accept(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))));
    actions.put("Double using defaults", s -> mDouble.accept(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))));
    for (Map.Entry<String, Supplier<Stream<Integer>>> s : sources.entrySet()) {
        setContext("source", s.getKey());
        for (Map.Entry<String, Function<Stream<Integer>, Stream<Integer>>> t : transforms.entrySet()) {
            setContext("transform", t.getKey());
            for (Map.Entry<String, Consumer<Stream<Integer>>> a : actions.entrySet()) {
                setContext("shape", a.getKey());
                Stream<Integer> stream = s.getValue().get();
                stream = t.getValue().apply(stream);
                a.getValue().accept(stream);
            }
        }
    }
}
Also used : Collector(java8.util.stream.Collector) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) Function(java8.util.function.Function) Supplier(java8.util.function.Supplier) IntStreams(java8.util.stream.IntStreams) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) RefStreams(java8.util.stream.RefStreams) Collectors.toCollection(java8.util.stream.Collectors.toCollection) Consumer(java8.util.function.Consumer) Map(java.util.Map) DefaultMethodStreams(java8.util.stream.DefaultMethodStreams) BooleanSupplier(java8.util.function.BooleanSupplier) DoubleStream(java8.util.stream.DoubleStream) LinkedList(java.util.LinkedList) Stream(java8.util.stream.Stream) HashMap(java.util.HashMap) Function(java8.util.function.Function) Consumer(java8.util.function.Consumer) Supplier(java8.util.function.Supplier) BooleanSupplier(java8.util.function.BooleanSupplier) BooleanSupplier(java8.util.function.BooleanSupplier) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Consumer (java8.util.function.Consumer)11 List (java.util.List)10 StreamSupport (java8.util.stream.StreamSupport)10 Test (org.testng.annotations.Test)10 Arrays (java.util.Arrays)9 Spliterator (java8.util.Spliterator)9 Spliterators (java8.util.Spliterators)9 Iterator (java.util.Iterator)7 LinkedList (java.util.LinkedList)7 NoSuchElementException (java.util.NoSuchElementException)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 ArrayDeque (java.util.ArrayDeque)6 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Collections (java.util.Collections)6 ConcurrentModificationException (java.util.ConcurrentModificationException)6 Deque (java.util.Deque)6 HashSet (java.util.HashSet)6 PriorityQueue (java.util.PriorityQueue)6 Queue (java.util.Queue)6