01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util;
05:
06: import com.tc.test.TCTestCase;
07:
08: public class TimeMovesForwardTest extends TCTestCase {
09:
10: private static final long SAMPLE_DURATION = 60000;
11:
12: public void testTimeNoDelay() {
13: measure(false);
14: }
15:
16: public void testTimeWithDelay() {
17: measure(true);
18: }
19:
20: public void measure(boolean delay) {
21: final long start = System.currentTimeMillis();
22: long prev = start;
23: int count = 0;
24:
25: while ((prev - start) < SAMPLE_DURATION) {
26: count++;
27: long sample = System.currentTimeMillis();
28: if (sample < prev) {
29: throw new AssertionError("Clock moved from " + prev
30: + " to " + sample);
31: }
32: prev = sample;
33:
34: if (delay) {
35: delay(sample);
36: }
37: }
38:
39: System.out.println(count + " samples took "
40: + (System.currentTimeMillis() - start) + " millis");
41: }
42:
43: public void delay(long sample) {
44: int n = 12500;
45: for (int i = 0; i < n; i++) {
46: // this code should prevent the optimizer from making this method a total noop
47: if (i == sample) {
48: System.out.println();
49: }
50: }
51: }
52:
53: }
|