Merge ~cjwatson/launchpad:simplify-librarian-thread-state-test into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 890103b1219cd3c144f3c0b3c2e40d83d63d9d87
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:simplify-librarian-thread-state-test
Merge into: launchpad:master
Diff against target: 62 lines (+10/-29)
1 file modified
lib/lp/services/librarian/tests/test_client.py (+10/-29)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+442915@code.launchpad.net

Commit message

Simplify LibrarianClientTestCase.test_thread_state_FileUploadClient

Description of the change

This needs the ability to call a function in a thread and propagate exceptions back to the caller. Since we can now rely on Python >= 3.2, `concurrent.futures` is a much simpler way to do this.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/librarian/tests/test_client.py b/lib/lp/services/librarian/tests/test_client.py
2index 1d06673..39f6ee8 100644
3--- a/lib/lp/services/librarian/tests/test_client.py
4+++ b/lib/lp/services/librarian/tests/test_client.py
5@@ -10,6 +10,7 @@ import socket
6 import textwrap
7 import threading
8 import unittest
9+from concurrent.futures import ThreadPoolExecutor
10 from urllib.error import HTTPError, URLError
11 from urllib.request import urlopen
12
13@@ -42,29 +43,6 @@ from lp.testing.layers import (
14 from lp.testing.views import create_webservice_error_view
15
16
17-class PropagatingThread(threading.Thread):
18- """Thread class that propagates errors to the parent."""
19-
20- # https://stackoverflow.com/a/31614591
21- def run(self):
22- self.exc = None
23- try:
24- if hasattr(self, "_Thread__target"):
25- # Thread uses name mangling prior to Python 3.
26- self.ret = self._Thread__target(
27- *self._Thread__args, **self._Thread__kwargs
28- )
29- else:
30- self.ret = self._target(*self._args, **self._kwargs)
31- except BaseException as e:
32- self.exc = e
33-
34- def join(self):
35- super().join()
36- if self.exc:
37- raise self.exc
38-
39-
40 class InstrumentedLibrarianClient(LibrarianClient):
41 def __init__(self, *args, **kwargs):
42 super().__init__(*args, **kwargs)
43@@ -598,12 +576,15 @@ class LibrarianClientTestCase(TestCase):
44
45 def test_thread_state_FileUploadClient(self):
46 client = InstrumentedLibrarianClient()
47- th = PropagatingThread(
48- target=client.addFile,
49- args=("sample.txt", 6, io.BytesIO(b"sample"), "text/plain"),
50- )
51- th.start()
52- th.join()
53+ with ThreadPoolExecutor(max_workers=1) as executor:
54+ future = executor.submit(
55+ client.addFile,
56+ "sample.txt",
57+ 6,
58+ io.BytesIO(b"sample"),
59+ "text/plain",
60+ )
61+ future.result()
62 self.assertEqual(5, client.check_error_calls)
63
64

Subscribers

People subscribed via source and target branches

to status/vote changes: