blob: d4b73d7efc7d8ba6ada30443d7e95a6ecd1b65e2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package org.simpleframework.common.lease;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.simpleframework.common.lease.Cleaner;
import org.simpleframework.common.lease.Contract;
import org.simpleframework.common.lease.ContractController;
import org.simpleframework.common.lease.ContractLease;
import org.simpleframework.common.lease.ContractMaintainer;
import org.simpleframework.common.lease.Expiration;
import org.simpleframework.common.lease.Lease;
public class LeaseTest extends TimeTestCase {
private static int ITERATIONS = 10000;
static {
String value = System.getProperty("iterations");
if (value != null) {
ITERATIONS = Integer.parseInt(value);
}
}
public void testLease() throws Exception {
final BlockingQueue<Integer> clean = new LinkedBlockingQueue<Integer>();
Cleaner<Integer> cleaner = new Cleaner<Integer>() {
public void clean(Integer key) {
clean.offer(key);
}
};
Map<Integer, Contract> table = new ConcurrentHashMap<Integer, Contract>();
List<Lease> list = new ArrayList<Lease>();
ContractController controller = new ContractMaintainer(cleaner);
for (int i = 0; i < ITERATIONS; i++) {
long random = (long) (Math.random() * 1000) + 1000L;
Contract<Integer> contract = new Expiration(i, random, TimeUnit.MILLISECONDS);
Lease lease = new ContractLease(controller, contract);
table.put(i, contract);
list.add(lease);
controller.issue(contract);
}
for (int i = 0; i < ITERATIONS; i++) {
long random = (long) (Math.random() * 1000);
try {
list.get(i).renew(random, TimeUnit.MILLISECONDS);
} catch (Exception e) {
continue;
// e.printStackTrace();
}
}
for (int i = 0; i < ITERATIONS; i++) {
try {
System.err.println("delay: "
+ list.get(i).getExpiry(TimeUnit.MILLISECONDS));
} catch (Exception e) {
continue;
// e.printStackTrace();
}
}
System.err.println("clean: " + clean.size());
for (int i = 0; i < ITERATIONS; i++) {
Integer index = clean.take();
Contract contract = table.get(index);
// assertLessThanOrEqual(-4000,
// contract.getDelay(TimeUnit.MILLISECONDS));
System.err.println(String.format("index=[%s] delay=[%s]", index,
contract.getDelay(TimeUnit.MILLISECONDS)));
}
}
public static void main(String[] list) throws Exception {
new LeaseTest().testLease();
}
}
|