1
2
3
4
5
6
7 package org.mule.transport;
8
9 import org.mule.api.config.ThreadingProfile;
10 import org.mule.api.endpoint.OutboundEndpoint;
11 import org.mule.api.transport.MessageDispatcher;
12 import org.mule.config.ImmutableThreadingProfile;
13 import org.mule.tck.junit4.AbstractMuleContextTestCase;
14 import org.mule.tck.testmodels.mule.TestConnector;
15
16 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
17 import org.junit.Test;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21
22 public class DispatcherPoolTestCase extends AbstractMuleContextTestCase
23 {
24
25 @Test
26 public void testDefaultDispatcherPoolConfiguration() throws Exception
27 {
28 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_RUN);
29
30
31 assertEquals(ThreadingProfile.WHEN_EXHAUSTED_RUN, connector.getDispatcherThreadingProfile()
32 .getPoolExhaustedAction());
33 assertEquals(2, connector.dispatchers.getMaxActive());
34
35
36 assertEquals(2, connector.dispatchers.getMaxIdle());
37 assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK,
38 connector.dispatchers.getWhenExhaustedAction());
39 assertEquals(-1, connector.dispatchers.getMaxWait());
40 }
41
42 @Test
43 public void testDefaultDispatcherPoolConfigurationThreadingProfileWait() throws Exception
44 {
45 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
46
47 assertEquals(ThreadingProfile.WHEN_EXHAUSTED_WAIT, connector.getDispatcherThreadingProfile()
48 .getPoolExhaustedAction());
49 assertEquals(1, connector.dispatchers.getMaxActive());
50 assertEquals(1, connector.dispatchers.getMaxIdle());
51 assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK,
52 connector.dispatchers.getWhenExhaustedAction());
53 assertEquals(-1, connector.dispatchers.getMaxWait());
54 }
55
56 @Test
57 public void testDispatcherPoolDefaultBlockExhaustedAction() throws Exception
58 {
59 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
60 connector.setDispatcherPoolMaxWait(100);
61
62 assertEquals(1, connector.dispatchers.getMaxActive());
63 assertEquals(100, connector.dispatchers.getMaxWait());
64
65 final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
66
67 new Thread(new Runnable()
68 {
69 public void run()
70 {
71 try
72 {
73 MessageDispatcher messageDispatcher = (MessageDispatcher) connector.dispatchers.borrowObject(endpoint);
74 Thread.sleep(50);
75 connector.dispatchers.returnObject(endpoint, messageDispatcher);
76 }
77 catch (Exception e)
78 {
79 e.printStackTrace();
80 }
81
82 }
83 }).start();
84 Thread.sleep(10);
85 assertEquals(1, connector.dispatchers.getNumActive());
86 connector.dispatchers.borrowObject(endpoint);
87 assertEquals(1, connector.dispatchers.getNumActive());
88
89 }
90
91 @Test
92 public void testDispatcherPoolBlockTimeoutExhaustedAction() throws Exception
93 {
94 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
95 connector.setDispatcherPoolMaxWait(10);
96
97 assertEquals(1, connector.dispatchers.getMaxActive());
98 assertEquals(10, connector.dispatchers.getMaxWait());
99
100 final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
101
102 new Thread(new Runnable()
103 {
104 public void run()
105 {
106 try
107 {
108 MessageDispatcher messageDispatcher = (MessageDispatcher) connector.dispatchers.borrowObject(endpoint);
109 Thread.sleep(200);
110 connector.dispatchers.returnObject(endpoint, messageDispatcher);
111 }
112 catch (Exception e)
113 {
114 e.printStackTrace();
115 }
116
117 }
118 }).start();
119 Thread.sleep(10);
120 assertEquals(1, connector.dispatchers.getNumActive());
121 try
122 {
123 connector.dispatchers.borrowObject(endpoint);
124 fail("Exception expected");
125 }
126 catch (Exception e)
127 {
128 assertEquals(1, connector.dispatchers.getNumActive());
129 }
130 }
131
132 @Test
133 public void testDispatcherPoolGrowExhaustedAction() throws Exception
134 {
135 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
136 connector.setDispatcherPoolWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
137
138 assertEquals(1, connector.dispatchers.getMaxActive());
139
140 final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
141
142 connector.dispatchers.borrowObject(endpoint);
143 connector.dispatchers.borrowObject(endpoint);
144 assertEquals(2, connector.dispatchers.getNumActive());
145
146 }
147
148 @Test
149 public void testDispatcherPoolFailExhaustedAction() throws Exception
150 {
151 final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
152 connector.setDispatcherPoolWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
153
154 assertEquals(1, connector.dispatchers.getMaxActive());
155
156 final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
157
158 connector.dispatchers.borrowObject(endpoint);
159 try
160 {
161 connector.dispatchers.borrowObject(endpoint);
162 fail("Exception expected");
163 }
164 catch (Exception e)
165 {
166 assertEquals(1, connector.dispatchers.getNumActive());
167 }
168 }
169
170 private TestConnector createConnectorWithSingleObjectDispatcherPool(int exhaustedAction) throws Exception
171 {
172 TestConnector connector = new TestConnector(muleContext);
173 ThreadingProfile threadingProfile = new ImmutableThreadingProfile(1, 1, 1, 1, 1, exhaustedAction,
174 true, null, null);
175 connector.setDispatcherThreadingProfile(threadingProfile);
176 connector.createReceiver(getTestService(), getTestInboundEndpoint("test", "test://test"));
177 muleContext.getRegistry().registerConnector(connector);
178 return connector;
179 }
180
181 }