@@ -98,6 +98,7 @@ public static void main(String... args) throws Exception {
9898 System .out .println ("Waiting for all processes to get started notification" );
9999 listener .started .acquire (PROCESS_COUNT );
100100
101+ System .out .println ("Terminating all processes" );
101102 for (JavaProcess javaProcess : javaProcesses ) {
102103 javaProcess .terminate ();
103104 }
@@ -138,7 +139,7 @@ public void disconnected(HostEvent arg0) {
138139 }
139140
140141 private void releaseStarted (Set <Integer > ids ) {
141- System .out .println ("realeaseStarted (" + ids + ")" );
142+ System .out .println ("releaseStarted (" + ids + ")" );
142143 for (Integer id : ids ) {
143144 releaseStarted (id );
144145 }
@@ -149,11 +150,12 @@ private void releaseStarted(Integer id) {
149150 if (hasMainArgs (id , jp .getMainArgsIdentifier ())) {
150151 // store id for terminated identification
151152 jp .setId (id );
152- System .out .println ("RELEASED (id=" + jp .getId () + ", args=" + jp .getMainArgsIdentifier () + ")" );
153+ System .out .println ("RELEASED started (id=" + jp .getId () + ", args=" + jp .getMainArgsIdentifier () + ")" );
153154 started .release ();
154155 return ;
155156 }
156157 }
158+ System .out .println ("releaseStarted: not a test pid: " + id );
157159 }
158160
159161 private void releaseTerminated (Set <Integer > ids ) {
@@ -166,23 +168,44 @@ private void releaseTerminated(Set<Integer> ids) {
166168 private void releaseTerminated (Integer id ) {
167169 for (JavaProcess jp : processes ) {
168170 if (id .equals (jp .getId ())) {
169- System .out .println ("RELEASED (id=" + jp .getId () + ", args=" + jp .getMainArgsIdentifier () + ")" );
171+ System .out .println ("RELEASED terminated (id=" + jp .getId () + ", args=" + jp .getMainArgsIdentifier () + ")" );
170172 terminated .release ();
171173 return ;
172174 }
173175 }
174176 }
175177
178+ private static final int ARGS_ATTEMPTS = 3 ;
179+
176180 private boolean hasMainArgs (Integer id , String args ) {
181+ VmIdentifier vmid = null ;
177182 try {
178- VmIdentifier vmid = new VmIdentifier ("//" + id .intValue ());
179- MonitoredVm target = host .getMonitoredVm (vmid );
180- String monitoredArgs = MonitoredVmUtil .mainArgs (target );
181- if (monitoredArgs != null && monitoredArgs .contains (args )) {
182- return true ;
183+ vmid = new VmIdentifier ("//" + id .intValue ());
184+ } catch (URISyntaxException e ) {
185+ System .out .println ("hasMainArgs(" + id + "): " + e );
186+ return false ;
187+ }
188+ // Retry a failing attempt to check arguments for a match,
189+ // as not recognizing a test process will cause timeout and failure.
190+ for (int i = 0 ; i < ARGS_ATTEMPTS ; i ++) {
191+ try {
192+ MonitoredVm target = host .getMonitoredVm (vmid );
193+ String monitoredArgs = MonitoredVmUtil .mainArgs (target );
194+ System .out .println ("hasMainArgs(" + id + "): has main args: '" + monitoredArgs + "'" );
195+ if (monitoredArgs == null || monitoredArgs .equals ("Unknown" )) {
196+ System .out .println ("hasMainArgs(" + id + "): retry" );
197+ takeNap ();
198+ continue ;
199+ } else if (monitoredArgs .contains (args )) {
200+ return true ;
201+ } else {
202+ return false ;
203+ }
204+ } catch (MonitorException e ) {
205+ // Process probably not running or not ours, e.g.
206+ // sun.jvmstat.monitor.MonitorException: Could not attach to PID
207+ System .out .println ("hasMainArgs(" + id + "): " + e );
183208 }
184- } catch (URISyntaxException | MonitorException e ) {
185- // ok. process probably not running
186209 }
187210 return false ;
188211 }
@@ -247,14 +270,6 @@ private static void waitForRemoval(Path path) {
247270 }
248271 }
249272
250- private static void takeNap () {
251- try {
252- Thread .sleep (100 );
253- } catch (InterruptedException e ) {
254- // ignore
255- }
256- }
257-
258273 private final String mainArgsIdentifier ;
259274 private final ShutdownHook shutdownHook ;
260275 private volatile Integer id ;
@@ -322,4 +337,12 @@ public String getMainArgsIdentifier() {
322337 return mainArgsIdentifier ;
323338 }
324339 }
340+
341+ public static void takeNap () {
342+ try {
343+ Thread .sleep (1000 );
344+ } catch (InterruptedException e ) {
345+ // ignore
346+ }
347+ }
325348}
0 commit comments