6 from eventlet.support import six
10 base_module_contents = """
13 print("base {0} {1}".format(socket, urllib))
16 patching_module_contents = """
17 from eventlet.green import socket
18 from eventlet.green import urllib
19 from eventlet import patcher
20 print('patcher {0} {1}'.format(socket, urllib))
21 patcher.inject('base', globals(), ('socket', socket), ('urllib', urllib))
25 import_module_contents = """
28 print("importing {0} {1} {2} {3}".format(patching, socket, patching.socket, patching.urllib))
32 class ProcessBase(tests.LimitedTestCase):
33 TEST_TIMEOUT = 3 # starting processes is time-consuming
36 super(ProcessBase, self).setUp()
37 self._saved_syspath = sys.path
38 self.tempdir = tempfile.mkdtemp('_patcher_test')
41 super(ProcessBase, self).tearDown()
42 sys.path = self._saved_syspath
43 shutil.rmtree(self.tempdir)
45 def write_to_tempfile(self, name, contents):
46 filename = os.path.join(self.tempdir, name)
47 if not filename.endswith('.py'):
48 filename = filename + '.py'
49 with open(filename, "w") as fd:
52 def launch_subprocess(self, filename):
53 path = os.path.join(self.tempdir, filename)
54 output = tests.run_python(path)
56 output = output.decode('utf-8')
60 lines = output.split(separator)
63 def run_script(self, contents, modname=None):
66 self.write_to_tempfile(modname, contents)
67 return self.launch_subprocess(modname)
70 class ImportPatched(ProcessBase):
71 def test_patch_a_module(self):
72 self.write_to_tempfile("base", base_module_contents)
73 self.write_to_tempfile("patching", patching_module_contents)
74 self.write_to_tempfile("importing", import_module_contents)
75 output, lines = self.launch_subprocess('importing.py')
76 assert lines[0].startswith('patcher'), repr(output)
77 assert lines[1].startswith('base'), repr(output)
78 assert lines[2].startswith('importing'), repr(output)
79 assert 'eventlet.green.socket' in lines[1], repr(output)
80 assert 'eventlet.green.urllib' in lines[1], repr(output)
81 assert 'eventlet.green.socket' in lines[2], repr(output)
82 assert 'eventlet.green.urllib' in lines[2], repr(output)
83 assert 'eventlet.green.httplib' not in lines[2], repr(output)
85 def test_import_patched_defaults(self):
86 self.write_to_tempfile("base", """
89 import urllib.request as urllib
92 print("base {0} {1}".format(socket, urllib))""")
95 from eventlet import patcher
96 base = patcher.import_patched('base')
97 print("newmod {0} {1} {2}".format(base, base.socket, base.urllib.socket.socket))
99 self.write_to_tempfile("newmod", new_mod)
100 output, lines = self.launch_subprocess('newmod.py')
101 assert lines[0].startswith('base'), repr(output)
102 assert lines[1].startswith('newmod'), repr(output)
103 assert 'eventlet.green.socket' in lines[1], repr(output)
104 assert 'GreenSocket' in lines[1], repr(output)
107 class MonkeyPatch(ProcessBase):
108 def test_patched_modules(self):
110 from eventlet import patcher
111 patcher.monkey_patch()
114 import urllib.request as urllib
117 print("newmod {0} {1}".format(socket.socket, urllib.socket.socket))
119 self.write_to_tempfile("newmod", new_mod)
120 output, lines = self.launch_subprocess('newmod.py')
121 assert lines[0].startswith('newmod'), repr(output)
122 self.assertEqual(lines[0].count('GreenSocket'), 2, repr(output))
124 def test_early_patching(self):
126 from eventlet import patcher
127 patcher.monkey_patch()
132 self.write_to_tempfile("newmod", new_mod)
133 output, lines = self.launch_subprocess('newmod.py')
134 self.assertEqual(len(lines), 2, repr(output))
135 assert lines[0].startswith('newmod'), repr(output)
137 def test_late_patching(self):
141 from eventlet import patcher
142 patcher.monkey_patch()
146 self.write_to_tempfile("newmod", new_mod)
147 output, lines = self.launch_subprocess('newmod.py')
148 self.assertEqual(len(lines), 2, repr(output))
149 assert lines[0].startswith('newmod'), repr(output)
151 def test_typeerror(self):
153 from eventlet import patcher
154 patcher.monkey_patch(finagle=True)
156 self.write_to_tempfile("newmod", new_mod)
157 output, lines = self.launch_subprocess('newmod.py')
158 assert lines[-2].startswith('TypeError'), repr(output)
159 assert 'finagle' in lines[-2], repr(output)
161 def assert_boolean_logic(self, call, expected, not_expected=''):
162 expected_list = ", ".join(['"%s"' % x for x in expected.split(',') if len(x)])
163 not_expected_list = ", ".join(['"%s"' % x for x in not_expected.split(',') if len(x)])
165 from eventlet import patcher
168 assert patcher.is_monkey_patched(mod), mod
170 assert not patcher.is_monkey_patched(mod), mod
171 print("already_patched {0}".format(",".join(sorted(patcher.already_patched.keys()))))
172 """ % (call, expected_list, not_expected_list)
173 self.write_to_tempfile("newmod", new_mod)
174 output, lines = self.launch_subprocess('newmod.py')
175 ap = 'already_patched'
176 assert lines[0].startswith(ap), repr(output)
177 patched_modules = lines[0][len(ap):].strip()
178 # psycopg might or might not be patched based on installed modules
179 patched_modules = patched_modules.replace("psycopg,", "")
181 patched_modules = patched_modules.replace("MySQLdb,", "")
183 patched_modules, expected,
184 "Logic:%s\nExpected: %s != %s" % (call, expected, patched_modules))
186 def test_boolean(self):
187 self.assert_boolean_logic("patcher.monkey_patch()",
188 'os,select,socket,thread,time')
190 def test_boolean_all(self):
191 self.assert_boolean_logic("patcher.monkey_patch(all=True)",
192 'os,select,socket,thread,time')
194 def test_boolean_all_single(self):
195 self.assert_boolean_logic("patcher.monkey_patch(all=True, socket=True)",
196 'os,select,socket,thread,time')
198 def test_boolean_all_negative(self):
199 self.assert_boolean_logic(
200 "patcher.monkey_patch(all=False, socket=False, select=True)",
203 def test_boolean_single(self):
204 self.assert_boolean_logic("patcher.monkey_patch(socket=True)",
207 def test_boolean_double(self):
208 self.assert_boolean_logic("patcher.monkey_patch(socket=True, select=True)",
211 def test_boolean_negative(self):
212 self.assert_boolean_logic("patcher.monkey_patch(socket=False)",
213 'os,select,thread,time')
215 def test_boolean_negative2(self):
216 self.assert_boolean_logic("patcher.monkey_patch(socket=False, time=False)",
219 def test_conflicting_specifications(self):
220 self.assert_boolean_logic("patcher.monkey_patch(socket=False, select=True)",
224 test_monkey_patch_threading = """
225 def test_monkey_patch_threading():
229 from eventlet.support import six
230 for i in six.moves.range(1000):
235 tpool.execute(time.sleep, 0.5)
238 w1 = eventlet.spawn(do_sleep)
241 assert tickcount[0] > 900
246 class Tpool(ProcessBase):
249 @tests.skip_with_pyevent
250 def test_simple(self):
253 from eventlet import patcher
254 patcher.monkey_patch()
255 from eventlet import tpool
256 print("newmod {0}".format(tpool.execute(len, "hi")))
257 print("newmod {0}".format(tpool.execute(len, "hi2")))
260 self.write_to_tempfile("newmod", new_mod)
261 output, lines = self.launch_subprocess('newmod.py')
262 self.assertEqual(len(lines), 3, output)
263 assert lines[0].startswith('newmod'), repr(output)
264 assert '2' in lines[0], repr(output)
265 assert '3' in lines[1], repr(output)
267 @tests.skip_with_pyevent
268 def test_unpatched_thread(self):
269 new_mod = """import eventlet
270 eventlet.monkey_patch(time=False, thread=False)
271 from eventlet import tpool
274 new_mod += test_monkey_patch_threading
275 new_mod += "\ntest_monkey_patch_threading()\n"
276 self.write_to_tempfile("newmod", new_mod)
277 output, lines = self.launch_subprocess('newmod.py')
278 self.assertEqual(len(lines), 2, lines)
280 @tests.skip_with_pyevent
281 def test_patched_thread(self):
282 new_mod = """import eventlet
283 eventlet.monkey_patch(time=False, thread=True)
284 from eventlet import tpool
287 new_mod += test_monkey_patch_threading
288 new_mod += "\ntest_monkey_patch_threading()\n"
289 self.write_to_tempfile("newmod", new_mod)
290 output, lines = self.launch_subprocess('newmod.py')
291 self.assertEqual(len(lines), 2, "\n".join(lines))
294 class Subprocess(ProcessBase):
295 def test_monkeypatched_subprocess(self):
296 new_mod = """import eventlet
297 eventlet.monkey_patch()
298 from eventlet.green import subprocess
300 subprocess.Popen(['true'], stdin=subprocess.PIPE)
303 self.write_to_tempfile("newmod", new_mod)
304 output, lines = self.launch_subprocess('newmod')
305 self.assertEqual(output, "done\n", output)
308 class Threading(ProcessBase):
309 def test_orig_thread(self):
310 new_mod = """import eventlet
311 eventlet.monkey_patch()
312 from eventlet import patcher
314 _threading = patcher.original('threading')
316 print(repr(threading.currentThread()))
317 t = _threading.Thread(target=test)
320 print(len(threading._active))
321 print(len(_threading._active))
323 self.write_to_tempfile("newmod", new_mod)
324 output, lines = self.launch_subprocess('newmod')
325 self.assertEqual(len(lines), 4, "\n".join(lines))
326 assert lines[0].startswith('<Thread'), lines[0]
327 self.assertEqual(lines[1], "1", lines[1])
328 self.assertEqual(lines[2], "1", lines[2])
330 def test_threading(self):
331 new_mod = """import eventlet
332 eventlet.monkey_patch()
335 print(repr(threading.currentThread()))
336 t = threading.Thread(target=test)
339 print(len(threading._active))
341 self.write_to_tempfile("newmod", new_mod)
342 output, lines = self.launch_subprocess('newmod')
343 self.assertEqual(len(lines), 3, "\n".join(lines))
344 assert lines[0].startswith('<_MainThread'), lines[0]
345 self.assertEqual(lines[1], "1", lines[1])
347 def test_tpool(self):
348 new_mod = """import eventlet
349 eventlet.monkey_patch()
350 from eventlet import tpool
353 print(repr(threading.currentThread()))
355 print(len(threading._active))
357 self.write_to_tempfile("newmod", new_mod)
358 output, lines = self.launch_subprocess('newmod')
359 self.assertEqual(len(lines), 3, "\n".join(lines))
360 assert lines[0].startswith('<Thread'), lines[0]
361 self.assertEqual(lines[1], "1", lines[1])
363 def test_greenlet(self):
364 new_mod = """import eventlet
365 eventlet.monkey_patch()
366 from eventlet import event
370 print(repr(threading.currentThread()))
372 eventlet.spawn_n(test)
374 print(len(threading._active))
376 self.write_to_tempfile("newmod", new_mod)
377 output, lines = self.launch_subprocess('newmod')
378 self.assertEqual(len(lines), 3, "\n".join(lines))
379 assert lines[0].startswith('<_MainThread'), lines[0]
380 self.assertEqual(lines[1], "1", lines[1])
382 def test_greenthread(self):
383 new_mod = """import eventlet
384 eventlet.monkey_patch()
387 print(repr(threading.currentThread()))
388 t = eventlet.spawn(test)
390 print(len(threading._active))
392 self.write_to_tempfile("newmod", new_mod)
393 output, lines = self.launch_subprocess('newmod')
394 self.assertEqual(len(lines), 3, "\n".join(lines))
395 assert lines[0].startswith('<_GreenThread'), lines[0]
396 self.assertEqual(lines[1], "1", lines[1])
398 def test_keyerror(self):
399 new_mod = """import eventlet
400 eventlet.monkey_patch()
402 self.write_to_tempfile("newmod", new_mod)
403 output, lines = self.launch_subprocess('newmod')
404 self.assertEqual(len(lines), 1, "\n".join(lines))
407 class Os(ProcessBase):
408 def test_waitpid(self):
409 new_mod = """import subprocess
411 eventlet.monkey_patch(all=False, os=True)
412 process = subprocess.Popen("sleep 0.1 && false", shell=True)
413 print(process.wait())"""
414 self.write_to_tempfile("newmod", new_mod)
415 output, lines = self.launch_subprocess('newmod')
416 self.assertEqual(len(lines), 2, "\n".join(lines))
417 self.assertEqual('1', lines[0], repr(output))
420 class GreenThreadWrapper(ProcessBase):
421 prologue = """import eventlet
422 eventlet.monkey_patch()
425 t = threading.currentThread()
428 t = eventlet.spawn(test)
433 self.write_to_tempfile("newmod", self.prologue + """
436 t2 = threading.currentThread()
437 eventlet.spawn(test2)
438 """ + self.epilogue + """
442 output, lines = self.launch_subprocess('newmod')
443 self.assertEqual(len(lines), 2, "\n".join(lines))
444 assert lines[0].startswith('<_GreenThread'), lines[0]
447 self.write_to_tempfile("newmod", self.prologue + """
460 output, lines = self.launch_subprocess('newmod')
461 self.assertEqual(len(lines), 10, "\n".join(lines))
462 for i in range(0, 3):
463 self.assertEqual(lines[i], "GreenThread-1", lines[i])
464 for i in range(3, 6):
465 self.assertEqual(lines[i], "foo", lines[i])
466 for i in range(6, 9):
467 self.assertEqual(lines[i], "bar", lines[i])
469 def test_ident(self):
470 self.write_to_tempfile("newmod", self.prologue + """
474 output, lines = self.launch_subprocess('newmod')
475 self.assertEqual(len(lines), 3, "\n".join(lines))
476 self.assertEqual(lines[0], lines[1])
478 def test_is_alive(self):
479 self.write_to_tempfile("newmod", self.prologue + """
483 output, lines = self.launch_subprocess('newmod')
484 self.assertEqual(len(lines), 3, "\n".join(lines))
485 self.assertEqual(lines[0], "True", lines[0])
486 self.assertEqual(lines[1], "True", lines[1])
488 def test_is_daemon(self):
489 self.write_to_tempfile("newmod", self.prologue + """
493 output, lines = self.launch_subprocess('newmod')
494 self.assertEqual(len(lines), 3, "\n".join(lines))
495 self.assertEqual(lines[0], "True", lines[0])
496 self.assertEqual(lines[1], "True", lines[1])
499 def test_importlib_lock():
500 tests.run_isolated('patcher_importlib_lock.py')
503 def test_threading_condition():
504 tests.run_isolated('patcher_threading_condition.py')
507 def test_threading_join():
508 tests.run_isolated('patcher_threading_join.py')