|
1 | 1 | /* |
2 | | - * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
43 | 43 | import java.rmi.registry.Registry; |
44 | 44 | import java.util.ArrayList; |
45 | 45 | import java.util.Arrays; |
| 46 | +import java.util.concurrent.TimeUnit; |
46 | 47 | import java.util.HashMap; |
47 | 48 | import java.util.HashSet; |
48 | 49 | import java.util.List; |
@@ -110,17 +111,21 @@ public synchronized void start() throws Exception { |
110 | 111 | try { |
111 | 112 | AtomicBoolean error = new AtomicBoolean(false); |
112 | 113 | AtomicBoolean bindError = new AtomicBoolean(false); |
| 114 | + // The predicate below tries to recognise failures. On a port clash, it sees e.g. |
| 115 | + // Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 46481; nested exception is: |
| 116 | + // ...and will never see "main enter" from TestApp. |
113 | 117 | p = ProcessTools.startProcess( |
114 | 118 | TEST_APP_NAME + "{" + name + "}", |
115 | 119 | pb, |
116 | 120 | (line) -> { |
117 | | - if (line.toLowerCase().contains("exception") |
118 | | - || line.toLowerCase().contains("error")) { |
| 121 | + if (line.contains("Exception")) { |
119 | 122 | error.set(true); |
| 123 | + bindError.set(line.toLowerCase().contains("port already in use")); |
| 124 | + return true; // On Exception, app will never start |
120 | 125 | } |
121 | | - bindError.set(line.toLowerCase().contains("bindexception")); |
122 | | - return true; |
123 | | - }); |
| 126 | + return line.contains("main enter"); |
| 127 | + }, |
| 128 | + 60, TimeUnit.SECONDS); |
124 | 129 | if (bindError.get()) { |
125 | 130 | throw new BindException("Process could not be started"); |
126 | 131 | } else if (error.get()) { |
@@ -164,9 +169,25 @@ public void close() throws Exception { |
164 | 169 | } |
165 | 170 |
|
166 | 171 | private static final String TEST_APP_NAME = "TestApp"; |
| 172 | + private static final int FREE_PORT_ATTEMPTS = 10; |
167 | 173 |
|
168 | 174 | private static void testDefaultAgent(String propertyFile) throws Exception { |
169 | | - int port = Utils.getFreePort(); |
| 175 | + for (int i = 1; i <= FREE_PORT_ATTEMPTS; i++) { |
| 176 | + int port = Utils.getFreePort(); |
| 177 | + System.out.println("Attempting testDefaultAgent(" + propertyFile + ") with port: " + port); |
| 178 | + try { |
| 179 | + testDefaultAgent(propertyFile, port); |
| 180 | + break; // return succesfully |
| 181 | + } catch (BindException b) { |
| 182 | + // Retry with new port. Throw if last iteration: |
| 183 | + if (i == FREE_PORT_ATTEMPTS) { |
| 184 | + throw(b); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private static void testDefaultAgent(String propertyFile, int port) throws Exception { |
170 | 191 | String propFile = System.getProperty("test.src") + File.separator + propertyFile; |
171 | 192 | List<String> pbArgs = new ArrayList<>(Arrays.asList( |
172 | 193 | "-cp", |
@@ -235,53 +256,32 @@ private static JMXServiceURL testConnect(int port) throws Exception { |
235 | 256 | public static void main(String[] args) throws Exception { |
236 | 257 | System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: starting ..."); |
237 | 258 |
|
238 | | - boolean retry = false; |
239 | | - do { |
240 | | - try { |
241 | | - // filter DefaultAgentFilterTest$MyTestObject |
242 | | - testDefaultAgent("mgmt1.properties"); |
243 | | - System.out.println("----\tTest FAILED !!"); |
244 | | - throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported"); |
245 | | - } catch (Exception ex) { |
246 | | - if (ex instanceof InvocationTargetException) { |
247 | | - if (ex.getCause() instanceof BindException |
248 | | - || ex.getCause() instanceof java.rmi.ConnectException) { |
249 | | - System.out.println("Failed to allocate ports. Retrying ..."); |
250 | | - retry = true; |
251 | | - } |
252 | | - } else if (ex instanceof InvalidClassException) { |
253 | | - System.out.println("----\tTest PASSED !!"); |
254 | | - } else if (ex instanceof UnmarshalException |
255 | | - && ((UnmarshalException) ex).getCause() instanceof InvalidClassException) { |
256 | | - System.out.println("----\tTest PASSED !!"); |
257 | | - } else { |
258 | | - System.out.println(ex); |
259 | | - System.out.println("----\tTest FAILED !!"); |
260 | | - throw ex; |
261 | | - } |
262 | | - } |
263 | | - } while (retry); |
264 | | - retry = false; |
265 | | - do { |
266 | | - try { |
267 | | - // filter non-existent class |
268 | | - testDefaultAgent("mgmt2.properties"); |
| 259 | + try { |
| 260 | + // filter DefaultAgentFilterTest$MyTestObject |
| 261 | + testDefaultAgent("mgmt1.properties"); |
| 262 | + System.out.println("----\tTest FAILED !!"); |
| 263 | + throw new RuntimeException("---" + DefaultAgentFilterTest.class.getName() + " - No exception reported"); |
| 264 | + } catch (Exception ex) { |
| 265 | + if (ex instanceof InvalidClassException) { |
269 | 266 | System.out.println("----\tTest PASSED !!"); |
270 | | - } catch (Exception ex) { |
271 | | - if (ex instanceof InvocationTargetException) { |
272 | | - if (ex.getCause() instanceof BindException |
273 | | - || ex.getCause() instanceof java.rmi.ConnectException) { |
274 | | - System.out.println("Failed to allocate ports. Retrying ..."); |
275 | | - retry = true; |
276 | | - } |
277 | | - } else { |
278 | | - System.out.println(ex); |
279 | | - System.out.println("----\tTest FAILED !!"); |
280 | | - throw ex; |
281 | | - } |
| 267 | + } else if (ex instanceof UnmarshalException |
| 268 | + && ((UnmarshalException) ex).getCause() instanceof InvalidClassException) { |
| 269 | + System.out.println("----\tTest PASSED !!"); |
| 270 | + } else { |
| 271 | + System.out.println(ex); |
| 272 | + System.out.println("----\tTest FAILED !!"); |
| 273 | + throw ex; |
282 | 274 | } |
283 | | - } while (retry); |
284 | | - |
| 275 | + } |
| 276 | + try { |
| 277 | + // filter non-existent class |
| 278 | + testDefaultAgent("mgmt2.properties"); |
| 279 | + System.out.println("----\tTest PASSED !!"); |
| 280 | + } catch (Exception ex) { |
| 281 | + System.out.println(ex); |
| 282 | + System.out.println("----\tTest FAILED !!"); |
| 283 | + throw ex; |
| 284 | + } |
285 | 285 | System.out.println("---" + DefaultAgentFilterTest.class.getName() + "-main: finished ..."); |
286 | 286 | } |
287 | 287 |
|
|
0 commit comments