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