1 /*
  2  * Copyright (c) 2022, 2023, 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  * @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     /**
 55      * Stress test bindings on platform thread.
 56      */
 57     @Test
 58     void testPlatformThread() {
 59         test();
 60     }
 61 
 62     /**
 63      * Stress test bindings on virtual thread.
 64      */
 65     @Test
 66     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(value, key.get());
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(value, key.get());
161             }
162         }
163     }
164 }