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.object.lockmanager.api;
05:
06: import java.util.Random;
07:
08: import junit.framework.TestCase;
09:
10: public class LockRequestTest extends TestCase {
11: public void testEqualsAndHashCode() throws Exception {
12: Random random = new Random();
13: int instanceCount = 1000000;
14:
15: LockRequest notEqual = new LockRequest(new LockID(
16: "nothing like me"), new ThreadID(Integer.MAX_VALUE),
17: LockLevel.WRITE);
18: long[] samples = new long[instanceCount];
19: for (int i = 0; i < instanceCount; i++) {
20: LockID lid = new LockID(random
21: .nextInt(Integer.MAX_VALUE - 1)
22: + "");
23: ThreadID tid = new ThreadID(random
24: .nextInt(Integer.MAX_VALUE - 1));
25: int lockType = (i % 2 == 0) ? LockLevel.READ
26: : LockLevel.WRITE;
27:
28: long t0 = System.currentTimeMillis();
29: LockRequest a = new LockRequest(lid, tid, lockType);
30: samples[i] = System.currentTimeMillis() - t0;
31:
32: LockRequest b = new LockRequest(new LockID(lid.asString()),
33: new ThreadID(tid.toLong()), lockType);
34: assertEquals(a, b);
35: assertEquals(a.hashCode(), b.hashCode());
36: assertFalse(a.equals(notEqual));
37: assertFalse(b.equals(notEqual));
38: }
39: long min = 0, max = 0, sum = 0;
40: for (int i = 0; i < samples.length; i++) {
41: if (samples[i] < min)
42: min = samples[i];
43: if (samples[i] > max)
44: max = samples[i];
45: sum += samples[i];
46: }
47: double mean = sum / samples.length;
48: System.out.println("min: " + min + ", max: " + max + ", mean: "
49: + mean);
50: }
51: }
|