1 /*
  2  * Copyright (c) 2019, 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 Test Virtual threads using thread locals
 27  * @library /test/lib
 28  * @enablePreview
 29  * @run junit ThreadLocals
 30  */
 31 
 32 import jdk.test.lib.thread.VThreadRunner;
 33 import org.junit.jupiter.api.Test;
 34 import static org.junit.jupiter.api.Assertions.*;
 35 
 36 class ThreadLocals {
 37     static final ThreadLocal<Object> LOCAL = new ThreadLocal<>();
 38     static final ThreadLocal<Object> INHERITED_LOCAL = new InheritableThreadLocal<>();
 39 
 40     /**
 41      * Basic test of thread local set/get.
 42      */
 43     @Test
 44     void testThreadLocal1() throws Exception {
 45         for (int i = 0; i < 10; i++) {
 46             VThreadRunner.run(() -> {
 47                 assertNull(LOCAL.get());
 48                 Object obj = new Object();
 49                 LOCAL.set(obj);
 50                 assertTrue(LOCAL.get() == obj);
 51             });
 52         }
 53     }
 54 
 55     /**
 56      * Test setting thread local before blocking operation.
 57      */
 58     @Test
 59     void testThreadLocal2() throws Exception {
 60         VThreadRunner.run(() -> {
 61             assertNull(LOCAL.get());
 62             Object obj = new Object();
 63             LOCAL.set(obj);
 64             try { Thread.sleep(100); } catch (InterruptedException e) { }
 65             assertTrue(LOCAL.get() == obj);
 66         });
 67     }
 68 
 69     /**
 70      * Basic test of inheritable thread local set/get, no initial value inherited.
 71      */
 72     @Test
 73     void testInheritedThreadLocal1() throws Exception {
 74         assertNull(INHERITED_LOCAL.get());
 75         for (int i = 0; i < 10; i++) {
 76             VThreadRunner.run(() -> {
 77                 assertNull(INHERITED_LOCAL.get());
 78                 Object obj = new Object();
 79                 INHERITED_LOCAL.set(obj);
 80                 assertTrue(INHERITED_LOCAL.get() == obj);
 81             });
 82         }
 83         assertNull(INHERITED_LOCAL.get());
 84     }
 85 
 86     /**
 87      * Test inheriting initial value of InheritableThreadLocal from platform thread.
 88      */
 89     @Test
 90     void testInheritedThreadLocal2() throws Exception {
 91         assertNull(INHERITED_LOCAL.get());
 92         var obj = new Object();
 93         INHERITED_LOCAL.set(obj);
 94         try {
 95             VThreadRunner.run(() -> {
 96                 assertTrue(INHERITED_LOCAL.get() == obj);
 97             });
 98         } finally {
 99             INHERITED_LOCAL.remove();
100         }
101     }
102 
103     /**
104      * Test inheriting initial value of InheritableThreadLocal from virtual thread.
105      */
106     @Test
107     void testInheritedThreadLocal3() throws Exception {
108         assertNull(INHERITED_LOCAL.get());
109         VThreadRunner.run(() -> {
110             var obj = new Object();
111             INHERITED_LOCAL.set(obj);
112             VThreadRunner.run(() -> {
113                 assertTrue(INHERITED_LOCAL.get() == obj);
114             });
115             assertTrue(INHERITED_LOCAL.get() == obj);
116 
117         });
118         assertNull(INHERITED_LOCAL.get());
119     }
120 
121     /**
122      * Test Thread that does not inherit initial value of InheritableThreadLocals
123      * from platform thread.
124      */
125     @Test
126     void testInheritedThreadLocal4() throws Exception {
127         assertNull(INHERITED_LOCAL.get());
128         var obj = new Object();
129         INHERITED_LOCAL.set(obj);
130         try {
131             int characteristics = VThreadRunner.NO_INHERIT_THREAD_LOCALS;
132             VThreadRunner.run(characteristics, () -> {
133                 assertNull(INHERITED_LOCAL.get());
134             });
135         } finally {
136             INHERITED_LOCAL.remove();
137         }
138     }
139 
140     /**
141      * Test Thread that does not inherit initial value of InheritableThreadLocals
142      * from virtual thread.
143      */
144     @Test
145     void testInheritedThreadLocal5() throws Exception {
146         assertNull(INHERITED_LOCAL.get());
147         VThreadRunner.run(() -> {
148             var obj = new Object();
149             INHERITED_LOCAL.set(obj);
150             int characteristics = VThreadRunner.NO_INHERIT_THREAD_LOCALS;
151             VThreadRunner.run(characteristics, () -> {
152                 assertNull(INHERITED_LOCAL.get());
153             });
154             assertTrue(INHERITED_LOCAL.get() == obj);
155 
156         });
157         assertNull(INHERITED_LOCAL.get());
158     }
159 }