Debian Codegraph @

tests/concurrency.py

raw
1 from queue import Queue
2
3 from invoke.util import ExceptionWrapper, ExceptionHandlingThread as EHThread
4
5
6 # TODO: rename
7 class ExceptionHandlingThread_:
8 class via_target:
9 def setup_method(self):
10 def worker(q):
11 q.put(7)
12
13 self.worker = worker
14
15 def base_case(self):
16 queue = Queue()
17 t = EHThread(target=self.worker, args=[queue])
18 t.start()
19 t.join()
20 assert queue.get(block=False) == 7
21 assert queue.empty()
22
23 def catches_exceptions(self):
24 # Induce exception by submitting a bad queue obj
25 t = EHThread(target=self.worker, args=[None])
26 t.start()
27 t.join()
28 wrapper = t.exception()
29 assert isinstance(wrapper, ExceptionWrapper)
30 assert wrapper.kwargs == {"args": [None], "target": self.worker}
31 assert wrapper.type == AttributeError
32 assert isinstance(wrapper.value, AttributeError)
33
34 def exhibits_is_dead_flag(self):
35 # Spin up a thread that will except internally (can't put() on a
36 # None object)
37 t = EHThread(target=self.worker, args=[None])
38 t.start()
39 t.join()
40 # Excepted -> it's dead
41 assert t.is_dead
42 # Spin up a happy thread that can exit peacefully (it's not "dead",
43 # though...maybe we should change that terminology)
44 t = EHThread(target=self.worker, args=[Queue()])
45 t.start()
46 t.join()
47 # Not dead, just uh...sleeping?
48 assert not t.is_dead
49
50 class via_subclassing:
51 def setup_method(self):
52 class MyThread(EHThread):
53 def __init__(self, *args, **kwargs):
54 self.queue = kwargs.pop("queue")
55 super().__init__(*args, **kwargs)
56
57 def _run(self):
58 self.queue.put(7)
59
60 self.klass = MyThread
61
62 def base_case(self):
63 queue = Queue()
64 t = self.klass(queue=queue)
65 t.start()
66 t.join()
67 assert queue.get(block=False) == 7
68 assert queue.empty()
69
70 def catches_exceptions(self):
71 # Induce exception by submitting a bad queue obj
72 t = self.klass(queue=None)
73 t.start()
74 t.join()
75 wrapper = t.exception()
76 assert isinstance(wrapper, ExceptionWrapper)
77 assert wrapper.kwargs == {}
78 assert wrapper.type == AttributeError
79 assert isinstance(wrapper.value, AttributeError)
80
81 def exhibits_is_dead_flag(self):
82 # Spin up a thread that will except internally (can't put() on a
83 # None object)
84 t = self.klass(queue=None)
85 t.start()
86 t.join()
87 # Excepted -> it's dead
88 assert t.is_dead
89 # Spin up a happy thread that can exit peacefully (it's not "dead",
90 # though...maybe we should change that terminology)
91 t = self.klass(queue=Queue())
92 t.start()
93 t.join()
94 # Not dead, just uh...sleeping?
95 assert not t.is_dead