Merge "Add close to swiftclient.client.Connection"
This commit is contained in:
		| @@ -1072,6 +1072,14 @@ class Connection(object): | ||||
|         self.insecure = insecure | ||||
|         self.ssl_compression = ssl_compression | ||||
|  | ||||
|     def close(self): | ||||
|         if self.http_conn and type(self.http_conn) is tuple\ | ||||
|                 and len(self.http_conn) > 1: | ||||
|             conn = self.http_conn[1] | ||||
|             if hasattr(conn, 'close') and callable(conn.close): | ||||
|                 conn.close() | ||||
|                 self.http_conn = None | ||||
|  | ||||
|     def get_auth(self): | ||||
|         return get_auth(self.authurl, self.user, self.key, | ||||
|                         snet=self.snet, | ||||
|   | ||||
| @@ -918,5 +918,30 @@ class TestLogging(MockHttpTest): | ||||
|             http_conn=conn, headers=headers) | ||||
|  | ||||
|  | ||||
| class TestCloseConnection(MockHttpTest): | ||||
|  | ||||
|     def test_close_none(self): | ||||
|         c.http_connection = self.fake_http_connection(200) | ||||
|         conn = c.Connection('http://www.test.com', 'asdf', 'asdf') | ||||
|         self.assertEqual(conn.http_conn, None) | ||||
|         conn.close() | ||||
|         self.assertEqual(conn.http_conn, None) | ||||
|  | ||||
|     def test_close_ok(self): | ||||
|         url = 'http://www.test.com' | ||||
|         c.http_connection = self.fake_http_connection(200) | ||||
|         conn = c.Connection(url, 'asdf', 'asdf') | ||||
|         self.assertEqual(conn.http_conn, None) | ||||
|  | ||||
|         conn.http_conn = c.http_connection(url) | ||||
|         self.assertEqual(type(conn.http_conn), tuple) | ||||
|         self.assertEqual(len(conn.http_conn), 2) | ||||
|         http_conn_obj = conn.http_conn[1] | ||||
|         self.assertEqual(http_conn_obj.isclosed(), False) | ||||
|         conn.close() | ||||
|         self.assertEqual(http_conn_obj.isclosed(), True) | ||||
|         self.assertEqual(conn.http_conn, None) | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     testtools.main() | ||||
|   | ||||
| @@ -56,6 +56,16 @@ def fake_http_connect(*code_iter, **kwargs): | ||||
|             self.etag = etag | ||||
|             self.body = body | ||||
|             self.timestamp = timestamp | ||||
|             self._is_closed = True | ||||
|  | ||||
|         def connect(self): | ||||
|             self._is_closed = False | ||||
|  | ||||
|         def close(self): | ||||
|             self._is_closed = True | ||||
|  | ||||
|         def isclosed(self): | ||||
|             return self._is_closed | ||||
|  | ||||
|         def getresponse(self): | ||||
|             if kwargs.get('raise_exc'): | ||||
| @@ -132,7 +142,9 @@ def fake_http_connect(*code_iter, **kwargs): | ||||
|         timestamp = timestamps_iter.next() | ||||
|         if status <= 0: | ||||
|             raise HTTPException() | ||||
|         return FakeConn(status, etag, body=kwargs.get('body', ''), | ||||
|         fake_conn = FakeConn(status, etag, body=kwargs.get('body', ''), | ||||
|                              timestamp=timestamp) | ||||
|         fake_conn.connect() | ||||
|         return fake_conn | ||||
|  | ||||
|     return connect | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jenkins
					Jenkins