Commit 34285a1b authored by Luke Macken's avatar Luke Macken

Fix our multi-threaded unit tests

parent 13241381
...@@ -47,8 +47,8 @@ class TestCodeInjection(unittest.TestCase): ...@@ -47,8 +47,8 @@ class TestCodeInjection(unittest.TestCase):
os.unlink(program) os.unlink(program)
def test_many_payloads_into_program_with_many_threads(self): def test_many_payloads_into_program_with_many_threads(self):
program = generate_program(threads=50) program = generate_program(threads=25)
num_payloads = 50 num_payloads = 25
try: try:
for exe in interpreters(): for exe in interpreters():
p = run_program(program, exe=exe) p = run_program(program, exe=exe)
......
...@@ -30,21 +30,20 @@ def generate_program(threads=1): ...@@ -30,21 +30,20 @@ def generate_program(threads=1):
import os, time, threading import os, time, threading
running = True running = True
pidfile = '/tmp/pyrasite_%d' % os.getpid() pidfile = '/tmp/pyrasite_%d' % os.getpid()
open(pidfile, 'w').close()
def cpu_bound(): def cpu_bound():
i = 2 i = 0
y = 0
def fib(n):
return fib(n - 1) + fib(n - 2)
while running: while running:
y += fib(i)
i += 1 i += 1
while os.path.exists(pidfile):
time.sleep(0.1)
""") """)
# CPU-bound threads # CPU-bound threads
for t in range(threads): for t in range(threads):
script += "threading.Thread(target=cpu_bound).start()\n" script += "threading.Thread(target=cpu_bound).start()\n"
script += "open(pidfile, 'w').close()\n" script += textwrap.dedent("""
while os.path.exists(pidfile):
time.sleep(0.1)
running = False
""")
tmp.write(script) tmp.write(script)
tmp.close() tmp.close()
return filename return filename
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment