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 1/2] 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) From 0fad295d471061bda13ebcfaf925a381d6aeb386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 19 May 2015 18:15:17 -0700 Subject: [PATCH 2/2] Add unit test for test_parallel decorator --- skimage/_shared/tests/test_testing.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/skimage/_shared/tests/test_testing.py b/skimage/_shared/tests/test_testing.py index a8b662bb..0ad15942 100644 --- a/skimage/_shared/tests/test_testing.py +++ b/skimage/_shared/tests/test_testing.py @@ -3,7 +3,7 @@ import numpy as np from nose.tools import (assert_true, assert_raises, assert_equal) -from skimage._shared.testing import doctest_skip_parser +from skimage._shared.testing import doctest_skip_parser, test_parallel def test_skipper(): @@ -84,5 +84,27 @@ def test_skipper(): assert_raises(NameError, doctest_skip_parser, c) +def test_test_parallel(): + state = [] + + @test_parallel() + def change_state1(): + state.append(None) + change_state1() + assert len(state) == 2 + + @test_parallel(num_threads=1) + def change_state2(): + state.append(None) + change_state2() + assert len(state) == 3 + + @test_parallel(num_threads=3) + def change_state3(): + state.append(None) + change_state3() + assert len(state) == 6 + + if __name__ == '__main__': np.testing.run_module_suite()