-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_context_process_SUITE.erl
More file actions
348 lines (290 loc) · 10.2 KB
/
py_context_process_SUITE.erl
File metadata and controls
348 lines (290 loc) · 10.2 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
%%% @doc Common Test suite for py_context process-per-context module.
%%%
%%% Tests the process-per-context architecture where each Erlang process
%%% owns a Python context (subinterpreter or worker).
-module(py_context_process_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
]).
-export([
test_context_start_stop/1,
test_context_call/1,
test_context_eval/1,
test_context_exec/1,
test_context_isolation/1,
test_context_module_caching/1,
test_context_under_supervisor/1,
test_multiple_contexts/1,
test_context_parallel_calls/1,
test_context_timeout/1,
test_context_error_handling/1,
test_context_type_conversions/1
]).
%% ============================================================================
%% Common Test callbacks
%% ============================================================================
all() ->
[
{group, worker}
].
groups() ->
Tests = [
test_context_start_stop,
test_context_call,
test_context_eval,
test_context_exec,
test_context_isolation,
test_context_module_caching,
test_context_under_supervisor,
test_multiple_contexts,
test_context_parallel_calls,
test_context_timeout,
test_context_error_handling,
test_context_type_conversions
],
[
{worker, [sequence], Tests}
].
init_per_suite(Config) ->
%% Ensure the application is started
application:ensure_all_started(erlang_python),
Config.
end_per_suite(_Config) ->
ok.
init_per_group(worker, Config) ->
[{context_type, worker} | Config].
end_per_group(_Group, _Config) ->
ok.
%% ============================================================================
%% Test cases
%% ============================================================================
%% @doc Test that a context can be started and stopped.
test_context_start_stop(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
true = is_process_alive(Ctx),
ok = py_context:stop(Ctx),
timer:sleep(50),
false = is_process_alive(Ctx).
%% @doc Test basic Python function calls.
test_context_call(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Test math.sqrt
{ok, 4.0} = py_context:call(Ctx, math, sqrt, [16], #{}),
%% Test with kwargs
{ok, _} = py_context:call(Ctx, json, dumps, [[{<<"a">>, 1}]], #{indent => 2}),
%% Test len function
{ok, 3} = py_context:call(Ctx, builtins, len, [[1, 2, 3]], #{})
after
py_context:stop(Ctx)
end.
%% @doc Test Python expression evaluation.
test_context_eval(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Simple arithmetic
{ok, 6} = py_context:eval(Ctx, <<"2 + 4">>, #{}),
%% With locals
{ok, 15} = py_context:eval(Ctx, <<"x * 3">>, #{x => 5}),
%% List comprehension
{ok, [1, 4, 9, 16]} = py_context:eval(Ctx, <<"[i*i for i in range(1, 5)]">>, #{})
after
py_context:stop(Ctx)
end.
%% @doc Test Python statement execution.
test_context_exec(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Execute statements
ok = py_context:exec(Ctx, <<"x = 42">>),
{ok, 42} = py_context:eval(Ctx, <<"x">>, #{}),
%% Define a function
ok = py_context:exec(Ctx, <<"def add(a, b): return a + b">>),
{ok, 7} = py_context:eval(Ctx, <<"add(3, 4)">>, #{})
after
py_context:stop(Ctx)
end.
%% @doc Test that contexts are isolated from each other.
test_context_isolation(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx1} = py_context:start_link(1, Mode),
{ok, Ctx2} = py_context:start_link(2, Mode),
try
%% Set different values in each context
ok = py_context:exec(Ctx1, <<"isolation_var = 'ctx1'">>),
ok = py_context:exec(Ctx2, <<"isolation_var = 'ctx2'">>),
%% Verify isolation
{ok, <<"ctx1">>} = py_context:eval(Ctx1, <<"isolation_var">>, #{}),
{ok, <<"ctx2">>} = py_context:eval(Ctx2, <<"isolation_var">>, #{}),
%% Verify interpreter IDs are different
{ok, Id1} = py_context:get_interp_id(Ctx1),
{ok, Id2} = py_context:get_interp_id(Ctx2),
true = Id1 =/= Id2
after
py_context:stop(Ctx1),
py_context:stop(Ctx2)
end.
%% @doc Test that modules are cached within a context.
test_context_module_caching(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% First call imports the module
{ok, 4.0} = py_context:call(Ctx, math, sqrt, [16], #{}),
%% Second call should use cached module (faster)
{ok, 5.0} = py_context:call(Ctx, math, sqrt, [25], #{}),
%% Multiple different modules
{ok, _} = py_context:call(Ctx, json, dumps, [[1, 2, 3]], #{}),
{ok, _} = py_context:call(Ctx, os, getcwd, [], #{})
after
py_context:stop(Ctx)
end.
%% @doc Test contexts under the supervisor.
test_context_under_supervisor(Config) ->
Mode = ?config(context_type, Config),
%% Start supervisor if not already running
case whereis(py_context_sup) of
undefined ->
{ok, _SupPid} = py_context_sup:start_link();
_ ->
ok
end,
try
%% Start contexts via supervisor
{ok, Ctx1} = py_context_sup:start_context(1, Mode),
{ok, Ctx2} = py_context_sup:start_context(2, Mode),
%% Verify they work
{ok, 4.0} = py_context:call(Ctx1, math, sqrt, [16], #{}),
{ok, 9.0} = py_context:call(Ctx2, math, sqrt, [81], #{}),
%% Check which_contexts
Contexts = py_context_sup:which_contexts(),
true = lists:member(Ctx1, Contexts),
true = lists:member(Ctx2, Contexts),
%% Stop one via supervisor
ok = py_context_sup:stop_context(Ctx1),
timer:sleep(50),
false = is_process_alive(Ctx1),
true = is_process_alive(Ctx2),
%% Clean up
py_context_sup:stop_context(Ctx2)
after
ok
end.
%% @doc Test multiple contexts working in parallel.
test_multiple_contexts(Config) ->
Mode = ?config(context_type, Config),
NumContexts = 4,
%% Start multiple contexts
Contexts = [begin
{ok, Ctx} = py_context:start_link(N, Mode),
Ctx
end || N <- lists:seq(1, NumContexts)],
try
%% Run calls in parallel
Parent = self(),
Pids = [spawn_link(fun() ->
Results = [py_context:call(Ctx, math, sqrt, [N*N], #{})
|| N <- lists:seq(1, 10)],
Parent ! {self(), Results}
end) || Ctx <- Contexts],
%% Collect results
[receive
{Pid, Results} ->
%% Verify all calls succeeded
lists:foreach(fun({ok, _}) -> ok end, Results)
after 5000 ->
ct:fail("Timeout waiting for results")
end || Pid <- Pids]
after
[py_context:stop(Ctx) || Ctx <- Contexts]
end.
%% @doc Test parallel calls to the same context.
test_context_parallel_calls(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Make multiple calls from different processes
%% The context should serialize them (process-owned)
Parent = self(),
NumCalls = 20,
Pids = [spawn_link(fun() ->
Result = py_context:call(Ctx, math, sqrt, [N*N], #{}),
Parent ! {self(), N, Result}
end) || N <- lists:seq(1, NumCalls)],
Results = [receive
{Pid, N, Result} -> {N, Result}
after 5000 ->
ct:fail("Timeout")
end || Pid <- Pids],
%% Verify all returned correct values
lists:foreach(fun({N, {ok, Val}}) ->
true = abs(Val - float(N)) < 0.0001
end, Results)
after
py_context:stop(Ctx)
end.
%% @doc Test call timeout handling.
test_context_timeout(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Quick call should succeed
{ok, 4.0} = py_context:call(Ctx, math, sqrt, [16], #{}, 1000),
%% Slow call with short timeout should fail
%% (Can't easily test this without a slow Python function)
ok
after
py_context:stop(Ctx)
end.
%% @doc Test error handling in context calls.
test_context_error_handling(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Invalid module
{error, _} = py_context:call(Ctx, nonexistent_module, func, [], #{}),
%% Invalid function
{error, _} = py_context:call(Ctx, math, nonexistent_func, [], #{}),
%% Python exception
{error, _} = py_context:eval(Ctx, <<"1/0">>, #{}),
%% Syntax error in exec
{error, _} = py_context:exec(Ctx, <<"if true">>),
%% Context should still work after errors
{ok, 4.0} = py_context:call(Ctx, math, sqrt, [16], #{})
after
py_context:stop(Ctx)
end.
%% @doc Test type conversions between Erlang and Python.
test_context_type_conversions(Config) ->
Mode = ?config(context_type, Config),
{ok, Ctx} = py_context:start_link(1, Mode),
try
%% Integers
{ok, 42} = py_context:eval(Ctx, <<"x">>, #{x => 42}),
%% Floats
{ok, 3.14159} = py_context:eval(Ctx, <<"x">>, #{x => 3.14159}),
%% Strings (binaries)
{ok, <<"hello">>} = py_context:eval(Ctx, <<"x">>, #{x => <<"hello">>}),
%% Lists
{ok, [1, 2, 3]} = py_context:eval(Ctx, <<"x">>, #{x => [1, 2, 3]}),
%% Maps/Dicts
{ok, Map} = py_context:eval(Ctx, <<"x">>, #{x => #{a => 1, b => 2}}),
true = is_map(Map),
%% Booleans
{ok, true} = py_context:eval(Ctx, <<"x">>, #{x => true}),
{ok, false} = py_context:eval(Ctx, <<"x">>, #{x => false}),
%% None
{ok, none} = py_context:eval(Ctx, <<"None">>, #{})
after
py_context:stop(Ctx)
end.