Skip to content

Commit ad0baa4

Browse files
committed
Add a test case
1 parent 67c5af0 commit ad0baa4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Lib/test/test_support.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import sysconfig
1414
import tempfile
1515
import textwrap
16+
import threading
17+
import time
1618
import unittest
1719
import warnings
1820

@@ -22,6 +24,7 @@
2224
from test.support import script_helper
2325
from test.support import socket_helper
2426
from test.support import warnings_helper
27+
from test.support.os_helper import EnvironmentVarGuard
2528

2629
TESTFN = os_helper.TESTFN
2730

@@ -794,6 +797,27 @@ def test_linked_to_musl(self):
794797
for v in linked:
795798
self.assertIsInstance(v, int)
796799

800+
def test_threadsafe_environmentvarguard(self):
801+
def worker1(guard):
802+
for i in range(1000):
803+
guard['MY_VAR'] = 'value1'
804+
time.sleep(0.0001) # Small delay to increase chance of thread switching
805+
806+
def worker2(guard):
807+
for i in range(1000):
808+
guard['MY_VAR'] = 'value2'
809+
time.sleep(0.0001)
810+
811+
guard = EnvironmentVarGuard()
812+
t1 = threading.Thread(target=worker1, args=(guard,))
813+
t2 = threading.Thread(target=worker2, args=(guard,))
814+
t1.start()
815+
t2.start()
816+
t1.join()
817+
t2.join()
818+
final_value = os.getenv('MY_VAR')
819+
self.assertEqual(final_value, "value2")
820+
797821

798822
# XXX -follows a list of untested API
799823
# make_legacy_pyc

0 commit comments

Comments
 (0)