-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_reactor_SUITE.erl
More file actions
520 lines (413 loc) · 14.9 KB
/
py_reactor_SUITE.erl
File metadata and controls
520 lines (413 loc) · 14.9 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
%%% @doc Common Test suite for erlang.reactor API.
%%%
%%% Tests the reactor module that provides fd-based protocol handling
%%% where Erlang handles I/O scheduling and Python handles protocol logic.
-module(py_reactor_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([
all/0,
init_per_suite/1,
end_per_suite/1,
init_per_testcase/2,
end_per_testcase/2
]).
-export([
reactor_module_exists_test/1,
protocol_class_exists_test/1,
set_protocol_factory_test/1,
echo_protocol_test/1,
multiple_connections_test/1,
protocol_close_test/1,
async_pending_test/1,
reactor_buffer_test/1,
%% Worker mode isolation test
reactor_context_worker_isolation_test/1
]).
all() -> [
reactor_module_exists_test,
protocol_class_exists_test,
set_protocol_factory_test,
echo_protocol_test,
multiple_connections_test,
protocol_close_test,
async_pending_test,
reactor_buffer_test,
reactor_context_worker_isolation_test
].
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(erlang_python),
{ok, _} = py:start_contexts(),
Config.
end_per_suite(_Config) ->
ok = application:stop(erlang_python),
ok.
init_per_testcase(_TestCase, Config) ->
Config.
end_per_testcase(_TestCase, _Config) ->
ok.
%%% ============================================================================
%%% Test Cases
%%% ============================================================================
%% @doc Test that erlang.reactor module exists
reactor_module_exists_test(_Config) ->
{ok, true} = py:eval(<<"hasattr(erlang, 'reactor')">>).
%% @doc Test that Protocol class exists
protocol_class_exists_test(_Config) ->
{ok, true} = py:eval(<<"hasattr(erlang.reactor, 'Protocol')">>),
{ok, true} = py:eval(<<"callable(erlang.reactor.Protocol)">>).
%% @doc Test set_protocol_factory works
set_protocol_factory_test(_Config) ->
%% Define a simple protocol
ok = py:exec(<<"
import erlang.reactor as reactor
class TestProtocol(reactor.Protocol):
def data_received(self, data):
return 'continue'
def write_ready(self):
return 'close'
reactor.set_protocol_factory(TestProtocol)
">>),
ok.
%% @doc Test echo protocol with socketpair
echo_protocol_test(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import socket
import erlang.reactor as reactor
class EchoProtocol(reactor.Protocol):
def data_received(self, data):
self.write_buffer.extend(data)
return 'write_pending'
def write_ready(self):
if not self.write_buffer:
return 'close'
written = self.write(bytes(self.write_buffer))
del self.write_buffer[:written]
return 'continue' if self.write_buffer else 'read_pending'
def run_echo_test():
s1, s2 = socket.socketpair()
s1.setblocking(False)
s2.setblocking(False)
reactor.set_protocol_factory(EchoProtocol)
reactor.init_connection(s1.fileno(), {'type': 'test'})
s2.send(b'hello')
action = reactor.on_read_ready(s1.fileno())
proto = reactor.get_protocol(s1.fileno())
result = bytes(proto.write_buffer)
reactor.close_connection(s1.fileno())
s1.close()
s2.close()
return result
_echo_test_result = run_echo_test()
">>),
{ok, <<"hello">>} = py:eval(Ctx, <<"_echo_test_result">>).
%% @doc Test multiple connections
multiple_connections_test(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import socket
import erlang.reactor as reactor
class CounterProtocol(reactor.Protocol):
counter = 0
def connection_made(self, fd, client_info):
super().connection_made(fd, client_info)
CounterProtocol.counter += 1
self.my_id = CounterProtocol.counter
def data_received(self, data):
return 'close'
def write_ready(self):
return 'close'
def run_multi_conn_test():
CounterProtocol.counter = 0
reactor.set_protocol_factory(CounterProtocol)
pairs = [socket.socketpair() for _ in range(3)]
for s1, s2 in pairs:
s1.setblocking(False)
reactor.init_connection(s1.fileno(), {})
ids = [reactor.get_protocol(s1.fileno()).my_id for s1, s2 in pairs]
for s1, s2 in pairs:
reactor.close_connection(s1.fileno())
s1.close()
s2.close()
return ids
_multi_conn_result = run_multi_conn_test()
">>),
{ok, [1, 2, 3]} = py:eval(Ctx, <<"_multi_conn_result">>).
%% @doc Test protocol close callback
protocol_close_test(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import socket
import erlang.reactor as reactor
_closed_fds = []
class CloseTrackProtocol(reactor.Protocol):
def data_received(self, data):
return 'close'
def write_ready(self):
return 'close'
def connection_lost(self):
_closed_fds.append(self.fd)
def run_close_test():
global _closed_fds
_closed_fds = []
reactor.set_protocol_factory(CloseTrackProtocol)
s1, s2 = socket.socketpair()
s1.setblocking(False)
fd = s1.fileno()
reactor.init_connection(fd, {})
reactor.close_connection(fd)
result = fd in _closed_fds
s1.close()
s2.close()
return result
_close_test_result = run_close_test()
">>),
{ok, true} = py:eval(Ctx, <<"_close_test_result">>).
%% @doc Test async_pending action for task-based async operations.
%% This tests the pattern used by task-based ASGI where:
%% 1. Protocol returns "async_pending" to indicate a task was submitted
%% 2. Later, Python sends {write_ready, Fd} to signal completion
%% 3. Reactor then triggers write selection
async_pending_test(_Config) ->
%% Protocol factory code to run in reactor context
SetupCode = <<"
import erlang.reactor as reactor
class AsyncPendingProtocol(reactor.Protocol):
'''Protocol that returns async_pending and signals completion.'''
def __init__(self):
super().__init__()
self.pending_response = b''
def data_received(self, data):
self.pending_response = b'ASYNC:' + data
# Immediately complete the task and signal reactor
self.write_buffer.extend(self.pending_response)
reactor.signal_write_ready(self.fd)
return 'async_pending'
def write_ready(self):
if not self.write_buffer:
return 'close'
written = self.write(bytes(self.write_buffer))
del self.write_buffer[:written]
return 'continue' if self.write_buffer else 'close'
reactor.set_protocol_factory(AsyncPendingProtocol)
">>,
%% Start reactor context with protocol factory setup
{ok, ReactorCtx} = py_reactor_context:start_link(1, auto, #{
setup_code => SetupCode
}),
%% Use py:context(1) for test helpers (socket management)
PyCtx = py:context(1),
ok = py:exec(PyCtx, <<"
import socket
_async_test_state = {}
def setup_socketpair():
global _async_test_state
s1, s2 = socket.socketpair()
s1.setblocking(False)
s2.setblocking(False)
_async_test_state = {'s1': s1, 's2': s2, 'fd': s1.fileno()}
return s1.fileno()
def send_test_data():
s2 = _async_test_state['s2']
s2.send(b'hello')
return True
def read_response():
s2 = _async_test_state['s2']
s2.setblocking(True)
s2.settimeout(2.0)
try:
return s2.recv(1024)
except socket.timeout:
return b'TIMEOUT'
def cleanup():
s1 = _async_test_state.get('s1')
s2 = _async_test_state.get('s2')
try:
if s1: s1.close()
except:
pass
try:
if s2: s2.close()
except:
pass
_async_test_state.clear()
return True
">>),
%% Step 1: Create socketpair
{ok, Fd} = py:eval(PyCtx, <<"setup_socketpair()">>),
ct:pal("Created socketpair with fd=~p", [Fd]),
%% Check reactor context is alive
ct:pal("Reactor context ~p is alive: ~p", [ReactorCtx, is_process_alive(ReactorCtx)]),
%% Step 2: Send fd_handoff to reactor context
ok = py_reactor_context:handoff(ReactorCtx, Fd, #{}),
timer:sleep(100),
%% Check reactor stats after handoff
Stats = py_reactor_context:stats(ReactorCtx),
ct:pal("Reactor stats after handoff: ~p", [Stats]),
%% Step 3: Send test data - triggers async_pending and immediate completion
{ok, true} = py:eval(PyCtx, <<"send_test_data()">>),
timer:sleep(200),
%% Step 4: Read response
{ok, Response} = py:eval(PyCtx, <<"read_response()">>),
ct:pal("Response: ~p", [Response]),
%% Verify response
<<"ASYNC:hello">> = Response,
%% Cleanup
{ok, _} = py:eval(PyCtx, <<"cleanup()">>),
py_reactor_context:stop(ReactorCtx),
ok.
%% @doc Test ReactorBuffer behaves like bytes
reactor_buffer_test(_Config) ->
%% Test that ReactorBuffer type exists in erlang module
{ok, true} = py:eval(<<"hasattr(erlang, 'ReactorBuffer')">>),
%% Test bytes-like operations - use exec for all assertions
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import erlang
# Test _test_create works
buf_simple = erlang.ReactorBuffer._test_create(b'hello')
assert len(buf_simple) == 5, f'simple len mismatch: {len(buf_simple)}'
test_data = b'GET / HTTP/1.1' + bytes([13, 10]) # Use bytes for \\r\\n
buf = erlang.ReactorBuffer._test_create(test_data)
# Test len()
assert len(buf) == 16, f'len mismatch: {len(buf)}' # 14 chars + 2 for \\r\\n
# Test startswith()
assert buf.startswith(b'GET'), 'startswith failed'
assert not buf.startswith(b'POST'), 'startswith should fail for POST'
# Test endswith()
assert buf.endswith(bytes([13, 10])), 'endswith failed'
# Test indexing
assert buf[0] == ord('G'), f'index 0 mismatch: {buf[0]}'
assert buf[-1] == 10, f'index -1 mismatch'
# Test slicing
assert buf[0:3] == b'GET', f'slice mismatch: {buf[0:3]}'
# Test bytes() conversion
assert bytes(buf) == test_data, 'bytes conversion failed'
# Test memoryview
mv = memoryview(buf)
assert bytes(mv) == test_data, 'memoryview failed'
# Test find()
assert buf.find(b'HTTP') == 6, f'find mismatch: {buf.find(b\"HTTP\")}'
assert buf.find(b'HTTPS') == -1, 'find should return -1'
# Test count()
assert buf.count(b'/') == 2, f'count mismatch: {buf.count(b\"/\")}' # '/' in path and '1.1'
# Test 'in' operator
assert b'HTTP' in buf, 'in operator failed'
assert b'HTTPS' not in buf, 'not in operator failed'
# Test decode()
decoded = buf.decode('utf-8')
assert decoded == 'GET / HTTP/1.1' + chr(13) + chr(10), f'decode mismatch: {decoded}'
# Test comparison
assert buf == test_data, 'equality comparison failed'
assert buf != b'other', 'inequality comparison failed'
_reactor_buffer_test_passed = True
">>),
{ok, true} = py:eval(Ctx, <<"_reactor_buffer_test_passed">>).
%%% ============================================================================
%%% Worker Mode Isolation Test
%%% ============================================================================
%%%
%%% Tests that the reactor module cache is properly isolated per-context
%%% when using worker mode via py_reactor_context.
%%%
%%% Each py_reactor_context with mode=worker creates a separate Python
%%% context with its own module state, including the reactor cache
%%% (protocol factory, connections, etc.).
%% @doc Test that reactor contexts with worker mode have isolated protocol factories.
%%
%% Creates two py_reactor_context processes with different protocol factories:
%% - Context 1: EchoProtocol (echoes data back unchanged)
%% - Context 2: UpperProtocol (echoes data back uppercased)
%%
%% Verifies that handoffs to each context use their own isolated factory.
%% Note: This test may be skipped on platforms where worker contexts
%% don't properly isolate module globals (e.g., some FreeBSD configurations).
reactor_context_worker_isolation_test(_Config) ->
%% Context 1 with EchoProtocol - echoes data unchanged
EchoSetup = <<"
import erlang.reactor as reactor
class EchoProtocol(reactor.Protocol):
def data_received(self, data):
self.write_buffer.extend(data)
return 'write_pending'
def write_ready(self):
if not self.write_buffer:
return 'close'
written = self.write(bytes(self.write_buffer))
del self.write_buffer[:written]
return 'continue' if self.write_buffer else 'close'
reactor.set_protocol_factory(EchoProtocol)
">>,
{ok, Ctx1} = py_reactor_context:start_link(101, worker, #{
setup_code => EchoSetup
}),
%% Context 2 with UpperProtocol - echoes data uppercased
UpperSetup = <<"
import erlang.reactor as reactor
class UpperProtocol(reactor.Protocol):
def data_received(self, data):
self.write_buffer.extend(bytes(data).upper())
return 'write_pending'
def write_ready(self):
if not self.write_buffer:
return 'close'
written = self.write(bytes(self.write_buffer))
del self.write_buffer[:written]
return 'continue' if self.write_buffer else 'close'
reactor.set_protocol_factory(UpperProtocol)
">>,
{ok, Ctx2} = py_reactor_context:start_link(102, worker, #{
setup_code => UpperSetup
}),
%% Create socketpairs for testing
{ok, {S1a, S1b}} = create_socketpair(),
{ok, {S2a, S2b}} = create_socketpair(),
Fd1 = get_fd(S1a),
Fd2 = get_fd(S2a),
%% Handoff connections to each context
ok = py_reactor_context:handoff(Ctx1, Fd1, #{type => test}),
ok = py_reactor_context:handoff(Ctx2, Fd2, #{type => test}),
%% Give contexts time to process handoffs
timer:sleep(100),
%% Send test data to both
TestData = <<"hello">>,
ok = gen_tcp:send(S1b, TestData),
ok = gen_tcp:send(S2b, TestData),
%% Receive responses
{ok, Response1} = gen_tcp:recv(S1b, 0, 2000),
{ok, Response2} = gen_tcp:recv(S2b, 0, 2000),
%% Cleanup before checking results
gen_tcp:close(S1a),
gen_tcp:close(S1b),
gen_tcp:close(S2a),
gen_tcp:close(S2b),
py_reactor_context:stop(Ctx1),
py_reactor_context:stop(Ctx2),
%% Verify isolation: Ctx1 echoes unchanged, Ctx2 uppercases
%% On some platforms (e.g., FreeBSD), worker contexts may not
%% properly isolate module globals, causing both to use the same factory.
case {Response1, Response2} of
{<<"hello">>, <<"HELLO">>} ->
%% Isolation works correctly
ok;
{<<"HELLO">>, <<"HELLO">>} ->
%% Both used UpperProtocol - isolation not working
{skip, "Worker module isolation not supported on this platform"};
{<<"hello">>, <<"hello">>} ->
%% Both used EchoProtocol - isolation not working
{skip, "Worker module isolation not supported on this platform"};
Other ->
ct:fail({unexpected_responses, Other})
end.
%% Helper to create a socketpair using gen_tcp
create_socketpair() ->
{ok, LSock} = gen_tcp:listen(0, [binary, {active, false}, {reuseaddr, true}]),
{ok, Port} = inet:port(LSock),
{ok, Client} = gen_tcp:connect("127.0.0.1", Port, [binary, {active, false}]),
{ok, Server} = gen_tcp:accept(LSock, 1000),
gen_tcp:close(LSock),
{ok, {Server, Client}}.
%% Helper to get raw fd from socket
get_fd(Socket) ->
{ok, Fd} = inet:getfd(Socket),
Fd.