Debian Codegraph @

tests/watchers.py

raw
1 from queue import Queue, Empty
2 from threading import Thread, Event
3
4 from invoke import Responder, FailingResponder, ResponseNotAccepted
5
6
7 # NOTE: StreamWatcher is basically just an interface/protocol; no behavior to
8 # test of its own. So this file tests Responder primarily, and some subclasses.
9
10
11 class Responder_:
12 def keeps_track_of_seen_index_per_thread(self):
13 # Instantiate a single object which will be used in >1 thread
14 r = Responder(pattern="foo", response="bar fight") # meh
15
16 # Thread body func allowing us to mimic actual IO thread behavior, with
17 # Queues used in place of actual pipes/files
18 def body(responder, in_q, out_q, finished):
19 while not finished.is_set():
20 try:
21 # NOTE: use nowait() so our loop is hot & can shutdown ASAP
22 # if finished gets set.
23 stream = in_q.get_nowait()
24 for response in r.submit(stream):
25 out_q.put_nowait(response)
26 except Empty:
27 pass
28
29 # Create two threads from that body func, and queues/etc for each
30 t1_in, t1_out, t1_finished = Queue(), Queue(), Event()
31 t2_in, t2_out, t2_finished = Queue(), Queue(), Event()
32 t1 = Thread(target=body, args=(r, t1_in, t1_out, t1_finished))
33 t2 = Thread(target=body, args=(r, t2_in, t2_out, t2_finished))
34 # Start the threads
35 t1.start()
36 t2.start()
37 try:
38 stream = "foo fighters"
39 # First thread will basically always work
40 t1_in.put(stream)
41 assert t1_out.get() == "bar fight"
42 # Second thread get() will block/timeout if threadlocals aren't in
43 # use, because the 2nd thread's copy of the responder will not have
44 # its own index & will thus already be 'past' the `foo` in the
45 # stream.
46 t2_in.put(stream)
47 assert t2_out.get(timeout=1) == "bar fight"
48 except Empty:
49 assert (
50 False
51 ), "Unable to read from thread 2 - implies threadlocal indices are broken!" # noqa
52 # Close up.
53 finally:
54 t1_finished.set()
55 t2_finished.set()
56 t1.join()
57 t2.join()
58
59 def yields_response_when_regular_string_pattern_seen(self):
60 r = Responder(pattern="empty", response="handed")
61 assert list(r.submit("the house was empty")) == ["handed"]
62
63 def yields_response_when_regex_seen(self):
64 r = Responder(pattern=r"tech.*debt", response="pay it down")
65 response = r.submit("technically, it's still debt")
66 assert list(response) == ["pay it down"]
67
68 def multiple_hits_within_stream_yield_multiple_responses(self):
69 r = Responder(pattern="jump", response="how high?")
70 assert list(r.submit("jump, wait, jump, wait")) == ["how high?"] * 2
71
72 def patterns_span_multiple_lines(self):
73 r = Responder(pattern=r"call.*problem", response="So sorry")
74 output = """
75 You only call me
76 when you have a problem
77 You never call me
78 Just to say hi
79 """
80 assert list(r.submit(output)) == ["So sorry"]
81
82
83 class FailingResponder_:
84 def behaves_like_regular_responder_by_default(self):
85 r = FailingResponder(
86 pattern="ju[^ ]{2}", response="how high?", sentinel="lolnope"
87 )
88 assert list(r.submit("jump, wait, jump, wait")) == ["how high?"] * 2
89
90 def raises_failure_exception_when_sentinel_detected(self):
91 r = FailingResponder(
92 pattern="ju[^ ]{2}", response="how high?", sentinel="lolnope"
93 )
94 # Behaves normally initially
95 assert list(r.submit("jump")) == ["how high?"]
96 # But then!
97 try:
98 r.submit("lolnope")
99 except ResponseNotAccepted as e:
100 message = str(e)
101 # Expect useful bits in exception text
102 err = "Didn't see pattern in {!r}".format(message)
103 assert "ju[^ ]{2}" in message, err
104 err = "Didn't see failure sentinel in {!r}".format(message)
105 assert "lolnope" in message, err
106 else:
107 assert False, "Did not raise ResponseNotAccepted!"