KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > TimeMovesForwardTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.test.TCTestCase;
7
8 public class TimeMovesForwardTest extends TCTestCase {
9
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) { throw new AssertionError JavaDoc("Clock moved from " + prev + " to " + sample); }
29       prev = sample;
30
31       if (delay) {
32         delay(sample);
33       }
34     }
35
36     System.out.println(count + " samples took " + (System.currentTimeMillis() - start) + " millis");
37   }
38
39   public void delay(long sample) {
40     int n = 12500;
41     for (int i = 0; i < n; i++) {
42       // this code should prevent the optimizer from making this method a total noop
43
if (i == sample) {
44         System.out.println();
45       }
46     }
47   }
48
49 }
50
Popular Tags