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 * Test Thread that cannot set values for its copy of thread-locals.
71 */
72 @Test
73 void testThreadLocal3() throws Exception {
74 Object INITIAL_VALUE = new Object();
75 ThreadLocal<Object> LOCAL2 = new ThreadLocal<>() {
76 @Override
77 protected Object initialValue() {
78 return INITIAL_VALUE;
79 }
80 };
81 ThreadLocal<Object> INHERITED_LOCAL2 = new InheritableThreadLocal<>() {
82 @Override
83 protected Object initialValue() {
84 return INITIAL_VALUE;
85 }
86 };
87
88 VThreadRunner.run(VThreadRunner.NO_THREAD_LOCALS, () -> {
89 assertThrows(UnsupportedOperationException.class, () -> LOCAL.set(null));
90 assertThrows(UnsupportedOperationException.class, () -> LOCAL.set(new Object()));
91 assertNull(LOCAL.get());
92 LOCAL.remove(); // should not throw
93
94 assertThrows(UnsupportedOperationException.class, () -> LOCAL2.set(null));
95 assertThrows(UnsupportedOperationException.class, () -> LOCAL2.set(new Object()));
96 assertTrue(LOCAL2.get() == INITIAL_VALUE);
97 LOCAL2.remove(); // should not throw
98
99 assertThrows(UnsupportedOperationException.class, () -> INHERITED_LOCAL.set(null));
100 assertThrows(UnsupportedOperationException.class, () -> INHERITED_LOCAL.set(new Object()));
101 assertNull(INHERITED_LOCAL.get());
102 INHERITED_LOCAL.remove(); // should not throw
103
104 assertThrows(UnsupportedOperationException.class, () -> INHERITED_LOCAL2.set(null));
105 assertThrows(UnsupportedOperationException.class, () -> INHERITED_LOCAL2.set(new Object()));
106 assertTrue(INHERITED_LOCAL2.get() == INITIAL_VALUE);
107 INHERITED_LOCAL2.remove(); // should not throw
108 });
109 }
110
111 /**
112 * Basic test of inheritable thread local set/get, no initial value inherited.
113 */
114 @Test
115 void testInheritedThreadLocal1() throws Exception {
116 assertNull(INHERITED_LOCAL.get());
117 for (int i = 0; i < 10; i++) {
118 VThreadRunner.run(() -> {
119 assertNull(INHERITED_LOCAL.get());
120 Object obj = new Object();
121 INHERITED_LOCAL.set(obj);
122 assertTrue(INHERITED_LOCAL.get() == obj);
123 });
124 }
125 assertNull(INHERITED_LOCAL.get());
126 }
127
128 /**
129 * Test inheriting initial value of InheritableThreadLocal from platform thread.
130 */
|
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 */
|