1 /*
  2  * Copyright (c) 2022, 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.
  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  * @enablePreview
 28  * @modules jdk.incubator.concurrent
 29  * @library /test/lib
 30  * @key randomness
 31  * @run testng ManyBindings
 32  */
 33 
 34 import jdk.incubator.concurrent.ScopedValue;
 35 import jdk.incubator.concurrent.ScopedValue.Carrier;
 36 import java.util.Arrays;
 37 import java.util.Objects;
 38 import java.util.Random;
 39 
 40 import jdk.test.lib.RandomFactory;
 41 import jdk.test.lib.thread.VThreadRunner;
 42 
 43 import org.testng.annotations.Test;
 44 import static org.testng.Assert.*;
 45 
 46 @Test
 47 public class ManyBindings {
 48     private static final Random RND = RandomFactory.getRandom();
 49 
 50     // number of scoped values to create
 51     private static final int SCOPED_VALUE_COUNT = 16;
 52 
 53     // recursive depth to test
 54     private static final int MAX_DEPTH = 24;
 55 
 56     /**
 57      * Stress test bindings on platform thread.
 58      */
 59     public void testPlatformThread() {
 60         test();
 61     }
 62 
 63     /**
 64      * Stress test bindings on virtual thread.
 65      */
 66     public void testVirtualThread() throws Exception {
 67         VThreadRunner.run(() -> test());
 68     }
 69 
 70     /**
 71      * Scoped value and its expected value (or null if not bound).
 72      */
 73     record KeyAndValue<T>(ScopedValue<T> key, T value) {
 74         KeyAndValue() {
 75             this(ScopedValue.newInstance(), null);
 76         }
 77     }
 78 
 79     /**
 80      * Stress test bindings on current thread.
 81      */
 82     private void test() {
 83         KeyAndValue<Integer>[] array = new KeyAndValue[SCOPED_VALUE_COUNT];
 84         for (int i = 0; i < array.length; i++) {
 85             array[i] = new KeyAndValue<>();
 86         }
 87         test(array, 1);
 88     }
 89 
 90     /**
 91      * Test that the scoped values in the array have the expected value, then
 92      * recursively call this method with some of the scoped values bound to a
 93      * new value.
 94      *
 95      * @param array the scoped values and their expected value
 96      * @param depth current recurive depth
 97      */
 98     private void test(KeyAndValue<Integer>[] array, int depth) {
 99         if (depth > MAX_DEPTH)
100             return;
101 
102         // check that the scoped values have the expected values
103         check(array);
104 
105         // try to pollute the cache
106         lotsOfReads(array);
107 
108         // create a Carrier to bind/rebind some of the scoped values
109         int len = array.length;
110         Carrier carrier = null;
111 
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(key.get(), value);
147             }
148         }
149     }
150 
151     /**
152      * Do lots of reads of the scoped values, to pollute the SV cache.
153      */
154     private void lotsOfReads(KeyAndValue<Integer>[] array) {
155         for (int k = 0; k < 1000; k++) {
156             int index = RND.nextInt(array.length);
157             Integer value = array[index].value;
158             if (value != null) {
159                 ScopedValue<Integer> key = array[index].key;
160                 assertEquals(key.get(), value);
161             }
162         }
163     }
164 }