1 | // |
2 | // SessionPoolTest.cpp |
3 | // |
4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "SessionPoolTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SQL/SessionPool.h" |
15 | #include "Poco/SQL/SessionPoolContainer.h" |
16 | #include "Poco/Thread.h" |
17 | #include "Poco/AutoPtr.h" |
18 | #include "Poco/Exception.h" |
19 | #include "Connector.h" |
20 | |
21 | |
22 | using namespace Poco::SQL::Keywords; |
23 | using Poco::Thread; |
24 | using Poco::AutoPtr; |
25 | using Poco::NotFoundException; |
26 | using Poco::InvalidAccessException; |
27 | using Poco::SQL::Session; |
28 | using Poco::SQL::SessionPool; |
29 | using Poco::SQL::SessionPoolContainer; |
30 | using Poco::SQL::SessionPoolExhaustedException; |
31 | using Poco::SQL::SessionPoolExistsException; |
32 | using Poco::SQL::SessionUnavailableException; |
33 | |
34 | |
35 | SessionPoolTest::SessionPoolTest(const std::string& name): CppUnit::TestCase(name) |
36 | { |
37 | Poco::SQL::Test::Connector::addToFactory(); |
38 | } |
39 | |
40 | |
41 | SessionPoolTest::~SessionPoolTest() |
42 | { |
43 | Poco::SQL::Test::Connector::removeFromFactory(); |
44 | } |
45 | |
46 | |
47 | void SessionPoolTest::testSessionPool() |
48 | { |
49 | SessionPool pool("test" , "cs" , 1, 4, 2); |
50 | |
51 | pool.setFeature("f1" , true); |
52 | assertTrue (pool.getFeature("f1" )); |
53 | try { pool.getFeature("g1" ); fail ("must fail" ); } |
54 | catch ( Poco::NotFoundException& ) { } |
55 | |
56 | pool.setProperty("p1" , 1); |
57 | assertTrue (1 == Poco::AnyCast<int>(pool.getProperty("p1" ))); |
58 | try { pool.getProperty("r1" ); fail ("must fail" ); } |
59 | catch ( Poco::NotFoundException& ) { } |
60 | |
61 | assertTrue (pool.capacity() == 4); |
62 | assertTrue (pool.allocated() == 0); |
63 | assertTrue (pool.idle() == 0); |
64 | assertTrue (pool.available() == 4); |
65 | assertTrue (pool.dead() == 0); |
66 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
67 | Session s1(pool.get()); |
68 | |
69 | assertTrue (s1.getFeature("f1" )); |
70 | assertTrue (1 == Poco::AnyCast<int>(s1.getProperty("p1" ))); |
71 | |
72 | try { pool.setFeature("f1" , true); fail ("must fail" ); } |
73 | catch ( Poco::InvalidAccessException& ) { } |
74 | |
75 | try { pool.setProperty("p1" , 1); fail ("must fail" ); } |
76 | catch ( Poco::InvalidAccessException& ) { } |
77 | |
78 | assertTrue (pool.capacity() == 4); |
79 | assertTrue (pool.allocated() == 1); |
80 | assertTrue (pool.idle() == 0); |
81 | assertTrue (pool.available() == 3); |
82 | assertTrue (pool.dead() == 0); |
83 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
84 | |
85 | Session s2(pool.get("f1" , false)); |
86 | assertTrue (!s2.getFeature("f1" )); |
87 | assertTrue (1 == Poco::AnyCast<int>(s2.getProperty("p1" ))); |
88 | |
89 | assertTrue (pool.capacity() == 4); |
90 | assertTrue (pool.allocated() == 2); |
91 | assertTrue (pool.idle() == 0); |
92 | assertTrue (pool.available() == 2); |
93 | assertTrue (pool.dead() == 0); |
94 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
95 | |
96 | { |
97 | Session s3(pool.get("p1" , 2)); |
98 | assertTrue (s3.getFeature("f1" )); |
99 | assertTrue (2 == Poco::AnyCast<int>(s3.getProperty("p1" ))); |
100 | |
101 | assertTrue (pool.capacity() == 4); |
102 | assertTrue (pool.allocated() == 3); |
103 | assertTrue (pool.idle() == 0); |
104 | assertTrue (pool.available() == 1); |
105 | assertTrue (pool.dead() == 0); |
106 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
107 | } |
108 | |
109 | assertTrue (pool.capacity() == 4); |
110 | assertTrue (pool.allocated() == 3); |
111 | assertTrue (pool.idle() == 1); |
112 | assertTrue (pool.available() == 2); |
113 | assertTrue (pool.dead() == 0); |
114 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
115 | |
116 | Session s4(pool.get()); |
117 | assertTrue (s4.getFeature("f1" )); |
118 | assertTrue (1 == Poco::AnyCast<int>(s4.getProperty("p1" ))); |
119 | |
120 | assertTrue (pool.capacity() == 4); |
121 | assertTrue (pool.allocated() == 3); |
122 | assertTrue (pool.idle() == 0); |
123 | assertTrue (pool.available() == 1); |
124 | assertTrue (pool.dead() == 0); |
125 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
126 | |
127 | Session s5(pool.get()); |
128 | |
129 | assertTrue (pool.capacity() == 4); |
130 | assertTrue (pool.allocated() == 4); |
131 | assertTrue (pool.idle() == 0); |
132 | assertTrue (pool.available() == 0); |
133 | assertTrue (pool.dead() == 0); |
134 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
135 | |
136 | try |
137 | { |
138 | Session s6(pool.get()); |
139 | fail("pool exhausted - must throw" ); |
140 | } |
141 | catch (SessionPoolExhaustedException&) { } |
142 | |
143 | s5.close(); |
144 | assertTrue (pool.capacity() == 4); |
145 | assertTrue (pool.allocated() == 4); |
146 | assertTrue (pool.idle() == 1); |
147 | assertTrue (pool.available() == 1); |
148 | assertTrue (pool.dead() == 0); |
149 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
150 | |
151 | try |
152 | { |
153 | s5 << "DROP TABLE IF EXISTS Test" , now; |
154 | fail("session unusable - must throw" ); |
155 | } |
156 | catch (SessionUnavailableException&) { } |
157 | |
158 | s4.close(); |
159 | assertTrue (pool.capacity() == 4); |
160 | assertTrue (pool.allocated() == 4); |
161 | assertTrue (pool.idle() == 2); |
162 | assertTrue (pool.available() == 2); |
163 | assertTrue (pool.dead() == 0); |
164 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
165 | |
166 | Thread::sleep(5000); // time to clean up idle sessions |
167 | |
168 | assertTrue (pool.capacity() == 4); |
169 | assertTrue (pool.allocated() == 2); |
170 | assertTrue (pool.idle() == 0); |
171 | assertTrue (pool.available() == 2); |
172 | assertTrue (pool.dead() == 0); |
173 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
174 | |
175 | Session s6(pool.get()); |
176 | |
177 | assertTrue (pool.capacity() == 4); |
178 | assertTrue (pool.allocated() == 3); |
179 | assertTrue (pool.idle() == 0); |
180 | assertTrue (pool.available() == 1); |
181 | assertTrue (pool.dead() == 0); |
182 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
183 | |
184 | s6.setFeature("connected" , false); |
185 | assertTrue (pool.dead() == 1); |
186 | |
187 | s6.close(); |
188 | assertTrue (pool.capacity() == 4); |
189 | assertTrue (pool.allocated() == 2); |
190 | assertTrue (pool.idle() == 0); |
191 | assertTrue (pool.available() == 2); |
192 | assertTrue (pool.dead() == 0); |
193 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
194 | |
195 | assertTrue (pool.isActive()); |
196 | pool.shutdown(); |
197 | assertTrue (!pool.isActive()); |
198 | try |
199 | { |
200 | Session s7(pool.get()); |
201 | fail("pool shut down - must throw" ); |
202 | } |
203 | catch (InvalidAccessException&) { } |
204 | |
205 | assertTrue (pool.capacity() == 4); |
206 | assertTrue (pool.allocated() == 0); |
207 | assertTrue (pool.idle() == 0); |
208 | assertTrue (pool.available() == 0); |
209 | assertTrue (pool.dead() == 0); |
210 | assertTrue (pool.allocated() == pool.used() + pool.idle()); |
211 | } |
212 | |
213 | |
214 | void SessionPoolTest::testSessionPoolContainer() |
215 | { |
216 | SessionPoolContainer spc; |
217 | AutoPtr<SessionPool> pPool = new SessionPool("TeSt" , "Cs" ); |
218 | spc.add(pPool); |
219 | assertTrue (pPool->isActive()); |
220 | assertTrue (spc.isActive("test" , "cs" )); |
221 | assertTrue (spc.isActive("test:///cs" )); |
222 | assertTrue (spc.has("test:///cs" )); |
223 | assertTrue (1 == spc.count()); |
224 | |
225 | Poco::SQL::Session sess = spc.get("test:///cs" ); |
226 | assertTrue ("test" == sess.impl()->connectorName()); |
227 | assertTrue ("Cs" == sess.impl()->connectionString()); |
228 | assertTrue ("test:///Cs" == sess.uri()); |
229 | |
230 | try { spc.add(pPool); fail ("must fail" ); } |
231 | catch (SessionPoolExistsException&) { } |
232 | pPool->shutdown(); |
233 | assertTrue (!pPool->isActive()); |
234 | assertTrue (!spc.isActive("test" , "cs" )); |
235 | assertTrue (!spc.isActive("test:///cs" )); |
236 | spc.remove(pPool->name()); |
237 | assertTrue (!spc.has("test:///cs" )); |
238 | assertTrue (!spc.isActive("test" , "cs" )); |
239 | assertTrue (!spc.isActive("test:///cs" )); |
240 | assertTrue (0 == spc.count()); |
241 | try { spc.get("test" ); fail ("must fail" ); } |
242 | catch (NotFoundException&) { } |
243 | |
244 | spc.add("tEsT" , "cs" ); |
245 | spc.add("TeSt" , "cs" );//duplicate request, must be silently ignored |
246 | assertTrue (1 == spc.count()); |
247 | spc.remove("TesT:///cs" ); |
248 | assertTrue (0 == spc.count()); |
249 | try { spc.get("test" ); fail ("must fail" ); } |
250 | catch (NotFoundException&) { } |
251 | } |
252 | |
253 | |
254 | void SessionPoolTest::setUp() |
255 | { |
256 | } |
257 | |
258 | |
259 | void SessionPoolTest::tearDown() |
260 | { |
261 | } |
262 | |
263 | |
264 | CppUnit::Test* SessionPoolTest::suite() |
265 | { |
266 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SessionPoolTest" ); |
267 | |
268 | CppUnit_addTest(pSuite, SessionPoolTest, testSessionPool); |
269 | CppUnit_addTest(pSuite, SessionPoolTest, testSessionPoolContainer); |
270 | |
271 | return pSuite; |
272 | } |
273 | |