Merge lp:~facundo/ubuntu-rest-scopes/centralized-urlread-timeout into lp:ubuntu-rest-scopes

Proposed by Facundo Batista
Status: Rejected
Rejected by: Facundo Batista
Proposed branch: lp:~facundo/ubuntu-rest-scopes/centralized-urlread-timeout
Merge into: lp:ubuntu-rest-scopes
Diff against target: 51 lines (+21/-2)
2 files modified
src/rest_scopes.py (+6/-2)
src/tests/test_rest_scopes.py (+15/-0)
To merge this branch: bzr merge lp:~facundo/ubuntu-rest-scopes/centralized-urlread-timeout
Reviewer Review Type Date Requested Status
Ubuntu One hackers Pending
Review via email: mp+256464@code.launchpad.net

Commit message

Have a timeout in centralized urlread.

Description of the change

Have a timeout in centralized urlread.

To post a comment you must log in.

Unmerged revisions

462. By Facundo Batista

Have a timeout in centralized urlread.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/rest_scopes.py'
2--- src/rest_scopes.py 2015-04-13 14:09:26 +0000
3+++ src/rest_scopes.py 2015-04-16 11:18:18 +0000
4@@ -84,7 +84,7 @@
5 return transposed
6
7
8-def urlread(meter, prefix, url, data=None, headers=None):
9+def urlread(meter, prefix, url, data=None, headers=None, timeout=None):
10 """Centralized urlopen with extra functionality."""
11 if headers is None:
12 headers = {}
13@@ -121,7 +121,11 @@
14 tini = time.time()
15 req = urllib2.Request(url, data=data, headers=headers)
16 try:
17- u = urllib2.urlopen(req)
18+ # can't always pass the timeout, as if it's None we'll override module defaults
19+ if timeout is None:
20+ u = urllib2.urlopen(req)
21+ else:
22+ u = urllib2.urlopen(req, timeout=timeout)
23 data = u.read()
24 except urllib2.HTTPError as exc:
25 _inform(str(exc.code), exc.hdrs)
26
27=== modified file 'src/tests/test_rest_scopes.py'
28--- src/tests/test_rest_scopes.py 2015-04-13 14:09:26 +0000
29+++ src/tests/test_rest_scopes.py 2015-04-16 11:18:18 +0000
30@@ -799,6 +799,21 @@
31 self.assertEqual(req.data, None)
32 self.assertEqual(req.headers, {'A': 3})
33
34+ def test_with_timeout(self):
35+ netfh = Mock()
36+ netfh.read.return_value = 'response'
37+ netfh.headers = {}
38+
39+ with patch.object(urllib2, 'urlopen') as mock_urlopen:
40+ mock_urlopen.return_value = netfh
41+ resp = rest_scopes.urlread(None, None, 'http://blah/', timeout=123)
42+
43+ self.assertEqual(resp, 'response')
44+ req = mock_urlopen.call_args[0][0]
45+ self.assertEqual(req.data, None)
46+ kwargs = mock_urlopen.call_args[1]
47+ self.assertEqual(kwargs, {'timeout': 123})
48+
49 def test_metrics_no_prefix(self):
50 netfh = Mock()
51 netfh.read.return_value = 'response'

Subscribers

People subscribed via source and target branches