From 13a6f4955cd3bbcd147e9a08ad085427b7bdb8d0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 3 Sep 2017 11:10:56 +0300 Subject: [PATCH] multiprocessing: tests: Turn into proper tests, make CPython compatible. --- multiprocessing/test_pipe.py | 13 ++++++++----- multiprocessing/test_pool.py | 2 +- multiprocessing/test_pool_async.py | 22 ++++++++++++++-------- multiprocessing/test_process.py | 2 +- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/multiprocessing/test_pipe.py b/multiprocessing/test_pipe.py index 84591d96..4e3d5643 100644 --- a/multiprocessing/test_pipe.py +++ b/multiprocessing/test_pipe.py @@ -1,6 +1,6 @@ import sys import os -from multiprocessing import Process, Pipe, Connection +from multiprocessing import Process, Pipe def f(conn): conn.send([42, None, 'hello']) @@ -9,11 +9,14 @@ def f(conn): if __name__ == '__main__': parent_conn, child_conn = Pipe(False) - print(parent_conn, child_conn) + #print(parent_conn, child_conn) p = Process(target=f, args=(child_conn,)) + # Extension: need to call this for uPy - p.register_pipe(parent_conn, child_conn) + if sys.implementation.name == "micropython": + p.register_pipe(parent_conn, child_conn) + p.start() - print(parent_conn.recv()) - print(parent_conn.recv()) + parent_conn.recv() == [42, None, 'hello'] + parent_conn.recv() == [42, 42, 42] p.join() diff --git a/multiprocessing/test_pool.py b/multiprocessing/test_pool.py index dcb89a76..cb98e6ef 100644 --- a/multiprocessing/test_pool.py +++ b/multiprocessing/test_pool.py @@ -4,4 +4,4 @@ def f(x): return x*x pool = Pool(4) -print(pool.apply(f, (10,))) +assert pool.apply(f, (10,)) == 100 diff --git a/multiprocessing/test_pool_async.py b/multiprocessing/test_pool_async.py index f489b2ac..1c481689 100644 --- a/multiprocessing/test_pool_async.py +++ b/multiprocessing/test_pool_async.py @@ -6,19 +6,21 @@ def f(x): pool = Pool(4) future = pool.apply_async(f, (10,)) -print(future.get()) +assert future.get() == 100 def f2(x): - time.sleep(1) + time.sleep(0.5) return x + 1 future = pool.apply_async(f2, (10,)) +iter = 0 while not future.ready(): - print("not ready") - time.sleep(0.2) - -print(future.get()) + #print("not ready") + time.sleep(0.1) + iter += 1 +assert future.get() == 11 +assert iter >= 5 and iter <= 8 t = time.time() futs = [ @@ -27,6 +29,7 @@ futs = [ pool.apply_async(f2, (12,)), ] +iter = 0 while True: #not all(futs): c = 0 @@ -35,7 +38,10 @@ while True: c += 1 if not c: break - print("not ready2") - time.sleep(0.2) + #print("not ready2") + time.sleep(0.1) + iter += 1 + +assert iter >= 5 and iter <= 8 print("Run 3 parallel sleep(1)'s in: ", time.time() - t) diff --git a/multiprocessing/test_process.py b/multiprocessing/test_process.py index 16a6a2ab..d4b14517 100644 --- a/multiprocessing/test_process.py +++ b/multiprocessing/test_process.py @@ -1,7 +1,7 @@ from multiprocessing import Process def f(name): - print('hello', name) + assert name == 'bob' if __name__ == '__main__': p = Process(target=f, args=('bob',))