Skip to content

Commit 05d4f54

Browse files
committed
refactor unittest
1 parent 813604d commit 05d4f54

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

ultraplot/tests/test_thread_safety.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import ultraplot as uplt, threading, pytest, warnings
22

33

4-
def modify_rc_in_context(prop: str, value=None):
4+
def modify_rc_on_thread(prop: str, value=None, with_context=True):
55
"""
66
Apply arbitrary rc parameters in a thread-safe manner.
77
"""
8-
with uplt.rc.context(fontsize=value):
8+
if with_context:
9+
with uplt.rc.context(fontsize=value):
10+
assert uplt.rc[prop] == value, f"Thread {id} failed to set rc params"
11+
else:
912
assert uplt.rc[prop] == value, f"Thread {id} failed to set rc params"
1013

1114

12-
def modify_rc_on_thread(prop: str, value=None):
13-
id = threading.get_ident() # set it to thread id
14-
uplt.rc[prop] = value
15-
assert uplt.rc[prop] == value, f"Thread {id} failed to set rc params {prop}={value}"
16-
17-
18-
def _spawn_and_run_threads(func, n=10, **kwargs):
15+
def _spawn_and_run_threads(func, n=30, **kwargs):
1916
options = kwargs.pop("options")
2017
workers = []
2118
exceptions = []
@@ -56,7 +53,9 @@ def test_setting_within_context():
5653
# be local to that context and not affect the main thread.
5754
prop, value = "font.size", uplt.rc["font.size"]
5855
options = list(range(10))
59-
_spawn_and_run_threads(modify_rc_in_context, prop=prop, options=options)
56+
_spawn_and_run_threads(
57+
modify_rc_on_thread, prop=prop, options=options, with_context=True
58+
)
6059
assert uplt.rc[prop] == value
6160

6261

@@ -71,10 +70,14 @@ def test_setting_without_context():
7170
# Test an ultraplot parameter
7271
prop = "abc"
7372
value = uplt.rc[prop]
74-
options = "A. a. aa".split()
75-
_spawn_and_run_threads(modify_rc_on_thread, prop=prop, options=options)
73+
options = "A. a. aa aaa aaaa.".split()
74+
_spawn_and_run_threads(
75+
modify_rc_on_thread, prop=prop, options=options, with_context=False
76+
)
7677
assert uplt.rc[prop] == value
7778

7879
prop, value = "font.size", uplt.rc["font.size"]
7980
options = list(range(10))
80-
_spawn_and_run_threads(modify_rc_on_thread, prop=prop, options=options)
81+
_spawn_and_run_threads(
82+
modify_rc_on_thread, prop=prop, options=options, with_context=False
83+
)

0 commit comments

Comments
 (0)