def endheaders(self, *args, **kwargs): if self._current_connection is None: raise httplib.ImproperConnectionState() try: return self._current_connection.endheaders(*args, **kwargs) except (httplib.HTTPException, IOError): self._current_connection = None raise
def getresponse(self): if self._current_connection is None: raise httplib.ImproperConnectionState() conn = self._current_connection self._current_connection = None self._busy_connections.append(conn) try: resp = conn.getresponse() return ResponseWrapper(resp, lambda: self._recycle(conn)) except httplib.HTTPException: self._busy_connections.remove(conn) raise
def _start_connection(self): if self._current_connection is not None: raise httplib.ImproperConnectionState() try: self._current_connection = self._free_connections.pop() except IndexError: if len(self._busy_connections) >= self._pool_size: raise NoFreeConnections() #print 'create new connection (%d busy)' % len(self._busy_connections) self._current_connection = self._connection_class(*self._conn_args, **self._conn_kwargs) self._current_connection.connect() # if we have set the debug level, set it on new connections too if self._debug_level: self._current_connection.set_debug_level = self._debug_level
def test_no_reconnect_recurse_to_infinity(self): # retry uses recursion when a reconnect is necessary # this test makes sure it stops after 1 recursive call sp = servermanager.ServerPool() with mock.patch(HTTPCON) as conmock: rv = conmock.return_value # hash header must be string instead of mock object rv.getresponse.return_value.getheader.return_value = 'HASH' sp.servers[0].capabilities = ['keep-alive'] sp.servers[0].rest_call('GET', '/first') # after retrying once, the rest call should raise the # exception up rv.request.side_effect = httplib.ImproperConnectionState() self.assertRaises(httplib.ImproperConnectionState, sp.servers[0].rest_call, *('GET', '/second')) # 1 for the first call, 2 for the second with retry self.assertEqual(rv.request.call_count, 3)
def test_reconnect_cached_connection(self): sp = servermanager.ServerPool() with mock.patch(HTTPCON) as conmock: rv = conmock.return_value rv.getresponse.return_value.getheader.return_value = 'HASH' sp.servers[0].capabilities = ['keep-alive'] sp.servers[0].rest_call('GET', '/first') # raise an error on re-use to verify reconnect # return okay the second time so the reconnect works rv.request.side_effect = [httplib.ImproperConnectionState(), mock.MagicMock()] sp.servers[0].rest_call('GET', '/second') uris = [c[1][1] for c in rv.request.mock_calls] expected = [ sp.base_uri + '/first', sp.base_uri + '/second', sp.base_uri + '/second', ] self.assertEqual(uris, expected)
def send(self, *args, **kwargs): if self._current_connection is None: raise httplib.ImproperConnectionState() return self._current_connection.send(*args, **kwargs)