From 574159f724fe2acf324b5e29d98743b29b9e4f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 19 May 2015 17:57:47 -0700 Subject: [PATCH] Add decorator to test parallel execution of unit test --- skimage/_shared/testing.py | 33 +++++++++++++++++++++++++++ skimage/transform/tests/test_warps.py | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/skimage/_shared/testing.py b/skimage/_shared/testing.py index f202fbff..5069e19e 100644 --- a/skimage/_shared/testing.py +++ b/skimage/_shared/testing.py @@ -3,6 +3,8 @@ import os import re +import threading +import functools from tempfile import NamedTemporaryFile from numpy import testing @@ -201,6 +203,37 @@ def teardown_test(): warnings.simplefilter('default') +def test_parallel(num_threads=2): + """Decorator to run the same function multiple times in parallel. + + Parameters + ---------- + num_threads : int, optional + The number of times the function is run in parallel. + + Notes + ----- + This decorator does not pass the return value of the decorated function. + + """ + + assert num_threads > 0 + + def wrapper(func): + @functools.wraps(func) + def inner(*args, **kwargs): + threads = [] + for i in range(num_threads): + thread = threading.Thread(target=func, args=args, kwargs=kwargs) + threads.append(thread) + for thread in threads: + thread.start() + for thread in threads: + thread.join() + return inner + return wrapper + + if __name__ == '__main__': color_check('pil') mono_check('pil') diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 115f0e6f..afa78da3 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -197,7 +197,7 @@ def test_swirl(): image = img_as_float(data.checkerboard()) swirl_params = {'radius': 80, 'rotation': 0, 'order': 2, 'mode': 'reflect'} - + with expected_warnings(['Bi-quadratic.*bug']): swirled = tf.swirl(image, strength=10, **swirl_params) unswirled = tf.swirl(swirled, strength=-10, **swirl_params)