J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4530538 |
| 27 | * @summary Basic unit test of RuntimeMXBean.getUptime() |
| 28 | * @author Alexei Guibadoulline |
| 29 | */ |
| 30 | |
| 31 | import java.lang.management.*; |
| 32 | |
| 33 | public class UpTime { |
| 34 | final static long DELAY = 5; // Seconds |
| 35 | final static long TIMEOUT = 30; // Minutes |
| 36 | private static RuntimeMXBean metrics |
| 37 | = ManagementFactory.getRuntimeMXBean(); |
| 38 | |
| 39 | public static void main(String argv[]) throws Exception { |
| 40 | long systemStartOuter = System.currentTimeMillis(); |
| 41 | long metricsStart = metrics.getUptime(); |
| 42 | long systemStartInner = System.currentTimeMillis(); |
| 43 | |
| 44 | // If uptime is more than 30 minutes then it looks like a bug in |
| 45 | // the method |
| 46 | if (metricsStart > TIMEOUT * 60 * 1000) |
| 47 | throw new RuntimeException("Uptime of the JVM is more than 30 " |
| 48 | + "minutes (" |
| 49 | + (metricsStart / 60 / 1000) |
| 50 | + " minutes)."); |
| 51 | |
| 52 | // Wait for DELAY seconds |
| 53 | Object o = new Object(); |
| 54 | while (System.currentTimeMillis() < systemStartInner + DELAY * 1000) { |
| 55 | synchronized (o) { |
| 56 | try { |
| 57 | o.wait(DELAY * 1000); |
| 58 | } catch (Exception e) { |
| 59 | e.printStackTrace(); |
| 60 | throw e; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | long systemEndInner = System.currentTimeMillis(); |
| 66 | long metricsEnd = metrics.getUptime(); |
| 67 | long systemEndOuter = System.currentTimeMillis(); |
| 68 | |
| 69 | long systemDifferenceInner = systemEndInner - systemStartInner; |
| 70 | long systemDifferenceOuter = systemEndOuter - systemStartOuter; |
| 71 | long metricsDifference = metricsEnd - metricsStart; |
| 72 | |
| 73 | // Check the flow of time in RuntimeMXBean.getUptime(). See the |
| 74 | // picture below |
| 75 | if (metricsDifference < systemDifferenceInner) |
| 76 | throw new RuntimeException("Flow of the time in " |
| 77 | + "RuntimeMXBean.getUptime() (" |
| 78 | + metricsDifference + ") is slower than " |
| 79 | + " in system (" + systemDifferenceInner |
| 80 | + ")"); |
| 81 | if (metricsDifference > systemDifferenceOuter) |
| 82 | throw new RuntimeException("Flow of the time in " |
| 83 | + "RuntimeMXBean.getUptime() (" |
| 84 | + metricsDifference + ") is faster than " |
| 85 | + "in system (" + systemDifferenceOuter |
| 86 | + ")"); |
| 87 | |
| 88 | System.out.println("Test passed."); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | |
| 94 | A picture to describe the second testcase that checks the flow of time in |
| 95 | RuntimeMXBean.getUptime() |
| 96 | |
| 97 | |
| 98 | start |
| 99 | o1 u1 i1 Sleep for DELAY minutes i2 u2 o2 |
| 100 | |-------|---|---|--------------------------------|---|---|---------------> time |
| 101 | |
| 102 | |
| 103 | The following inequality (checked by the test) must always be true: |
| 104 | |
| 105 | o2-o1 >= u2-u1 >= i2-i1 |
| 106 | |
| 107 | In the test: |
| 108 | |
| 109 | i1 - systemStartInner |
| 110 | i2 - systemEndInner |
| 111 | o1 - systemStartOuter |
| 112 | o2 - systemEndOuter |
| 113 | u1 - metricsStart |
| 114 | u2 - metricsEnd |
| 115 | |
| 116 | */ |