mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Remove ConnectionRemover class that tried to get rid of vcr connections in ConnectionPools.
This commit is contained in:
50
vcr/patch.py
50
vcr/patch.py
@@ -123,12 +123,6 @@ class CassettePatcherBuilder(object):
|
|||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
return
|
return
|
||||||
from .stubs.requests_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection
|
from .stubs.requests_stubs import VCRRequestsHTTPConnection, VCRRequestsHTTPSConnection
|
||||||
http_connection_remover = ConnectionRemover(
|
|
||||||
self._get_cassette_subclass(VCRHTTPConnection)
|
|
||||||
)
|
|
||||||
https_connection_remover = ConnectionRemover(
|
|
||||||
self._get_cassette_subclass(VCRHTTPSConnection)
|
|
||||||
)
|
|
||||||
mock_triples = (
|
mock_triples = (
|
||||||
(cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection),
|
(cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection),
|
||||||
(cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection),
|
(cpool, 'VerifiedHTTPSConnection', VCRRequestsHTTPSConnection),
|
||||||
@@ -140,11 +134,8 @@ class CassettePatcherBuilder(object):
|
|||||||
# connections of the appropriate type.
|
# connections of the appropriate type.
|
||||||
(cpool.HTTPConnectionPool, '_get_conn', self._patched_get_conn(cpool.HTTPConnectionPool)),
|
(cpool.HTTPConnectionPool, '_get_conn', self._patched_get_conn(cpool.HTTPConnectionPool)),
|
||||||
(cpool.HTTPSConnectionPool, '_get_conn', self._patched_get_conn(cpool.HTTPSConnectionPool)),
|
(cpool.HTTPSConnectionPool, '_get_conn', self._patched_get_conn(cpool.HTTPSConnectionPool)),
|
||||||
(cpool.HTTPConnectionPool, '_new_conn', self._patched_new_conn(cpool.HTTPConnectionPool, http_connection_remover)),
|
|
||||||
(cpool.HTTPSConnectionPool, '_new_conn', self._patched_new_conn(cpool.HTTPConnectionPool, https_connection_remover))
|
|
||||||
)
|
)
|
||||||
return itertools.chain(self._build_patchers_from_mock_triples(mock_triples),
|
return self._build_patchers_from_mock_triples(mock_triples)
|
||||||
(http_connection_remover, https_connection_remover))
|
|
||||||
|
|
||||||
def _patched_get_conn(self, connection_pool_class):
|
def _patched_get_conn(self, connection_pool_class):
|
||||||
get_conn = connection_pool_class._get_conn
|
get_conn = connection_pool_class._get_conn
|
||||||
@@ -156,15 +147,6 @@ class CassettePatcherBuilder(object):
|
|||||||
return connection
|
return connection
|
||||||
return patched_get_conn
|
return patched_get_conn
|
||||||
|
|
||||||
def _patched_new_conn(self, connection_pool_class, connection_remover):
|
|
||||||
new_conn = connection_pool_class._new_conn
|
|
||||||
@functools.wraps(new_conn)
|
|
||||||
def patched_new_conn(pool):
|
|
||||||
new_connection = new_conn(pool)
|
|
||||||
connection_remover.add_connection_to_pool_entry(pool, new_connection)
|
|
||||||
return new_connection
|
|
||||||
return patched_new_conn
|
|
||||||
|
|
||||||
@_build_patchers_from_mock_triples_decorator
|
@_build_patchers_from_mock_triples_decorator
|
||||||
def _urllib3(self):
|
def _urllib3(self):
|
||||||
try:
|
try:
|
||||||
@@ -203,36 +185,6 @@ class CassettePatcherBuilder(object):
|
|||||||
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
|
yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection
|
||||||
|
|
||||||
|
|
||||||
class ConnectionRemover(object):
|
|
||||||
|
|
||||||
def __init__(self, connection_class):
|
|
||||||
self._connection_class = connection_class
|
|
||||||
self._connection_pool_to_connections = {}
|
|
||||||
|
|
||||||
def add_connection_to_pool_entry(self, pool, connection):
|
|
||||||
if isinstance(connection, self._connection_class):
|
|
||||||
self._connection_pool_to_connection.setdefault(pool, set()).add(connection)
|
|
||||||
|
|
||||||
def remove_connection_to_pool_entry(self, pool, connection):
|
|
||||||
if isinstance(connection, self._connection_class):
|
|
||||||
self._connection_pool_to_connection[self._connection_class].remove(connection)
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
for pool, connections in self._connection_pool_to_connections.items():
|
|
||||||
readd_connections = []
|
|
||||||
while pool.not_empty() and connections:
|
|
||||||
connection = pool.get()
|
|
||||||
if isinstance(connection, self._connection_class):
|
|
||||||
connections.remove(connection)
|
|
||||||
else:
|
|
||||||
readd_connections.append(connection)
|
|
||||||
for connection in readd_connections:
|
|
||||||
self.pool._put_conn(connection)
|
|
||||||
|
|
||||||
|
|
||||||
def reset_patchers():
|
def reset_patchers():
|
||||||
yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
|
yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
|
||||||
yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)
|
yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)
|
||||||
|
|||||||
Reference in New Issue
Block a user