54
54
import java .util .regex .Matcher ;
55
55
import java .util .regex .Pattern ;
56
56
57
- import com .oracle .truffle .api .test .SubprocessTestUtils ;
58
57
import org .graalvm .nativeimage .ImageInfo ;
59
58
import org .graalvm .options .OptionCategory ;
60
59
import org .graalvm .options .OptionDescriptors ;
76
75
import com .oracle .truffle .api .frame .VirtualFrame ;
77
76
import com .oracle .truffle .api .nodes .RootNode ;
78
77
import com .oracle .truffle .api .test .GCUtils ;
78
+ import com .oracle .truffle .api .test .SubprocessTestUtils ;
79
79
import com .oracle .truffle .api .test .common .TestUtils ;
80
80
import com .oracle .truffle .tck .tests .TruffleTestAssumptions ;
81
81
@@ -97,33 +97,42 @@ public void testTraceSourceCacheFailure() throws Throwable {
97
97
}
98
98
99
99
@ Test
100
- public void testTraceSourceCacheEviction () throws IOException {
100
+ public void testTraceSourceCacheEviction () throws IOException , InterruptedException {
101
101
TruffleTestAssumptions .assumeWeakEncapsulation (); // Can't control GC in the isolate.
102
- try (ByteArrayOutputStream out = new ByteArrayOutputStream (); Context context = Context .newBuilder ().option ("engine.TraceSourceCache" , "true" ).out (out ).err (out ).build ()) {
103
- Source auxiliarySource = Source .newBuilder (SourceCacheTestLanguage .ID , "x" , "AuxiliarySource" ).build ();
104
- String sourceName = "TestSource" ;
105
- String [] sourceHash = new String [1 ];
106
- for (int i = 0 ; i < 2 ; i ++) {
107
- Source source = Source .newBuilder (SourceCacheTestLanguage .ID , "" , sourceName ).build ();
108
- int sourceHashCode = context .eval (source ).asInt ();
109
- sourceHash [0 ] = String .format ("0x%08x" , sourceHashCode );
110
- WeakReference <Source > souceRef = new WeakReference <>(source );
111
- source = null ;
112
- GCUtils .assertGc ("Source was not collected" , souceRef );
113
- context .eval (auxiliarySource );
114
- }
115
- List <String > logs = new ArrayList <>();
116
- forEachLog (out .toByteArray (), (matcher ) -> {
117
- String logType = matcher .group (1 );
118
- if ("evict" .equals (logType )) {
119
- logs .add (logType );
120
- Assert .assertEquals (sourceHash [0 ], matcher .group (2 ));
121
- Assert .assertEquals (sourceName , matcher .group (3 ));
102
+ Runnable runnable = () -> {
103
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream (); Context context = Context .newBuilder ().option ("engine.TraceSourceCache" , "true" ).out (out ).err (out ).build ()) {
104
+ Source auxiliarySource = Source .newBuilder (SourceCacheTestLanguage .ID , "x" , "AuxiliarySource" ).build ();
105
+ String sourceName = "TestSource" ;
106
+ String [] sourceHash = new String [1 ];
107
+ for (int i = 0 ; i < 2 ; i ++) {
108
+ Source source = Source .newBuilder (SourceCacheTestLanguage .ID , "" , sourceName ).build ();
109
+ int sourceHashCode = context .eval (source ).asInt ();
110
+ sourceHash [0 ] = String .format ("0x%08x" , sourceHashCode );
111
+ WeakReference <Source > souceRef = new WeakReference <>(source );
112
+ source = null ;
113
+ GCUtils .assertGc ("Source was not collected" , souceRef );
114
+ context .eval (auxiliarySource );
122
115
}
123
- });
124
- // at least one
125
- Assert .assertFalse (logs .isEmpty ());
126
- Assert .assertEquals ("evict" , logs .get (1 ));
116
+ List <String > logs = new ArrayList <>();
117
+ forEachLog (out .toByteArray (), (matcher ) -> {
118
+ String logType = matcher .group (1 );
119
+ if ("evict" .equals (logType )) {
120
+ logs .add (logType );
121
+ Assert .assertEquals (sourceHash [0 ], matcher .group (2 ));
122
+ Assert .assertEquals (sourceName , matcher .group (3 ));
123
+ }
124
+ });
125
+ // at least one
126
+ Assert .assertFalse (logs .isEmpty ());
127
+ Assert .assertEquals ("evict" , logs .get (1 ));
128
+ } catch (IOException ioe ) {
129
+ throw new AssertionError (ioe );
130
+ }
131
+ };
132
+ if (ImageInfo .inImageCode ()) {
133
+ runnable .run ();
134
+ } else {
135
+ SubprocessTestUtils .newBuilder (SourceCacheTest .class , runnable ).run ();
127
136
}
128
137
}
129
138
0 commit comments