Debian Codegraph @

tests/debug_test.py

raw
1 import io
2 import os
3 import sys
4
5 from eventlet import debug
6 import tests
7 import eventlet
8
9
10 class TestSpew(tests.LimitedTestCase):
11 def setUp(self):
12 self.orig_trace = sys.settrace
13 sys.settrace = self._settrace
14 self.tracer = None
15
16 def tearDown(self):
17 sys.settrace = self.orig_trace
18 sys.stdout = sys.__stdout__
19
20 def _settrace(self, cb):
21 self.tracer = cb
22
23 def test_spew(self):
24 debug.spew()
25 assert isinstance(self.tracer, debug.Spew)
26
27 def test_unspew(self):
28 debug.spew()
29 debug.unspew()
30 assert self.tracer is None
31
32 def test_line(self):
33 frame_str = "f=<frame at"
34
35 sys.stdout = io.StringIO()
36 s = debug.Spew()
37 f = sys._getframe()
38 s(f, "line", None)
39 lineno = f.f_lineno - 1 # -1 here since we called with frame f in the line above
40 output = sys.stdout.getvalue()
41 assert "%s:%i" % (__name__, lineno) in output, "Didn't find line %i in %s" % (lineno, output)
42 assert frame_str in output
43
44 def test_line_nofile(self):
45 sys.stdout = io.StringIO()
46 s = debug.Spew()
47 g = globals().copy()
48 del g['__file__']
49 f = eval("sys._getframe()", g)
50 lineno = f.f_lineno
51 s(f, "line", None)
52 output = sys.stdout.getvalue()
53 assert "[unknown]:%i" % lineno in output, "Didn't find [unknown]:%i in %s" % (lineno, output)
54 if "PYTEST_XDIST_WORKER" not in os.environ:
55 assert "VM instruction #" in output, output
56
57 def test_line_global(self):
58 frame_str = "f=<frame at"
59
60 global GLOBAL_VAR
61 sys.stdout = io.StringIO()
62 GLOBAL_VAR = debug.Spew()
63 f = sys._getframe()
64 GLOBAL_VAR(f, "line", None)
65 lineno = f.f_lineno - 1 # -1 here since we called with frame f in the line above
66 output = sys.stdout.getvalue()
67 assert "%s:%i" % (__name__, lineno) in output, "Didn't find line %i in %s" % (lineno, output)
68 assert frame_str in output
69 assert "GLOBAL_VAR" in f.f_globals
70 assert "GLOBAL_VAR=<eventlet.debug.Spew object at" in output
71 del GLOBAL_VAR
72
73 def test_line_novalue(self):
74 sys.stdout = io.StringIO()
75 s = debug.Spew(show_values=False)
76 f = sys._getframe()
77 s(f, "line", None)
78 lineno = f.f_lineno - 1 # -1 here since we called with frame f in the line above
79 output = sys.stdout.getvalue()
80 assert "%s:%i" % (__name__, lineno) in output, "Didn't find line %i in %s" % (lineno, output)
81 assert "f=<frame object at" not in output
82
83 def test_line_nooutput(self):
84 sys.stdout = io.StringIO()
85 s = debug.Spew(trace_names=['foo'])
86 f = sys._getframe()
87 s(f, "line", None)
88 output = sys.stdout.getvalue()
89 assert output == ""
90
91
92 class TestDebug(tests.LimitedTestCase):
93 def test_everything(self):
94 debug.hub_exceptions(True)
95 debug.hub_exceptions(False)
96 debug.tpool_exceptions(True)
97 debug.tpool_exceptions(False)
98 debug.hub_listener_stacks(True)
99 debug.hub_listener_stacks(False)
100 debug.hub_timer_stacks(True)
101 debug.hub_timer_stacks(False)
102 debug.format_hub_listeners()
103 debug.format_hub_timers()
104
105 def test_hub_exceptions(self):
106 debug.hub_exceptions(True)
107 server = eventlet.listen(('0.0.0.0', 0))
108 client = eventlet.connect(('127.0.0.1', server.getsockname()[1]))
109 client_2, addr = server.accept()
110
111 def hurl(s):
112 s.recv(1)
113 {}[1] # keyerror
114
115 with tests.capture_stderr() as fake:
116 gt = eventlet.spawn(hurl, client_2)
117 eventlet.sleep(0.001)
118 client.send(b' ')
119 eventlet.sleep(0.001)
120 # allow the "hurl" greenlet to trigger the KeyError
121 # not sure why the extra context switch is needed
122 eventlet.sleep(0.001)
123 self.assertRaises(KeyError, gt.wait)
124 debug.hub_exceptions(False)
125 # look for the KeyError exception in the traceback
126 assert 'KeyError: 1' in fake.getvalue(), "Traceback not in:\n" + fake.getvalue()