Commit f8eea8ec authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Unittest ResponseManager

parent 7fa5c263
......@@ -162,11 +162,16 @@ class RequestManager(object):
url=str(self.path),
headers=self.headers,
body=self.data)
while True:
keep_trying = 60.0
while keep_trying > 0:
try:
return conn.getresponse()
except ResponseNotReady:
sleep(0.03 * random())
wait = 0.03 * random()
sleep(wait)
keep_trying -= wait
recvlog('Kamaki Timeout %s %s\t[%s]' % (self.method, self.path, self))
raise ClientError('HTTPResponse takes too long - kamaki timeout')
class ResponseManager(object):
......@@ -206,8 +211,7 @@ class ResponseManager(object):
raise ClientError(
'Failed while http-connecting to %s (%s)' % (
self.request.url,
err),
1000)
err))
@property
def status_code(self):
......@@ -246,7 +250,7 @@ class ResponseManager(object):
try:
return loads(self._content)
except ValueError as err:
ClientError('Response not formated in JSON - %s' % err)
raise ClientError('Response not formated in JSON - %s' % err)
class SilentEvent(Thread):
......
......@@ -121,6 +121,92 @@ class RequestManager(TestCase):
self.assertEqual(req.headers, headers)
self.assertRaises(AssertionError, self.RM, 'GOT', '', '', '', {}, {})
@patch('httplib.HTTPConnection.getresponse')
@patch('httplib.HTTPConnection.request')
def test_perform(self, request, getresponse):
from httplib import HTTPConnection
self.RM('GET', 'http://example.com', '/').perform(
HTTPConnection('http', 'example.com'))
expected = dict(body=None, headers={}, url='/', method='GET')
request.assert_called_once_with(**expected)
getresponse.assert_called_once_with()
class FakeResp(object):
READ = 'something to read'
HEADERS = dict(k='v', k1='v1', k2='v2')
reason = 'some reason'
status = 42
def read(self):
return self.READ
def getheaders(self):
return self.HEADERS.items()
class ResponseManager(TestCase):
def setUp(self):
from kamaki.clients import ResponseManager, RequestManager
from httplib import HTTPConnection
self.RM = ResponseManager(RequestManager('GET', 'http://ok', '/'))
self.HTTPC = HTTPConnection
def tearDown(self):
FakeResp.READ = 'something to read'
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_content(self, perform):
self.assertEqual(self.RM.content, FakeResp.READ)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_text(self, perform):
self.assertEqual(self.RM.text, FakeResp.READ)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_status(self, perform):
self.assertEqual(self.RM.status, FakeResp.reason)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_status_code(self, perform):
self.assertEqual(self.RM.status_code, FakeResp.status)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_headers(self, perform):
self.assertEqual(self.RM.headers, FakeResp.HEADERS)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_json(self, perform):
try:
self.RM.json
except Exception as e:
self.assertEqual(
'%s' % e,
'Response not formated in JSON - '
'No JSON object could be decoded\n')
from json import dumps
FakeResp.READ = dumps(FakeResp.HEADERS)
self.RM._request_performed = False
self.assertEqual(self.RM.json, FakeResp.HEADERS)
self.assertTrue(isinstance(perform.call_args[0][0], self.HTTPC))
@patch('kamaki.clients.RequestManager.perform', return_value=FakeResp())
def test_all(self, perform):
self.assertEqual(self.RM.content, FakeResp.READ)
self.assertEqual(self.RM.text, FakeResp.READ)
self.assertEqual(self.RM.status, FakeResp.reason)
self.assertEqual(self.RM.status_code, FakeResp.status)
self.assertEqual(self.RM.headers, FakeResp.HEADERS)
perform.assert_called_only_once
class SilentEvent(TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment