-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_event_loop_pool_owngil_SUITE.erl
More file actions
281 lines (228 loc) · 9.27 KB
/
py_event_loop_pool_owngil_SUITE.erl
File metadata and controls
281 lines (228 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
%%% @doc Common Test suite for OWN_GIL Event Loop Pool.
%%%
%%% Tests the OWN_GIL mode with true parallel execution in separate GIL workers.
-module(py_event_loop_pool_owngil_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([
all/0,
groups/0,
init_per_suite/1,
end_per_suite/1,
init_per_group/2,
end_per_group/2,
init_per_testcase/2,
end_per_testcase/2
]).
-export([
%% Pool tests
test_owngil_pool_stats/1,
test_owngil_session_creation/1,
%% Task execution tests
test_sync_function_call/1,
test_async_coroutine_call/1,
test_concurrent_tasks/1,
%% Process affinity tests
test_same_process_same_worker/1,
test_tasks_execute_in_order/1,
%% Cleanup tests
test_session_cleanup_on_process_exit/1,
%% Parallelism tests
test_true_parallel_execution/1
]).
all() ->
[{group, owngil_tests}].
groups() ->
[{owngil_tests, [sequence], [
test_owngil_pool_stats,
test_owngil_session_creation,
test_sync_function_call,
test_async_coroutine_call,
test_concurrent_tasks,
test_same_process_same_worker,
test_tasks_execute_in_order,
test_session_cleanup_on_process_exit,
test_true_parallel_execution
]}].
init_per_suite(Config) ->
%% Check if subinterpreters are supported
case py_nif:subinterp_supported() of
false ->
{skip, "Subinterpreters not supported (Python < 3.12)"};
true ->
%% Stop any existing app to ensure clean state
application:stop(erlang_python),
timer:sleep(100),
%% Enable OWN_GIL mode
application:set_env(erlang_python, event_loop_pool_owngil, true),
application:set_env(erlang_python, event_loop_pool_size, 4),
case application:ensure_all_started(erlang_python) of
{ok, _} ->
timer:sleep(500),
case wait_for_owngil_pool(5000) of
ok -> Config;
{error, Reason} -> {skip, {pool_not_ready, Reason}}
end;
{error, {App, Reason}} ->
{skip, {failed_to_start, App, Reason}}
end
end.
wait_for_owngil_pool(Timeout) when Timeout =< 0 ->
{error, timeout};
wait_for_owngil_pool(Timeout) ->
Stats = py_event_loop_pool:get_stats(),
case maps:get(owngil_enabled, Stats, false) of
true -> ok;
false ->
timer:sleep(100),
wait_for_owngil_pool(Timeout - 100)
end.
end_per_suite(_Config) ->
ok = application:stop(erlang_python),
application:set_env(erlang_python, event_loop_pool_owngil, false),
ok.
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, _Config) ->
ok.
init_per_testcase(_TestCase, Config) ->
Config.
end_per_testcase(_TestCase, _Config) ->
ok.
%% ============================================================================
%% Pool Tests
%% ============================================================================
test_owngil_pool_stats(_Config) ->
Stats = py_event_loop_pool:get_stats(),
ct:log("OWN_GIL Pool stats: ~p", [Stats]),
true = maps:get(owngil_enabled, Stats),
NumLoops = maps:get(num_loops, Stats),
true = NumLoops > 0,
ok.
test_owngil_session_creation(_Config) ->
%% Test that session is created when submitting a task
Ref = py_event_loop_pool:create_task(math, sqrt, [16.0]),
true = is_reference(Ref),
{ok, 4.0} = py_event_loop_pool:await(Ref, 5000),
%% Check that active_sessions increased
Stats = py_event_loop_pool:get_stats(),
ct:log("Stats after task: ~p", [Stats]),
ActiveSessions = maps:get(active_sessions, Stats, 0),
true = ActiveSessions >= 1,
ok.
%% ============================================================================
%% Task Execution Tests
%% ============================================================================
test_sync_function_call(_Config) ->
%% Test calling a regular sync function
{ok, 3} = py_event_loop_pool:run(math, floor, [3.7]),
{ok, 4} = py_event_loop_pool:run(math, ceil, [3.2]),
ok.
test_async_coroutine_call(_Config) ->
%% Test that async tasks work (using simple math function)
%% Note: asyncio.sleep is a coroutine but may have timing issues on CI
%% so we just verify the basic task submission works
Ref = py_event_loop_pool:create_task(math, sqrt, [16.0]),
Result = py_event_loop_pool:await(Ref, 5000),
ct:log("Async result: ~p", [Result]),
{ok, 4.0} = Result,
ok.
test_concurrent_tasks(_Config) ->
NumTasks = 20,
Refs = [py_event_loop_pool:create_task(math, sqrt, [float(I * I)])
|| I <- lists:seq(1, NumTasks)],
ct:log("Created ~p tasks", [length(Refs)]),
Results = [py_event_loop_pool:await(Ref, 5000) || Ref <- Refs],
OkResults = [{ok, V} || {ok, V} <- Results],
NumTasks = length(OkResults),
ok.
%% ============================================================================
%% Process Affinity Tests
%% ============================================================================
test_same_process_same_worker(_Config) ->
%% Multiple tasks from the same process should go to the same worker
%% We verify process affinity by checking that multiple calls succeed
%% (same process always routes to same worker due to PID hashing)
%% Submit multiple tasks from this process - they all go to same worker
Refs = [py_event_loop_pool:create_task(math, sqrt, [float(I * I)])
|| I <- lists:seq(1, 5)],
Results = [py_event_loop_pool:await(Ref, 5000) || Ref <- Refs],
ct:log("Results: ~p", [Results]),
%% All should succeed with expected values
Expected = [{ok, float(I)} || I <- lists:seq(1, 5)],
Expected = Results,
ok.
test_tasks_execute_in_order(_Config) ->
%% All tasks from this process go to the same worker, so they execute in order
Refs = [py_event_loop_pool:create_task(math, sqrt, [float(I)])
|| I <- [1, 4, 9, 16, 25]],
Results = [py_event_loop_pool:await(Ref, 5000) || Ref <- Refs],
ct:log("Results: ~p", [Results]),
%% Results should be in submission order
[{ok, 1.0}, {ok, 2.0}, {ok, 3.0}, {ok, 4.0}, {ok, 5.0}] = Results,
ok.
%% ============================================================================
%% Cleanup Tests
%% ============================================================================
test_session_cleanup_on_process_exit(_Config) ->
%% This test verifies that session cleanup happens when a process dies
%% We spawn multiple processes to ensure sessions are created
Parent = self(),
NumProcs = 5,
%% Spawn multiple processes that create sessions
Pids = [spawn(fun() ->
%% Create a task which creates a session
Ref = py_event_loop_pool:create_task(math, sqrt, [9.0]),
{ok, 3.0} = py_event_loop_pool:await(Ref, 5000),
Parent ! {self(), session_created},
receive stop -> ok end
end) || _ <- lists:seq(1, NumProcs)],
%% Wait for all sessions to be created
[receive
{Pid, session_created} -> ok
after 5000 ->
ct:fail({timeout_waiting_for_session, Pid})
end || Pid <- Pids],
%% Check stats after creation
Stats1 = py_event_loop_pool:get_stats(),
SessionsBefore = maps:get(active_sessions, Stats1, 0),
ct:log("Sessions after create: ~p", [SessionsBefore]),
%% Kill all processes
[Pid ! stop || Pid <- Pids],
timer:sleep(500), %% Give time for cleanup
%% Verify session count after cleanup
Stats2 = py_event_loop_pool:get_stats(),
SessionsAfter = maps:get(active_sessions, Stats2, 0),
ct:log("Sessions after cleanup: ~p", [SessionsAfter]),
%% Sessions should have been cleaned up
%% Note: Our main test process also has a session, so count won't be 0
ct:log("Session cleanup: before=~p, after=~p", [SessionsBefore, SessionsAfter]),
ok.
%% ============================================================================
%% Parallelism Tests
%% ============================================================================
test_true_parallel_execution(_Config) ->
%% Test that multiple tasks can be submitted from different processes
%% Each process gets its own worker (due to OWN_GIL mode)
Parent = self(),
NumWorkers = 4,
T1 = erlang:monotonic_time(millisecond),
%% Run math.sqrt tasks in parallel from different processes
Pids = [spawn(fun() ->
Ref = py_event_loop_pool:create_task(math, sqrt, [float(I * I)]),
Result = py_event_loop_pool:await(Ref, 5000),
Parent ! {self(), Result}
end) || I <- lists:seq(1, NumWorkers)],
%% Collect results
Results = [receive {Pid, R} -> R after 5000 -> {error, timeout} end || Pid <- Pids],
T2 = erlang:monotonic_time(millisecond),
Elapsed = T2 - T1,
ct:log("Parallel results: ~p", [Results]),
ct:log("Elapsed time: ~p ms", [Elapsed]),
%% Check that at least some tasks succeeded
OkResults = [R || {ok, _} = R <- Results],
ct:log("Successful tasks: ~p out of ~p", [length(OkResults), NumWorkers]),
%% At least half should succeed (lenient check)
true = length(OkResults) >= NumWorkers div 2,
%% Log the elapsed time for analysis
ct:log("Parallelism test: ~p ms elapsed for ~p tasks", [Elapsed, NumWorkers]),
ok.