mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-19 11:27:45 +08:00
Merge pull request #1517 from ahojnnes/parallel-decoration
Add decorator to test parallel execution of unit tests
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user