4242
4343import static org .junit .Assert .assertNotNull ;
4444
45+ import java .io .IOException ;
4546import java .lang .ref .WeakReference ;
4647import java .util .List ;
4748import java .util .concurrent .Semaphore ;
4849
50+ import com .oracle .truffle .api .test .SubprocessTestUtils ;
51+ import org .graalvm .nativeimage .ImageInfo ;
4952import org .junit .Test ;
5053
5154import com .oracle .truffle .api .CompilerDirectives ;
7376public class WeakCachedTest extends AbstractPolyglotTest {
7477
7578 @ Test
76- public void testWeakSimpleNode () {
77- WeakSimpleNode node = WeakSimpleNodeGen .create ();
78- Object o = new String ("" );
79- WeakReference <Object > ref = new WeakReference <>(o );
80- node .execute (o );
81- o = null ;
82- GCUtils .assertGc ("Reference is not collected" , ref );
79+ public void testWeakSimpleNode () throws IOException , InterruptedException {
80+ runInSubprocess (() -> {
81+ WeakSimpleNode node = WeakSimpleNodeGen .create ();
82+ Object o = new String ("" );
83+ WeakReference <Object > ref = new WeakReference <>(o );
84+ node .execute (o );
85+ o = null ;
86+ GCUtils .assertGc ("Reference is not collected" , ref );
87+ });
88+ }
89+
90+ private static void runInSubprocess (Runnable runnable ) throws IOException , InterruptedException {
91+ if (ImageInfo .inImageCode ()) {
92+ runnable .run ();
93+ } else {
94+ SubprocessTestUtils .newBuilder (WeakCachedTest .class , runnable ).run ();
95+ }
8396 }
8497
8598 @ GenerateUncached
@@ -96,26 +109,28 @@ Object s0(String arg,
96109 }
97110
98111 @ Test
99- public void testWeakInlineCache () {
100- WeakInlineCacheNode node = WeakInlineCacheNodeGen .create ();
101- Object o0 = new String ("" );
102- Object o1 = new String ("" );
103- Object o2 = new String ("" );
104- WeakReference <Object > ref0 = new WeakReference <>(o0 );
105- WeakReference <Object > ref1 = new WeakReference <>(o1 );
106- WeakReference <Object > ref2 = new WeakReference <>(o2 );
107- node .execute (o0 );
108- node .execute (o1 );
109- o0 = null ;
110- GCUtils .assertGc ("Reference is not collected" , ref0 );
111-
112- node .execute (o1 );
113- node .execute (o2 );
114- o1 = null ;
115- o2 = null ;
116- GCUtils .assertGc ("Reference is not collected" , List .of (ref1 , ref2 ));
117-
118- assertFails (() -> node .execute (new String ("" )), UnsupportedSpecializationException .class );
112+ public void testWeakInlineCache () throws IOException , InterruptedException {
113+ runInSubprocess (() -> {
114+ WeakInlineCacheNode node = WeakInlineCacheNodeGen .create ();
115+ Object o0 = new String ("" );
116+ Object o1 = new String ("" );
117+ Object o2 = new String ("" );
118+ WeakReference <Object > ref0 = new WeakReference <>(o0 );
119+ WeakReference <Object > ref1 = new WeakReference <>(o1 );
120+ WeakReference <Object > ref2 = new WeakReference <>(o2 );
121+ node .execute (o0 );
122+ node .execute (o1 );
123+ o0 = null ;
124+ GCUtils .assertGc ("Reference is not collected" , ref0 );
125+
126+ node .execute (o1 );
127+ node .execute (o2 );
128+ o1 = null ;
129+ o2 = null ;
130+ GCUtils .assertGc ("Reference is not collected" , List .of (ref1 , ref2 ));
131+
132+ assertFails (() -> node .execute (new String ("" )), UnsupportedSpecializationException .class );
133+ });
119134 }
120135
121136 @ GenerateUncached
@@ -133,26 +148,28 @@ Object s0(String arg,
133148 }
134149
135150 @ Test
136- public void testWeakCachedLibrary () {
137- WeakCachedLibraryNode node = adoptNode (WeakCachedLibraryNodeGen .create ()).get ();
138- Object o0 = new String ("" );
139- Object o1 = new String ("" );
140- Object o2 = new String ("" );
141- WeakReference <Object > ref0 = new WeakReference <>(o0 );
142- WeakReference <Object > ref1 = new WeakReference <>(o1 );
143- WeakReference <Object > ref2 = new WeakReference <>(o2 );
144- node .execute (o0 );
145- node .execute (o1 );
146- o0 = null ;
147- GCUtils .assertGc ("Reference is not collected" , ref0 );
148-
149- node .execute (o1 );
150- node .execute (o2 );
151- o1 = null ;
152- o2 = null ;
153- GCUtils .assertGc ("Reference is not collected" , List .of (ref1 , ref2 ));
154-
155- assertFails (() -> node .execute (new String ("" )), UnsupportedSpecializationException .class );
151+ public void testWeakCachedLibrary () throws InterruptedException , IOException {
152+ runInSubprocess (() -> {
153+ WeakCachedLibraryNode node = adoptNode (WeakCachedLibraryNodeGen .create ()).get ();
154+ Object o0 = new String ("" );
155+ Object o1 = new String ("" );
156+ Object o2 = new String ("" );
157+ WeakReference <Object > ref0 = new WeakReference <>(o0 );
158+ WeakReference <Object > ref1 = new WeakReference <>(o1 );
159+ WeakReference <Object > ref2 = new WeakReference <>(o2 );
160+ node .execute (o0 );
161+ node .execute (o1 );
162+ o0 = null ;
163+ GCUtils .assertGc ("Reference is not collected" , ref0 );
164+
165+ node .execute (o1 );
166+ node .execute (o2 );
167+ o1 = null ;
168+ o2 = null ;
169+ GCUtils .assertGc ("Reference is not collected" , List .of (ref1 , ref2 ));
170+
171+ assertFails (() -> node .execute (new String ("" )), UnsupportedSpecializationException .class );
172+ });
156173 }
157174
158175 @ GenerateUncached
@@ -181,14 +198,16 @@ Object s0replace(String arg) {
181198 }
182199
183200 @ Test
184- public void testWeakSharedNode () {
185- WeakSharedCacheNode node = WeakSharedCacheNodeGen .create ();
186- Object o0 = new String ("" );
187- WeakReference <Object > ref1 = new WeakReference <>(o0 );
188- node .execute (o0 , false );
189- o0 = null ;
190- GCUtils .assertGc ("Reference is not collected" , ref1 );
191- node .execute ("" , false );
201+ public void testWeakSharedNode () throws IOException , InterruptedException {
202+ runInSubprocess (() -> {
203+ WeakSharedCacheNode node = WeakSharedCacheNodeGen .create ();
204+ Object o0 = new String ("" );
205+ WeakReference <Object > ref1 = new WeakReference <>(o0 );
206+ node .execute (o0 , false );
207+ o0 = null ;
208+ GCUtils .assertGc ("Reference is not collected" , ref1 );
209+ node .execute ("" , false );
210+ });
192211 }
193212
194213 @ GenerateUncached
@@ -217,28 +236,33 @@ Object s1(String arg, boolean expectNull,
217236 * reference cannot get collected.
218237 */
219238 @ Test
220- public void testConsistentGuardAndSpecialization () throws InterruptedException {
221- ConsistentGuardAndSpecializationNode node = ConsistentGuardAndSpecializationNodeGen .create ();
222- Object o0 = new String ("" );
223- WeakReference <Object > ref1 = new WeakReference <>(o0 );
224- node .execute (o0 );
225- Thread t = new Thread (new Runnable () {
226- public void run () {
227- node .locksEnabled = true ;
228- node .execute (new String ("" ));
239+ public void testConsistentGuardAndSpecialization () throws IOException , InterruptedException {
240+ runInSubprocess (() -> {
241+ ConsistentGuardAndSpecializationNode node = ConsistentGuardAndSpecializationNodeGen .create ();
242+ Object o0 = new String ("" );
243+ WeakReference <Object > ref1 = new WeakReference <>(o0 );
244+ node .execute (o0 );
245+ Thread t = new Thread (new Runnable () {
246+ public void run () {
247+ node .locksEnabled = true ;
248+ node .execute (new String ("" ));
249+ }
250+ });
251+ t .start ();
252+ try {
253+ node .waitForGuard .acquire ();
254+ o0 = null ;
255+ try {
256+ GCUtils .assertNotGc ("Reference is not collected" , ref1 );
257+ } finally {
258+ node .waitForSpecialization .release ();
259+ t .join ();
260+ }
261+ } catch (InterruptedException ie ) {
262+ throw new AssertionError (ie );
229263 }
264+ GCUtils .assertGc ("Reference is not collected" , ref1 );
230265 });
231- t .start ();
232- node .waitForGuard .acquire ();
233- o0 = null ;
234- try {
235- GCUtils .assertNotGc ("Reference is not collected" , ref1 );
236- } finally {
237- node .waitForSpecialization .release ();
238- t .join ();
239- }
240- GCUtils .assertGc ("Reference is not collected" , ref1 );
241-
242266 }
243267
244268 @ Test
0 commit comments