1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.id.uuid.clock;
19
20 import junit.framework.TestCase;
21
22 import java.util.Arrays;
23
24
25
26
27
28
29
30 public class ThreadClockImplTest extends TestCase {
31
32
33 public static final String SYS_OS_NAME_PROPERTY = "os.name";
34
35
36 public static final String SYS_OS_WINDOWS = "Windows";
37
38
39 private static boolean isWindows = false;
40
41 protected void setUp() throws Exception {
42 super.setUp();
43 if (System.getProperty(SYS_OS_NAME_PROPERTY).indexOf(SYS_OS_WINDOWS) > -1) {
44 isWindows = true;
45 }
46 }
47
48
49
50
51
52
53
54 public void testUnique() throws Exception {
55
56 if (!isWindows) {
57 return;
58 }
59
60
61
62
63 int iterations = 11000;
64
65
66
67
68 int threadCount = 4;
69
70
71
72 long[][] threadTimes = new long[threadCount][iterations];
73 Thread[] clockClients = new Thread[threadCount];
74
75 for (int i = 0; i < threadCount; i++) {
76 clockClients[i] = new ClockClient(threadTimes[i], iterations);
77 clockClients[i].start();
78 }
79
80
81 boolean working = true;
82 while (working) {
83 working = false;
84 for (int i = 0; i < threadCount; i++) {
85 if (clockClients[i].isAlive()) {
86 working = true;
87 }
88 }
89 }
90
91
92
93 for (int j = 0; j < threadTimes.length; j++) {
94 Arrays.sort(threadTimes[j]);
95 for (int i = 0; i < threadTimes.length - 1; i++) {
96 if (threadTimes[j][i] == threadTimes[j][i + 1]) {
97 fail( "Duplicate time stamps generated: " + threadTimes[j][i] + " " + i);
98 }
99 }
100 }
101 }
102
103
104
105
106
107
108
109 public void testRange() throws Exception {
110 if (!isWindows) {
111 return;
112 }
113 long time = 0;
114 long baseTime = 0;
115 Clock c = new ThreadClockImpl();
116 for (int i = 0; i < 100; i++) {
117 Thread.currentThread().sleep(10);
118 for (int j = 0; j < 100; j++) {
119 try {
120 baseTime = System.currentTimeMillis();
121 time = c.getUUIDTime();
122 assertTrue( "Generated timestamp too large", time < ((baseTime + Clock.GREGORIAN_CHANGE_OFFSET + 1000)
123 * Clock.INTERVALS_PER_MILLI));
124
125 assertTrue("Generated timestamp too small", time > ((baseTime + Clock.GREGORIAN_CHANGE_OFFSET - 1000)
126 * Clock.INTERVALS_PER_MILLI));
127 } catch (OverClockedException oce) {
128
129 }
130 }
131 }
132 }
133
134
135
136
137
138 protected static class ClockClient extends Thread {
139
140 private static int overClocks;
141
142
143 private Clock c = new ThreadClockImpl();
144
145
146
147
148 protected long[] times;
149
150
151
152
153 protected int iterations;
154
155
156
157
158
159
160
161 ClockClient(long[] timez, int iterationz) {
162 super();
163 this.times = timez;
164 this.iterations = iterationz;
165 }
166
167
168
169
170 public void run() {
171 for (int i = 0; i < iterations;) {
172 try {
173 times[i] = c.getUUIDTime();
174 i++;
175 } catch (OverClockedException oce) {
176 overClocks++;
177 }
178 }
179 System.out.println("At End of this thread - Overclocks currently at: " + overClocks);
180 }
181 }
182 }