Example usage for org.apache.commons.pool VisitTracker getValidateCount

List of usage examples for org.apache.commons.pool VisitTracker getValidateCount

Introduction

In this page you can find the example usage for org.apache.commons.pool VisitTracker getValidateCount.

Prototype

public int getValidateCount() 

Source Link

Usage

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

private void checkEvictorVisiting(boolean lifo) throws Exception {
    VisitTrackerFactory factory = new VisitTrackerFactory();
    GenericObjectPool pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(2);/*from  w  w  w  .ja va 2  s  .co  m*/
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // Visit oldest 2 - 0 and 1
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    // borrow, return, borrow, return
    // FIFO will move 0 and 1 to end
    // LIFO, 7 out, then in, then out, then in
    pool.evict(); // Should visit 2 and 3 in either case
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        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());
        }
    }

    factory = new VisitTrackerFactory();
    pool = new GenericObjectPool(factory);
    pool.setNumTestsPerEvictionRun(3);
    pool.setMinEvictableIdleTimeMillis(-1);
    pool.setTestWhileIdle(true);
    pool.setLifo(lifo);
    pool.setTestOnReturn(false);
    pool.setTestOnBorrow(false);
    for (int i = 0; i < 8; i++) {
        pool.addObject();
    }
    pool.evict(); // 0, 1, 2
    pool.evict(); // 3, 4, 5
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(obj);
    obj = pool.borrowObject();
    pool.returnObject(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();
    // Should hit 6,7,0 - 0 for second time
    for (int i = 0; i < 8; i++) {
        VisitTracker tracker = (VisitTracker) pool.borrowObject();
        if (tracker.getId() != 0) {
            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 a 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());
    for (int i = 0; i < 4; i++) {
        pool.setNumTestsPerEvictionRun(smallPrimes[i]);
        for (int j = 0; j < 5; j++) {
            pool = new GenericObjectPool(factory);
            pool.setNumTestsPerEvictionRun(3);
            pool.setMinEvictableIdleTimeMillis(-1);
            pool.setTestWhileIdle(true);
            pool.setLifo(lifo);
            pool.setTestOnReturn(false);
            pool.setTestOnBorrow(false);
            pool.setMaxIdle(-1);
            int instanceCount = 10 + random.nextInt(20);
            pool.setMaxActive(instanceCount);
            for (int k = 0; k < instanceCount; k++) {
                pool.addObject();
            }

            // Execute a random number of evictor runs
            int runs = 10 + random.nextInt(50);
            for (int k = 0; k < runs; k++) {
                pool.evict();
            }

            // Number of times evictor should have cycled through the pool
            int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount;

            // 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 < instanceCount; k++) {
                tracker = (VisitTracker) pool.borrowObject();
                assertTrue(pool.getNumActive() <= pool.getMaxActive());
                visitCount = tracker.getValidateCount();
                assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1);
            }
        }
    }
}