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 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     /**
 54      * Stress test bindings on platform thread.
 55      */
 56     @Test
 57     void testPlatformThread() {
 58         test();
 59     }
 60 
 61     /**
 62      * Stress test bindings on virtual thread.
 63      */
 64     @Test
 65     void testVirtualThread() throws Exception {
 66         VThreadRunner.run(() -> test());
 67     }
 68 
 69     /**
 70      * Scoped value and its expected value (or null if not bound).
 71      */
 72     record KeyAndValue<T>(ScopedValue<T> key, T value) {
 73         KeyAndValue() {
 74             this(ScopedValue.newInstance(), null);
 75         }
 76     }
 77 
 78     /**
 79      * Stress test bindings on current thread.
 80      */
 81     private void test() {
 82         KeyAndValue<Integer>[] array = new KeyAndValue[SCOPED_VALUE_COUNT];
 83         for (int i = 0; i < array.length; i++) {
 84             array[i] = new KeyAndValue<>();
 85         }
 86         test(array, 1);
 87     }
 88 
 89     /**
 90      * Test that the scoped values in the array have the expected value, then
 91      * recursively call this method with some of the scoped values bound to a
 92      * new value.
 93      *
 94      * @param array the scoped values and their expected value
 95      * @param depth current recurive depth
 96      */
 97     private void test(KeyAndValue<Integer>[] array, int depth) {
 98         if (depth > MAX_DEPTH)
 99             return;
100 
101         // check that the scoped values have the expected values
102         check(array);
103 
104         // try to pollute the cache
105         lotsOfReads(array);
106 
107         // create a Carrier to bind/rebind some of the scoped values
108         int len = array.length;
109         Carrier carrier = null;
110 
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.
152      */
153     private void lotsOfReads(KeyAndValue<Integer>[] array) {
154         for (int k = 0; k < 1000; k++) {
155             int index = RND.nextInt(array.length);
156             Integer value = array[index].value;
157             if (value != null) {
158                 ScopedValue<Integer> key = array[index].key;
159                 assertEquals(value, key.get());
160             }
161         }
162     }
163 }