Merge ~pappacena/launchpad:testfix-librarian-client-race-condition into launchpad:master

Proposed by Thiago F. Pappacena
Status: Merged
Approved by: Thiago F. Pappacena
Approved revision: befb3673b9ac006563e4f571ae60a7904903e0db
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~pappacena/launchpad:testfix-librarian-client-race-condition
Merge into: launchpad:master
Diff against target: 43 lines (+12/-2)
2 files modified
lib/lp/services/librarian/client.py (+2/-1)
lib/lp/services/librarian/tests/test_client.py (+10/-1)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+389162@code.launchpad.net

Commit message

[testfix] Avoiding race condition at librarian client test

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve
befb367... by Thiago F. Pappacena

Setting a smaller timeout at test

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/librarian/client.py b/lib/lp/services/librarian/client.py
2index 737d9e4..ba18805 100644
3--- a/lib/lp/services/librarian/client.py
4+++ b/lib/lp/services/librarian/client.py
5@@ -88,6 +88,7 @@ class FileUploadClient:
6 # it will be shared between threads. The easiest way of making this
7 # class thread safe is by storing all state in a thread local.
8 self.state = threading.local()
9+ self.state.s_poll_timeout = 0
10
11 def _connect(self):
12 """Connect this client.
13@@ -115,7 +116,7 @@ class FileUploadClient:
14 del self.state.f
15
16 def _checkError(self):
17- poll_result = self.state.s_poll.poll(0)
18+ poll_result = self.state.s_poll.poll(self.state.s_poll_timeout)
19 if poll_result:
20 fileno, event = poll_result[0]
21 # Accepts any event that contains input data. Even if we
22diff --git a/lib/lp/services/librarian/tests/test_client.py b/lib/lp/services/librarian/tests/test_client.py
23index 34dce5f..3b406c1 100644
24--- a/lib/lp/services/librarian/tests/test_client.py
25+++ b/lib/lp/services/librarian/tests/test_client.py
26@@ -252,7 +252,16 @@ class LibrarianClientTestCase(TestCase):
27 'librarian', upload_host=upload_host, upload_port=upload_port)
28
29 client = LibrarianClient()
30-
31+ # Artificially increases timeout to avoid race condition.
32+ # The fake server is running in another thread, and we are sure it
33+ # will (eventually) reply with an error message. So, let's just wait
34+ # until that message arrives.
35+ client.state.s_poll_timeout = 120
36+
37+ # Please, note the mismatch between file size (7) and its actual size
38+ # of the content (6). This is intentional, to make sure we are raising
39+ # the error coming from the fake server (and not the local check
40+ # right after, while uploading the file).
41 self.assertRaisesRegex(
42 UploadFailed, 'Server said early: STORE 7 sample.txt',
43 client.addFile, 'sample.txt', 7, StringIO('sample'), 'text/plain')