|
36 | 36 | test_process_namespace_exec/1, |
37 | 37 | test_process_namespace_eval/1, |
38 | 38 | test_process_namespace_async_func/1, |
39 | | - test_process_namespace_isolation/1 |
| 39 | + test_process_namespace_isolation/1, |
| 40 | + test_process_namespace_reentrant/1 |
40 | 41 | ]). |
41 | 42 |
|
42 | 43 | all() -> |
@@ -72,7 +73,8 @@ all() -> |
72 | 73 | test_process_namespace_exec, |
73 | 74 | test_process_namespace_eval, |
74 | 75 | test_process_namespace_async_func, |
75 | | - test_process_namespace_isolation |
| 76 | + test_process_namespace_isolation, |
| 77 | + test_process_namespace_reentrant |
76 | 78 | ]. |
77 | 79 |
|
78 | 80 | groups() -> []. |
@@ -435,3 +437,37 @@ test_process_namespace_isolation(_Config) -> |
435 | 437 | {ok, _} -> |
436 | 438 | ct:log("isolation test: parent unexpectedly saw child_var") |
437 | 439 | end. |
| 440 | + |
| 441 | +test_process_namespace_reentrant(_Config) -> |
| 442 | + %% Test that namespace variables are accessible during task execution |
| 443 | + %% This verifies the thread-local namespace is set correctly |
| 444 | + |
| 445 | + %% Define a variable and a function that uses it |
| 446 | + ok = py_event_loop:exec(<<" |
| 447 | +shared_value = 100 |
| 448 | +
|
| 449 | +def use_shared(): |
| 450 | + # Access shared_value from namespace |
| 451 | + return shared_value + 23 |
| 452 | +">>), |
| 453 | + |
| 454 | + %% Call the function via create_task - it should access the namespace |
| 455 | + Ref = py_event_loop:create_task('__main__', use_shared, []), |
| 456 | + {ok, Result} = py_event_loop:await(Ref, 5000), |
| 457 | + ct:log("reentrant test: use_shared() returned ~p (expected 123)", [Result]), |
| 458 | + 123 = Result, |
| 459 | + |
| 460 | + %% Test with a function that modifies namespace |
| 461 | + ok = py_event_loop:exec(<<" |
| 462 | +def increment_shared(): |
| 463 | + global shared_value |
| 464 | + shared_value += 1 |
| 465 | + return shared_value |
| 466 | +">>), |
| 467 | + |
| 468 | + Ref2 = py_event_loop:create_task('__main__', increment_shared, []), |
| 469 | + {ok, 101} = py_event_loop:await(Ref2, 5000), |
| 470 | + |
| 471 | + %% Verify the change persists in namespace |
| 472 | + {ok, 101} = py_event_loop:eval(<<"shared_value">>), |
| 473 | + ct:log("reentrant test: namespace modifications persist correctly"). |
0 commit comments