001: /* FairGenericObjectPoolTest
002: *
003: * $Id: FairGenericObjectPoolTest.java 4672 2006-09-27 00:03:16Z paul_jack $
004: *
005: * Created on Apr 7, 2006
006: *
007: * Copyright (C) 2006 Internet Archive.
008: *
009: */
010: package org.apache.commons.pool.impl;
011:
012: import java.util.Arrays;
013: import java.util.Collections;
014: import java.util.LinkedList;
015: import java.util.List;
016:
017: import junit.framework.TestCase;
018:
019: import org.apache.commons.pool.BasePoolableObjectFactory;
020:
021: /**
022: * Test for FairGenericObjectPool.
023: *
024: * @author gojomo
025: */
026: @SuppressWarnings("unchecked")
027: public class FairGenericObjectPoolTest extends TestCase {
028: // public void testUnfair() throws InterruptedException {
029: //// System.out.println("unfair");
030: // GenericObjectPool pool = new GenericObjectPool();
031: //
032: // Object[] borrowOrder = tryPool(pool);
033: //
034: // Object[] sortedOrder = (Object[]) borrowOrder.clone();
035: // Arrays.sort(sortedOrder);
036: // assertFalse("unexpectedly fair", Arrays.equals(borrowOrder,sortedOrder));
037: // }
038:
039: public void testFair() throws InterruptedException {
040: // System.out.println("fair");
041: GenericObjectPool pool = new FairGenericObjectPool();
042:
043: Object[] borrowOrder = tryPool(pool);
044:
045: Object[] sortedOrder = (Object[]) borrowOrder.clone();
046: Arrays.sort(sortedOrder);
047: assertTrue("unexpectedly unfair", Arrays.equals(borrowOrder,
048: sortedOrder));
049: }
050:
051: /**
052: * Test the given pool for fairness.
053: *
054: * @param pool GenericObjectPool to test
055: * @throws InterruptedException
056: */
057: private Object[] tryPool(GenericObjectPool pool)
058: throws InterruptedException {
059: BlockerObjectFactory factory = new BlockerObjectFactory();
060: pool.setFactory(factory);
061: pool.setMaxActive(1);
062: List borrowOrder = Collections
063: .synchronizedList(new LinkedList());
064: for (int i = 0; i < 10; i++) {
065: Contender c = new Contender(borrowOrder);
066: c.pool = pool;
067: c.ordinal = i;
068: (new Thread(c)).start();
069: Thread.sleep(500);
070: }
071: factory.single.release();
072: Thread.sleep(5000);
073: return borrowOrder.toArray();
074: }
075:
076: class Contender implements Runnable {
077: public GenericObjectPool pool;
078: public int ordinal;
079: public List reportList;
080:
081: public Contender(List borrowOrder) {
082: reportList = borrowOrder;
083: }
084:
085: public void run() {
086: try {
087: Blocker block = (Blocker) pool.borrowObject();
088: System.out.println("borrowed #" + ordinal);
089: reportList.add(new Integer(ordinal));
090: block.acquire();
091: System.out.println("returning #" + ordinal);
092: pool.returnObject(block);
093: } catch (Exception e) {
094: throw new RuntimeException(e);
095: }
096: }
097:
098: }
099:
100: class BlockerObjectFactory extends BasePoolableObjectFactory {
101: public Blocker single = new Blocker();
102:
103: public Object makeObject() throws Exception {
104: System.out.println("makeObject");
105: return single;
106: }
107: }
108:
109: class Blocker {
110: boolean block = true;
111:
112: public synchronized void acquire() {
113: // only block first time through
114: if (block) {
115: try {
116: wait();
117: } catch (InterruptedException e) {
118: throw new RuntimeException(e);
119: }
120: }
121: block = false;
122: }
123:
124: public synchronized void release() {
125: notifyAll();
126: }
127: }
128: }
|