1
2
3
4
5
6
7 package org.mule.util.pool;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.object.ObjectFactory;
11 import org.mule.config.PoolingProfile;
12 import org.mule.tck.testmodels.fruit.BananaFactory;
13
14 import java.util.NoSuchElementException;
15
16 import org.junit.Test;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 public class CommonsPoolObjectPoolTestCase extends AbstractPoolingTestCase
23 {
24 @Test
25 public void testPoolExhaustedFail() throws Exception
26 {
27 ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_FAIL);
28 assertEquals(0, pool.getNumActive());
29
30 borrowObjectsUntilPoolIsFull(pool);
31
32
33 try
34 {
35 pool.borrowObject();
36 fail("borrowing an object from a pool with policy WHEN_EXHAUSTED_FAIL must fail");
37 }
38 catch (NoSuchElementException nse)
39 {
40
41 }
42 }
43
44 @Test
45 public void testPoolExhaustedGrow() throws Exception
46 {
47 ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_GROW);
48 assertEquals(0, pool.getNumActive());
49
50 borrowObjectsUntilPoolIsFull(pool);
51
52
53 pool.borrowObject();
54 assertEquals(MAX_ACTIVE + 1, pool.getNumActive());
55 }
56
57 @Test
58 public void testPoolExhaustedWait() throws Exception
59 {
60 ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_WAIT);
61 assertEquals(0, pool.getNumActive());
62
63 borrowObjectsUntilPoolIsFull(pool);
64
65
66 long before = System.currentTimeMillis();
67 try
68 {
69 pool.borrowObject();
70 fail("WHEN_EXHAUSTED_WAIT was specified but the pool returned an object");
71 }
72 catch (NoSuchElementException nse)
73 {
74 long delta = System.currentTimeMillis() - before;
75 assertTrue(delta >= MAX_WAIT);
76 }
77 }
78
79 @Test
80 public void testInitPoolNone() throws Exception
81 {
82 ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_NONE);
83 CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
84 assertEquals(0, objectFactory.getInstanceCount());
85 }
86
87 @Test
88 public void testInitPoolOne() throws Exception
89 {
90 ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_ONE);
91 CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
92 assertEquals(1, objectFactory.getInstanceCount());
93 }
94
95 @Test
96 public void testInitPoolAll() throws Exception
97 {
98 ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_ALL);
99 CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
100 assertEquals(MAX_ACTIVE, objectFactory.getInstanceCount());
101 }
102
103 private ObjectPool createPoolWithExhaustedAction(int exhaustedAction) throws Exception
104 {
105 PoolingProfile poolingProfile = createDefaultPoolingProfile();
106 poolingProfile.setExhaustedAction(exhaustedAction);
107
108 ObjectFactory objectFactory = new BananaFactory();
109 return createPool(poolingProfile, objectFactory);
110 }
111
112 private ObjectPool createPoolWithInitialisationPolicy(int initPolicy) throws Exception
113 {
114 PoolingProfile poolingProfile = createDefaultPoolingProfile();
115 poolingProfile.setInitialisationPolicy(initPolicy);
116
117 ObjectFactory objectFactory = new CountingObjectFactory();
118 return createPool(poolingProfile, objectFactory);
119 }
120
121 private ObjectPool createPool(PoolingProfile poolingProfile, ObjectFactory objectFactory) throws Exception
122 {
123 CommonsPoolObjectPool pool = new CommonsPoolObjectPool(objectFactory, poolingProfile, muleContext);
124 pool.initialise();
125 return pool;
126 }
127
128 private void borrowObjectsUntilPoolIsFull(ObjectPool pool) throws Exception
129 {
130 for (int i = 1; i <= MAX_ACTIVE; i++)
131 {
132 pool.borrowObject();
133 assertEquals(i, pool.getNumActive());
134 }
135 }
136
137 private static class CountingObjectFactory extends BananaFactory
138 {
139 private int instanceCount = 0;
140
141 @Override
142 public Object getInstance(MuleContext muleContext) throws Exception
143 {
144 instanceCount++;
145 return super.getInstance(muleContext);
146 }
147
148 public int getInstanceCount()
149 {
150 return instanceCount;
151 }
152 }
153 }