// borrow, return, borrow, return
// FIFO will move 0 and 1 to end - 2,3,4,5,6,7,0,1
// LIFO, 7 out, then in, then out, then in - 7,6,5,4,3,2,1,0
pool.evict(); // Should visit 02 and 03 in either case
for (int i = 0; i < 8; i++) {
VisitTracker tracker = (VisitTracker) pool.borrowObject(zero);
if (tracker.getId() >= 4) {
assertEquals("Unexpected instance visited " + tracker.getId(),
0, tracker.getValidateCount());
} else {
assertEquals("Instance " + tracker.getId() +
" visited wrong number of times.",
1, tracker.getValidateCount());
}
}
// 0's are all out
pool.setNumTestsPerEvictionRun(3);
pool.evict(); // 10, 11, 12
pool.evict(); // 13, 14, 15
obj = pool.borrowObject(one);
pool.returnObject(one, obj);
obj = pool.borrowObject(one);
pool.returnObject(one, obj);
obj = pool.borrowObject(one);
pool.returnObject(one, obj);
// borrow, return, borrow, return
// FIFO 3,4,5,^,6,7,0,1,2
// LIFO 7,6,^,5,4,3,2,1,0
// In either case, pointer should be at 6
pool.evict();
// LIFO - 16, 17, 20
// FIFO - 16, 17, 10
pool.evict();
// LIFO - 21, 22, 23
// FIFO - 11, 12, 20
pool.evict();
// LIFO - 24, 25, 26
// FIFO - 21, 22, 23
pool.evict();
// LIFO - 27, skip, 10
// FIFO - 24, 25, 26
for (int i = 0; i < 8; i++) {
VisitTracker tracker = (VisitTracker) pool.borrowObject(one);
if ((lifo && tracker.getId() > 0) ||
(!lifo && tracker.getId() > 2)) {
assertEquals("Instance " + tracker.getId() +
" visited wrong number of times.",
1, tracker.getValidateCount());
} else {
assertEquals("Instance " + tracker.getId() +
" visited wrong number of times.",
2, tracker.getValidateCount());
}
}
// Randomly generate some pools with random numTests
// and make sure evictor cycles through elements appropriately
int[] smallPrimes = {2, 3, 5, 7};
Random random = new Random();
random.setSeed(System.currentTimeMillis());
pool.setMaxIdle(-1);
for (int i = 0; i < smallPrimes.length; i++) {
pool.setNumTestsPerEvictionRun(smallPrimes[i]);
for (int j = 0; j < 5; j++) {// Try the tests a few times
pool.clear();
assertEquals("NumIdle should be zero after clearing the pool",0,pool.getNumIdle());
int zeroLength = 10 + random.nextInt(20);
for (int k = 0; k < zeroLength; k++) {
pool.addObject(zero);
}
int oneLength = 10 + random.nextInt(20);
for (int k = 0; k < oneLength; k++) {
pool.addObject(one);
}
int twoLength = 10 + random.nextInt(20);
for (int k = 0; k < twoLength; k++) {
pool.addObject(two);
}
// Choose a random number of evictor runs
int runs = 10 + random.nextInt(50);
for (int k = 0; k < runs; k++) {
pool.evict();
}
// Total instances in pool
int totalInstances = zeroLength + oneLength + twoLength;
// Number of times evictor should have cycled through pools
int cycleCount = (runs * pool.getNumTestsPerEvictionRun())
/ totalInstances;
// Look at elements and make sure they are visited cycleCount
// or cycleCount + 1 times
VisitTracker tracker = null;
int visitCount = 0;
for (int k = 0; k < zeroLength; k++) {
tracker = (VisitTracker) pool.borrowObject(zero);
visitCount = tracker.getValidateCount();
if (visitCount < cycleCount || visitCount > cycleCount + 1){
fail(formatSettings("ZERO", "runs", runs, "lifo", lifo, "i", i, "j", j,
"k", k, "visitCount", visitCount, "cycleCount", cycleCount,
"totalInstances", totalInstances, zeroLength, oneLength, twoLength));
}
}
for (int k = 0; k < oneLength; k++) {
tracker = (VisitTracker) pool.borrowObject(one);
visitCount = tracker.getValidateCount();
if (visitCount < cycleCount || visitCount > cycleCount + 1){
fail(formatSettings("ONE", "runs", runs, "lifo", lifo, "i", i, "j", j,
"k", k, "visitCount", visitCount, "cycleCount", cycleCount,
"totalInstances", totalInstances, zeroLength, oneLength, twoLength));
}
}
int visits[] = new int[twoLength];
for (int k = 0; k < twoLength; k++) {
tracker = (VisitTracker) pool.borrowObject(two);
visitCount = tracker.getValidateCount();
visits[k] = visitCount;
if (visitCount < cycleCount || visitCount > cycleCount + 1){
StringBuffer sb = new StringBuffer("Visits:");
for (int l = 0; l <= k; l++){
sb.append(visits[l]).append(' ');