KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > api > LockRequestTest


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