-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_async_e2e_SUITE.erl
More file actions
159 lines (134 loc) · 3.7 KB
/
py_async_e2e_SUITE.erl
File metadata and controls
159 lines (134 loc) · 3.7 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
%%% @doc End-to-end test suite for Python asyncio TCP integration.
-module(py_async_e2e_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([
test_asyncio_sleep/1,
test_asyncio_gather/1,
test_asyncio_tcp_echo/1,
test_asyncio_concurrent_tcp/1
]).
all() ->
[
test_asyncio_sleep,
test_asyncio_gather,
test_asyncio_tcp_echo,
test_asyncio_concurrent_tcp
].
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(erlang_python),
%% Ensure contexts are running
{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 - All imports included in the code block
%% ============================================================================
%% Test basic asyncio.sleep timing
test_asyncio_sleep(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import time
import erlang
async def timed_sleep():
start = time.monotonic()
await asyncio.sleep(0.05)
return time.monotonic() - start
elapsed = erlang.run(timed_sleep())
assert elapsed >= 0.04, f'Expected >= 0.04s, got {elapsed:.3f}s'
">>),
ok.
%% Test asyncio.gather runs tasks concurrently
test_asyncio_gather(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import time
import erlang
async def task(val):
await asyncio.sleep(0.05)
return val
async def main():
start = time.monotonic()
r = await asyncio.gather(task(1), task(2), task(3))
elapsed = time.monotonic() - start
assert list(r) == [1, 2, 3], f'Expected [1, 2, 3], got {list(r)}'
# Allow more time on CI (0.3s instead of 0.15s)
assert elapsed < 0.3, f'Expected < 0.3s, got {elapsed:.3f}s'
erlang.run(main())
">>),
ok.
%% Test TCP echo server/client using asyncio
test_asyncio_tcp_echo(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import erlang
async def handler(r, w):
data = await r.read(100)
w.write(data)
await w.drain()
w.close()
await w.wait_closed()
async def test():
srv = await asyncio.start_server(handler, '127.0.0.1', 0)
port = srv.sockets[0].getsockname()[1]
r, w = await asyncio.open_connection('127.0.0.1', port)
w.write(b'hello')
await w.drain()
resp = await r.read(100)
w.close()
await w.wait_closed()
srv.close()
await srv.wait_closed()
assert resp == b'hello', f'Expected b\"hello\", got {resp}'
erlang.run(test())
">>),
ok.
%% Test multiple concurrent TCP connections
test_asyncio_concurrent_tcp(_Config) ->
Ctx = py:context(1),
ok = py:exec(Ctx, <<"
import asyncio
import erlang
async def handler(r, w):
data = await r.read(100)
w.write(b're:' + data)
await w.drain()
w.close()
await w.wait_closed()
async def req(port, msg):
r, w = await asyncio.open_connection('127.0.0.1', port)
w.write(msg)
await w.drain()
resp = await r.read(100)
w.close()
await w.wait_closed()
return resp
async def test():
srv = await asyncio.start_server(handler, '127.0.0.1', 0)
port = srv.sockets[0].getsockname()[1]
results = await asyncio.gather(
req(port, b'1'),
req(port, b'2'),
req(port, b'3')
)
srv.close()
await srv.wait_closed()
assert set(results) == {b're:1', b're:2', b're:3'}, f'Expected {{b\"re:1\", b\"re:2\", b\"re:3\"}}, got {set(results)}'
erlang.run(test())
">>),
ok.