1
1
import ultraplot as uplt , threading , pytest , warnings
2
2
3
3
4
- def modify_rc_in_context (prop : str , value = None ):
4
+ def modify_rc_on_thread (prop : str , value = None , with_context = True ):
5
5
"""
6
6
Apply arbitrary rc parameters in a thread-safe manner.
7
7
"""
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 :
9
12
assert uplt .rc [prop ] == value , f"Thread { id } failed to set rc params"
10
13
11
14
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 ):
19
16
options = kwargs .pop ("options" )
20
17
workers = []
21
18
exceptions = []
@@ -56,7 +53,9 @@ def test_setting_within_context():
56
53
# be local to that context and not affect the main thread.
57
54
prop , value = "font.size" , uplt .rc ["font.size" ]
58
55
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
+ )
60
59
assert uplt .rc [prop ] == value
61
60
62
61
@@ -71,10 +70,14 @@ def test_setting_without_context():
71
70
# Test an ultraplot parameter
72
71
prop = "abc"
73
72
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
+ )
76
77
assert uplt .rc [prop ] == value
77
78
78
79
prop , value = "font.size" , uplt .rc ["font.size" ]
79
80
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