Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3727,18 +3727,38 @@ def test_set_env__thread_safety(

C_NUM_THREADS = 2

if OsOpsHelpers.is_localhost(os_ops):
C_NUM_ITERATIONS = 2000
else:
if type(os_ops).__name__ == "RemoteOperations":
C_NUM_ITERATIONS = 200
else:
C_NUM_ITERATIONS = 2000

logging.info("NUM_ITERATIONS: {}".format(C_NUM_ITERATIONS))

# Queue for collecting exceptions from background threads
exceptions_queue = queue.Queue()

# The function that each thread will run
def thread_worker(var_name: str, var_value: str, iterations: int):
def thread_worker(
thread_num: int,
var_name: str,
var_value: str,
iterations: int,
):
logging.info("Hello from thread [{}].".format(
thread_num,
))

try:
for _ in range(iterations):
nPass = 0
while nPass < iterations:
if nPass > 0 and (nPass % 100) == 0:
logging.info("thread [{}]: {}".format(
thread_num,
nPass,
))

nPass += 1

# 1. The thread writes ITS own isolated variable
os_ops.set_env(var_name, var_value)

Expand All @@ -3755,7 +3775,12 @@ def thread_worker(var_name: str, var_value: str, iterations: int):
# 3. Clean up after yourself
os_ops.reset_env(var_name, None)
assert os_ops.environ(var_name) is None
continue

logging.info("thread [{}] finished ({})".format(
thread_num,
nPass,
))
except Exception as e:
# If something goes wrong, we pass the error to the main test thread
exceptions_queue.put(e)
Expand All @@ -3774,7 +3799,7 @@ def thread_worker(var_name: str, var_value: str, iterations: int):

threads[i] = threading.Thread(
target=thread_worker,
args=(thread_name, thread_val, C_NUM_ITERATIONS),
args=(i, thread_name, thread_val, C_NUM_ITERATIONS),
)
continue

Expand Down