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