| 1 | package org.jtoolkit.essence.test; |
| 2 | |
| 3 | import junit.framework.TestCase; |
| 4 | import org.apache.commons.logging.Log; |
| 5 | import org.apache.commons.logging.LogFactory; |
| 6 | import org.jtoolkit.essence.ConfigProperties; |
| 7 | import org.jtoolkit.essence.app.Main; |
| 8 | import org.jtoolkit.essence.concurrency.Threads; |
| 9 | import org.jtoolkit.essence.data.Transaction; |
| 10 | import org.jtoolkit.essence.utils.JvmMetrics; |
| 11 | |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.List; |
| 14 | import java.util.Map; |
| 15 | import java.util.Properties; |
| 16 | |
| 17 | @SuppressWarnings({"JUnitTestCaseInProductSource", "UseOfSystemOutOrSystemErr"}) |
| 18 | public abstract class MetricTestCase extends TestCase { |
| 19 | private static final Log LOG = LogFactory.getLog(MetricTestCase.class); |
| 20 | private static final long MEMORY_THREADHOLD = Long.getLong(ConfigProperties.ESSENCE_TEST_MEMORY_THREASHOLD, 256 * 1024); |
| 21 | private static final boolean MEMORY_HEAPDUMP = Boolean.getBoolean(ConfigProperties.ESSENCE_TEST_MEMORY_HEAPDUMP); |
| 22 | private static final Properties SYSTEM_PROPERTIES = System.getProperties(); |
| 23 | |
| 24 | protected static final boolean BIG_TEST = Boolean.getBoolean(ConfigProperties.ESSENCE_BIG_TEST); |
| 25 | |
| 26 | private Map<Thread, StackTraceElement[]> allStackTraces = null; |
| 27 | protected final JvmMetrics startMetrics = new JvmMetrics(); |
| 28 | |
| 29 | protected void setUp() throws Exception { |
| 30 | super.setUp(); |
| 31 | Properties props = new Properties(); |
| 32 | //noinspection UseOfPropertiesAsHashtable |
| 33 | props.putAll(SYSTEM_PROPERTIES); |
| 34 | System.setProperties(props); |
| 35 | } |
| 36 | |
| 37 | protected void runTest() throws Throwable { |
| 38 | JvmMetrics.waitForGC(); |
| 39 | |
| 40 | JvmMetrics sm = new JvmMetrics(); |
| 41 | allStackTraces = Thread.getAllStackTraces(); |
| 42 | |
| 43 | super.runTest(); |
| 44 | System.out.println(getDescription() + sm.delta()); |
| 45 | } |
| 46 | |
| 47 | protected void tearDown() throws Exception { |
| 48 | super.tearDown(); |
| 49 | |
| 50 | Transaction.rollbackAll(); |
| 51 | Threads.clearDefaultSES(); |
| 52 | Main.closeAll(); |
| 53 | |
| 54 | // check all threads started are stopped. |
| 55 | boolean threadsOkay = checkRunningThreads(); |
| 56 | if (!threadsOkay) |
| 57 | LOG.error(getDescription() + "Additional threads are running after the test"); |
| 58 | |
| 59 | JvmMetrics smDelta = startMetrics.delta(); |
| 60 | if (smDelta.threadCount > 0) |
| 61 | LOG.warn(getDescription() + "The number of threads increased by " + smDelta.threadCount); |
| 62 | if (smDelta.mbeanCount > 0) |
| 63 | LOG.warn(getDescription() + "The number of MBeans increased by " + smDelta.mbeanCount); |
| 64 | if (smDelta.memoryUsed > MEMORY_THREADHOLD) { |
| 65 | LOG.warn(getDescription() + "Test consumed " + smDelta.memoryUsed + " bytes."); |
| 66 | // trigger OutOfMemoryError. Must be triggered to cause a Heap dump. |
| 67 | if (MEMORY_HEAPDUMP) |
| 68 | triggerOutOfMemoryError(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public static void triggerOutOfMemoryError() { |
| 73 | //noinspection MismatchedQueryAndUpdateOfCollection |
| 74 | List<long[]> list = new ArrayList<long[]>(); |
| 75 | //noinspection InfiniteLoopStatement |
| 76 | while (true) |
| 77 | list.add(new long[1024 * 1024]); |
| 78 | } |
| 79 | |
| 80 | private String getDescription() { |
| 81 | return getClass().getName() + '.' + getName() + "()" + " ~~~~ "; |
| 82 | } |
| 83 | |
| 84 | protected boolean checkRunningThreads() throws InterruptedException { |
| 85 | boolean threadsOkay = true; |
| 86 | LOOP: |
| 87 | for (int i = 0; i < 12; i++) { |
| 88 | JvmMetrics.waitForGC(); |
| 89 | Thread.sleep(1000); |
| 90 | // look for left over threads. |
| 91 | Map<Thread, StackTraceElement[]> allStackTraces2 = Thread.getAllStackTraces(); |
| 92 | threadsOkay = true; |
| 93 | for (Thread thread : allStackTraces2.keySet()) { |
| 94 | if (allStackTraces.containsKey(thread)) continue; |
| 95 | String name = thread.getName(); |
| 96 | if (name.startsWith(Threads.CONTROL_THREAD)) continue; |
| 97 | if (i < 11) { |
| 98 | System.out.println("tearDown(): Waiting for " + thread + " to close."); |
| 99 | continue LOOP; |
| 100 | } |
| 101 | if (Threads.dumpStack(thread, allStackTraces2.get(thread))) |
| 102 | threadsOkay = false; |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | return threadsOkay; |
| 107 | } |
| 108 | |
| 109 | public static void assertEquals(Object[] array1, Object[] array2) { |
| 110 | assertEquals("", array1, array2); |
| 111 | } |
| 112 | |
| 113 | public static void assertEquals(String text, Object[] array1, Object[] array2) { |
| 114 | assertEquals(text + " size", array1.length, array2.length); |
| 115 | for (int i = 0; i < array1.length; i++) |
| 116 | assertEquals(text + "[" + i + ']', array1[i], array2[i]); |
| 117 | } |
| 118 | } |