Revision 4ab1af1a snf-common/synnefo/lib/pool/tests.py
b/snf-common/synnefo/lib/pool/tests.py | ||
---|---|---|
57 | 57 |
import threading |
58 | 58 |
from collections import defaultdict |
59 | 59 |
|
60 |
from socket import socket, AF_INET, SOCK_STREAM, IPPROTO_TCP, SHUT_RDWR |
|
61 |
|
|
60 | 62 |
from synnefo.lib.pool import ObjectPool, PoolLimitError, PoolVerificationError |
61 |
from synnefo.lib.pool.http import get_http_connection
|
|
63 |
from synnefo.lib.pool.http import PooledHTTPConnection, HTTPConnectionPool
|
|
62 | 64 |
from synnefo.lib.pool.http import _pools as _http_pools |
63 | 65 |
|
64 | 66 |
# Use backported unittest functionality if Python < 2.7 |
... | ... | |
291 | 293 |
|
292 | 294 |
|
293 | 295 |
class TestHTTPConnectionTestCase(unittest.TestCase): |
294 |
def test_double_close(self): |
|
295 |
conn = get_http_connection("127.0.0.1", "http") |
|
296 |
self.assertEqual(conn._pool, _http_pools[("http", "127.0.0.1")]) |
|
297 |
conn.close() |
|
298 |
self.assertIsNone(conn._pool) |
|
299 |
# This call does nothing, because conn._pool is already None |
|
300 |
conn.close() |
|
301 |
self.assertIsNone(conn._pool) |
|
296 |
def setUp(self): |
|
297 |
#netloc = "127.0.0.1:9999" |
|
298 |
#scheme='http' |
|
299 |
#self.pool = HTTPConnectionPool( |
|
300 |
# netloc=netloc, |
|
301 |
# scheme=scheme, |
|
302 |
# pool_size=1) |
|
303 |
#key = (scheme, netloc) |
|
304 |
#_http_pools[key] = pool |
|
305 |
|
|
306 |
_http_pools.clear() |
|
307 |
|
|
308 |
self.host = "127.0.0.1" |
|
309 |
self.port = 9999 |
|
310 |
self.netloc = "%s:%s" % (self.host, self.port) |
|
311 |
self.scheme = "http" |
|
312 |
self.key = (self.scheme, self.netloc) |
|
313 |
|
|
314 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) |
|
315 |
sock.bind((self.host, self.port)) |
|
316 |
sock.listen(1) |
|
317 |
self.sock = sock |
|
318 |
|
|
319 |
def tearDown(self): |
|
320 |
sock = self.sock |
|
321 |
sock.shutdown(SHUT_RDWR) |
|
322 |
sock.close() |
|
323 |
|
|
324 |
def test_double_release(self): |
|
325 |
pooled = PooledHTTPConnection(self.netloc, self.scheme) |
|
326 |
pooled.acquire() |
|
327 |
pool = pooled._pool |
|
328 |
self.assertTrue(pooled._pool is _http_pools[(self.scheme, self.netloc)]) |
|
329 |
pooled.release() |
|
330 |
|
|
331 |
poolsize = len(pool._set) |
|
332 |
|
|
333 |
if PooledHTTPConnection._pool_disable_after_release: |
|
334 |
self.assertTrue(pooled._pool is False) |
|
335 |
|
|
336 |
if not PooledHTTPConnection._pool_ignore_double_release: |
|
337 |
with self.assertRaises(AssertionError): |
|
338 |
pooled.release() |
|
339 |
else: |
|
340 |
pooled.release() |
|
341 |
|
|
342 |
self.assertEqual(poolsize, len(pool._set)) |
|
302 | 343 |
|
303 | 344 |
def test_distinct_pools_per_scheme(self): |
304 |
conn = get_http_connection("127.0.0.1", "http") |
|
305 |
pool = conn._pool |
|
306 |
self.assertTrue(pool is _http_pools[("http", "127.0.0.1")]) |
|
307 |
conn.close() |
|
308 |
conn2 = get_http_connection("127.0.0.1", "https") |
|
309 |
self.assertTrue(conn is not conn2) |
|
310 |
self.assertNotEqual(pool, conn2._pool) |
|
311 |
self.assertTrue(conn2._pool is _http_pools[("https", "127.0.0.1")]) |
|
312 |
conn2.close() |
|
345 |
with PooledHTTPConnection("127.0.0.1", "http", |
|
346 |
attach_context=True) as conn: |
|
347 |
pool = conn._pool_context._pool |
|
348 |
self.assertTrue(pool is _http_pools[("http", "127.0.0.1")]) |
|
349 |
|
|
350 |
with PooledHTTPConnection("127.0.0.1", "https", |
|
351 |
attach_context=True) as conn2: |
|
352 |
pool2 = conn2._pool_context._pool |
|
353 |
self.assertTrue(conn is not conn2) |
|
354 |
self.assertNotEqual(pool, pool2) |
|
355 |
self.assertTrue(pool2 is _http_pools[("https", "127.0.0.1")]) |
|
356 |
|
|
357 |
def test_clean_connection(self): |
|
358 |
pool = None |
|
359 |
pooled = PooledHTTPConnection(self.netloc, self.scheme) |
|
360 |
conn = pooled.acquire() |
|
361 |
pool = pooled._pool |
|
362 |
self.assertTrue(pool is not None) |
|
363 |
pooled.release() |
|
364 |
self.assertTrue(pooled._pool is False) |
|
365 |
poolset = pool._set |
|
366 |
self.assertEqual(len(poolset), 1) |
|
367 |
pooled_conn = list(poolset)[0] |
|
368 |
self.assertTrue(pooled_conn is conn) |
|
369 |
|
|
370 |
def test_dirty_connection(self): |
|
371 |
pooled = PooledHTTPConnection(self.netloc, self.scheme) |
|
372 |
conn = pooled.acquire() |
|
373 |
pool = pooled._pool |
|
374 |
conn.request("GET", "/") |
|
375 |
serversock, addr = self.sock.accept() |
|
376 |
serversock.send("HTTP/1.1 200 OK\n" |
|
377 |
"Content-Length: 6\n" |
|
378 |
"\n" |
|
379 |
"HELLO\n") |
|
380 |
time.sleep(0.3) |
|
381 |
# We would read this message like this |
|
382 |
#resp = conn.getresponse() |
|
383 |
# but we won't so the connection is dirty |
|
384 |
pooled.release() |
|
385 |
|
|
386 |
poolset = pool._set |
|
387 |
self.assertEqual(len(poolset), 0) |
|
388 |
|
|
389 |
def test_context_manager_exception_safety(self): |
|
390 |
class TestError(Exception): |
|
391 |
pass |
|
392 |
|
|
393 |
for i in xrange(10): |
|
394 |
pool = None |
|
395 |
try: |
|
396 |
with PooledHTTPConnection( |
|
397 |
self.netloc, self.scheme, |
|
398 |
size=1, attach_context=True) as conn: |
|
399 |
pool = conn._pool_context._pool |
|
400 |
raise TestError() |
|
401 |
except TestError: |
|
402 |
self.assertTrue(pool is not None) |
|
403 |
self.assertEqual(pool._semaphore._Semaphore__value, 1) |
|
404 |
|
|
313 | 405 |
|
314 | 406 |
if __name__ == '__main__': |
315 | 407 |
unittest.main() |
Also available in: Unified diff