< prev index next >

test/jdk/java/lang/ScopedValue/ManyBindings.java

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @summary Stress test ScopedValue with many bindings and rebinings
 27  * @modules jdk.incubator.concurrent
 28  * @library /test/lib
 29  * @key randomness
 30  * @run junit ManyBindings
 31  */
 32 
 33 import jdk.incubator.concurrent.ScopedValue;
 34 import jdk.incubator.concurrent.ScopedValue.Carrier;
 35 import java.util.Arrays;
 36 import java.util.Objects;
 37 import java.util.Random;
 38 
 39 import jdk.test.lib.RandomFactory;
 40 import jdk.test.lib.thread.VThreadRunner;
 41 
 42 import org.junit.jupiter.api.Test;
 43 import static org.junit.jupiter.api.Assertions.*;
 44 
 45 class ManyBindings {
 46     private static final Random RND = RandomFactory.getRandom();
 47 
 48     // number of scoped values to create
 49     private static final int SCOPED_VALUE_COUNT = 16;
 50 
 51     // recursive depth to test
 52     private static final int MAX_DEPTH = 24;
 53 
 54     /**

112         KeyAndValue<Integer>[] newArray = Arrays.copyOf(array, len);
113         int n = Math.max(1, RND.nextInt(len / 2));
114         while (n > 0) {
115             int index = RND.nextInt(len);
116             ScopedValue<Integer> key = array[index].key;
117             int newValue = RND.nextInt();
118             if (carrier == null) {
119                 carrier = ScopedValue.where(key, newValue);
120             } else {
121                 carrier = carrier.where(key, newValue);
122             }
123             newArray[index] = new KeyAndValue<>(key, newValue);
124             n--;
125         }
126 
127         // invoke recursively
128         carrier.run(() -> {
129             test(newArray, depth+1);
130         });
131 
132         // check that the scoped values have the origina values
133         check(array);
134     }
135 
136     /**
137      * Check that the given scoped values have the expected value.
138      */
139     private void check(KeyAndValue<Integer>[] array) {
140         for (int i = 0; i < array.length; i++) {
141             ScopedValue<Integer> key = array[i].key;
142             Integer value = array[i].value;
143             if (value == null) {
144                 assertFalse(key.isBound());
145             } else {
146                 assertEquals(value, key.get());
147             }
148         }
149     }
150 
151     /**
152      * Do lots of reads of the scoped values, to pollute the SV cache.

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @summary Stress test ScopedValue with many bindings and rebindings
 27  * @enablePreview
 28  * @library /test/lib
 29  * @key randomness
 30  * @run junit ManyBindings
 31  */
 32 
 33 import java.lang.ScopedValue.Carrier;

 34 import java.util.Arrays;
 35 import java.util.Objects;
 36 import java.util.Random;
 37 
 38 import jdk.test.lib.RandomFactory;
 39 import jdk.test.lib.thread.VThreadRunner;
 40 
 41 import org.junit.jupiter.api.Test;
 42 import static org.junit.jupiter.api.Assertions.*;
 43 
 44 class ManyBindings {
 45     private static final Random RND = RandomFactory.getRandom();
 46 
 47     // number of scoped values to create
 48     private static final int SCOPED_VALUE_COUNT = 16;
 49 
 50     // recursive depth to test
 51     private static final int MAX_DEPTH = 24;
 52 
 53     /**

111         KeyAndValue<Integer>[] newArray = Arrays.copyOf(array, len);
112         int n = Math.max(1, RND.nextInt(len / 2));
113         while (n > 0) {
114             int index = RND.nextInt(len);
115             ScopedValue<Integer> key = array[index].key;
116             int newValue = RND.nextInt();
117             if (carrier == null) {
118                 carrier = ScopedValue.where(key, newValue);
119             } else {
120                 carrier = carrier.where(key, newValue);
121             }
122             newArray[index] = new KeyAndValue<>(key, newValue);
123             n--;
124         }
125 
126         // invoke recursively
127         carrier.run(() -> {
128             test(newArray, depth+1);
129         });
130 
131         // check that the scoped values have the original values
132         check(array);
133     }
134 
135     /**
136      * Check that the given scoped values have the expected value.
137      */
138     private void check(KeyAndValue<Integer>[] array) {
139         for (int i = 0; i < array.length; i++) {
140             ScopedValue<Integer> key = array[i].key;
141             Integer value = array[i].value;
142             if (value == null) {
143                 assertFalse(key.isBound());
144             } else {
145                 assertEquals(value, key.get());
146             }
147         }
148     }
149 
150     /**
151      * Do lots of reads of the scoped values, to pollute the SV cache.
< prev index next >