|
24 | 24 | import java.io.FileOutputStream; |
25 | 25 | import java.io.IOException; |
26 | 26 | import java.io.PrintWriter; |
| 27 | +import java.lang.management.ManagementFactory; |
| 28 | +import java.lang.management.ThreadInfo; |
| 29 | +import java.lang.management.ThreadMXBean; |
| 30 | +import java.util.Timer; |
27 | 31 |
|
28 | 32 | public class TestShell extends TestCase { |
29 | 33 |
|
@@ -95,6 +99,46 @@ public void testShellCommandTimeout() throws Throwable { |
95 | 99 | shellFile.delete(); |
96 | 100 | assertTrue("Script didnt not timeout" , shexc.isTimedOut()); |
97 | 101 | } |
| 102 | + |
| 103 | + private static int countTimerThreads() { |
| 104 | + ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); |
| 105 | + |
| 106 | + int count = 0; |
| 107 | + ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20); |
| 108 | + for (ThreadInfo info : infos) { |
| 109 | + if (info == null) continue; |
| 110 | + for (StackTraceElement elem : info.getStackTrace()) { |
| 111 | + if (elem.getClassName().contains("Timer")) { |
| 112 | + count++; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return count; |
| 118 | + } |
| 119 | + |
| 120 | + public void testShellCommandTimerLeak() throws Exception { |
| 121 | + String quickCommand[] = new String[] {"/bin/sleep", "100"}; |
| 122 | + |
| 123 | + int timersBefore = countTimerThreads(); |
| 124 | + System.err.println("before: " + timersBefore); |
| 125 | + |
| 126 | + for (int i = 0; i < 10; i++) { |
| 127 | + Shell.ShellCommandExecutor shexec = new Shell.ShellCommandExecutor( |
| 128 | + quickCommand, null, null, 1); |
| 129 | + try { |
| 130 | + shexec.execute(); |
| 131 | + fail("Bad command should throw exception"); |
| 132 | + } catch (Exception e) { |
| 133 | + // expected |
| 134 | + } |
| 135 | + } |
| 136 | + Thread.sleep(1000); |
| 137 | + int timersAfter = countTimerThreads(); |
| 138 | + System.err.println("after: " + timersAfter); |
| 139 | + assertEquals(timersBefore, timersAfter); |
| 140 | + } |
| 141 | + |
98 | 142 |
|
99 | 143 | private void testInterval(long interval) throws IOException { |
100 | 144 | Command command = new Command(interval); |
|
0 commit comments