Merge lp:~ack/landscape-client/multi-curl-test-fix into lp:~landscape/landscape-client/trunk

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 822
Merged at revision: 822
Proposed branch: lp:~ack/landscape-client/multi-curl-test-fix
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 48 lines (+16/-9)
1 file modified
landscape/lib/tests/test_fetch.py (+16/-9)
To merge this branch: bzr merge lp:~ack/landscape-client/multi-curl-test-fix
Reviewer Review Type Date Requested Status
Adam Collard (community) Approve
Free Ekanayaka (community) Approve
Review via email: mp+261355@code.launchpad.net

Commit message

Fix a flaky CURL-related test.
The MultiCurlStub wasn't really thread-safe, so it could break when used with fetch_many_async.

Description of the change

Fix a flaky CURL-related test.
The MultiCurlStub wasn't really thread-safe, so it could break when used with fetch_many_async.

To post a comment you must log in.
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

+1

review: Approve
Revision history for this message
Adam Collard (adam-collard) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/lib/tests/test_fetch.py'
2--- landscape/lib/tests/test_fetch.py 2014-12-04 09:48:07 +0000
3+++ landscape/lib/tests/test_fetch.py 2015-06-08 08:21:25 +0000
4@@ -1,4 +1,5 @@
5 import os
6+from threading import local
7
8 import pycurl
9
10@@ -54,23 +55,29 @@
11 else:
12 body = result[0]
13 http_code = result[1]
14- self.curls[url] = CurlStub(body, {pycurl.HTTP_CODE: http_code})
15- self.current = None
16+ self.curls[url] = CurlStub(
17+ result=body, infos={pycurl.HTTP_CODE: http_code})
18+
19+ # Use thread local storage to keep the current CurlStub since
20+ # CurlManyStub is passed to multiple threads, but the state needs to be
21+ # local.
22+ self._local = local()
23+ self._local.current = None
24
25 def getinfo(self, what):
26- if not self.current.performed:
27+ if not self._local.current.performed:
28 raise AssertionError("getinfo() can't be called before perform()")
29- result = self.current.getinfo(what)
30- self.current = None
31+ result = self._local.current.getinfo(what)
32+ self._local.current = None
33 return result
34
35 def setopt(self, option, value):
36- if option is pycurl.URL:
37- self.current = self.curls[value]
38- self.current.setopt(option, value)
39+ if option == pycurl.URL:
40+ self._local.current = self.curls[value]
41+ self._local.current.setopt(option, value)
42
43 def perform(self):
44- self.current.perform()
45+ self._local.current.perform()
46
47
48 class Any(object):

Subscribers

People subscribed via source and target branches

to all changes: