1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary Tests stream execution in a custom ForkJoinPool
  29  * @bug 8190974
  30  * @run testng/othervm CustomFJPoolTest
  31  * @run testng/othervm -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 CustomFJPoolTest
  32  */
  33 
  34 import org.testng.annotations.Test;
  35 
  36 import java.util.Comparator;
  37 import java.util.Spliterator;
  38 import java.util.concurrent.ForkJoinPool;
  39 import java.util.concurrent.atomic.AtomicInteger;
  40 import java.util.function.Consumer;
  41 import java.util.stream.IntStream;
  42 import java.util.stream.StreamSupport;
  43 
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertTrue;
  46 
  47 @Test
  48 public class CustomFJPoolTest {
  49 
  50     // A Spliterator that counts the number of spliterators created
  51     // including itself, thus the count starts at 1
  52     static class SplitCountingSpliterator<T> implements Spliterator<T> {
  53         final Spliterator<T> s;
  54         final AtomicInteger nsplits;
  55 
  56         // Top-level constructor
  57         public SplitCountingSpliterator(Spliterator<T> s) {
  58             this.s = s;
  59             nsplits = new AtomicInteger(1);
  60         }
  61 
  62         // Splitting constructor
  63         SplitCountingSpliterator(Spliterator<T> s, AtomicInteger nsplits) {
  64             this.s = s;
  65             this.nsplits = nsplits;
  66         }
  67 
  68         int splits() {
  69             return nsplits.get();
  70         }
  71 
  72         @Override
  73 
  74         public boolean tryAdvance(Consumer<? super T> action) {
  75             return s.tryAdvance(action);
  76         }
  77 
  78         @Override
  79         public void forEachRemaining(Consumer<? super T> action) {
  80             s.forEachRemaining(action);
  81         }
  82 
  83         @Override
  84         public Spliterator<T> trySplit() {
  85             var split = s.trySplit();
  86             if (split != null) {
  87                 nsplits.incrementAndGet();
  88                 return new SplitCountingSpliterator<>(split, nsplits);
  89             }
  90             else {
  91                 return null;
  92             }
  93         }
  94 
  95         @Override
  96         public long estimateSize() {
  97             return s.estimateSize();
  98         }
  99 
 100         @Override
 101         public long getExactSizeIfKnown() {
 102             return s.getExactSizeIfKnown();
 103         }
 104 
 105         @Override
 106         public int characteristics() {
 107             return s.characteristics();
 108         }
 109 
 110         @Override
 111         public boolean hasCharacteristics(int characteristics) {
 112             return s.hasCharacteristics(characteristics);
 113         }
 114 
 115         @Override
 116         public Comparator<? super T> getComparator() {
 117             return s.getComparator();
 118         }
 119     }
 120 
 121     public void testCustomPools() throws Exception {
 122         int splitsForP1 = countSplits(new ForkJoinPool(1));
 123         int splitsForP2 = countSplits(new ForkJoinPool(2));
 124         assertEquals(splitsForP2, splitsForP1 * 2);
 125 
 126         int commonParallelism = ForkJoinPool.getCommonPoolParallelism();
 127         if (commonParallelism > 1 && commonParallelism < 128) {
 128             int splitsForPHalfC = countSplits(new ForkJoinPool(commonParallelism / 2));
 129             int splitsForPC = countSplits(ForkJoinPool.commonPool());
 130 
 131             assertTrue(splitsForPHalfC < splitsForPC);
 132             assertEquals(splitsForPC / splitsForPHalfC,
 133                          nearestPowerOfTwo(commonParallelism) / nearestPowerOfTwo(commonParallelism / 2));
 134         }
 135     }
 136 
 137     static int countSplits(ForkJoinPool fjp) throws Exception {
 138         // The number of splits will be equivalent to the number of leaf nodes
 139         // and will be a power of 2
 140         var fInteger = fjp.submit(() -> {
 141             var s = IntStream.range(0, 1024).boxed().parallel().spliterator();
 142             var cs = new SplitCountingSpliterator<>(s);
 143             StreamSupport.stream(cs, true).forEach(e -> {});
 144             return cs.splits();
 145         });
 146         return fInteger.get();
 147     }
 148 
 149     static int nearestPowerOfTwo(int i) {
 150         return (i & (i - 1)) == 0
 151                ? i
 152                : 1 << (32 - Integer.numberOfLeadingZeros(i));
 153     }
 154 }