-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_erlang_sleep_SUITE.erl
More file actions
176 lines (149 loc) · 4.38 KB
/
py_erlang_sleep_SUITE.erl
File metadata and controls
176 lines (149 loc) · 4.38 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
%% @doc Tests for erlang.sleep() and asyncio integration.
%%
%% Tests the erlang.sleep() function and erlang module asyncio integration.
-module(py_erlang_sleep_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([
test_erlang_sleep_available/1,
test_erlang_sleep_basic/1,
test_erlang_sleep_zero/1,
test_erlang_sleep_accuracy/1,
test_erlang_run_module/1,
test_erlang_asyncio_gather/1,
test_erlang_asyncio_wait_for/1,
test_erlang_asyncio_create_task/1
]).
all() ->
[
test_erlang_sleep_available,
test_erlang_sleep_basic,
test_erlang_sleep_zero,
test_erlang_sleep_accuracy,
test_erlang_run_module,
test_erlang_asyncio_gather,
test_erlang_asyncio_wait_for,
test_erlang_asyncio_create_task
].
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(erlang_python),
{ok, _} = py:start_contexts(),
timer:sleep(500),
Config.
end_per_suite(_Config) ->
ok.
%% Test that erlang.sleep is available
test_erlang_sleep_available(_Config) ->
ok = py:exec(<<"
import erlang
result = hasattr(erlang, 'sleep')
assert result, 'erlang.sleep not found'
">>),
ct:pal("erlang.sleep is available"),
ok.
%% Test basic sleep functionality (sync context via callback)
test_erlang_sleep_basic(_Config) ->
ok = py:exec(<<"
import erlang
# Test basic sleep in sync context - should not raise
erlang.sleep(0.01) # 10ms
">>),
ct:pal("Basic sleep completed"),
ok.
%% Test zero/negative delay returns immediately
test_erlang_sleep_zero(_Config) ->
ok = py:exec(<<"
import erlang
import time
start = time.time()
erlang.sleep(0)
elapsed = (time.time() - start) * 1000
# Should return immediately (< 10ms accounting for Python overhead)
assert elapsed < 10, f'Zero sleep was slow: {elapsed}ms'
">>),
ct:pal("Zero sleep returned fast"),
ok.
%% Test sleep accuracy
test_erlang_sleep_accuracy(_Config) ->
ok = py:exec(<<"
import erlang
import time
delays = [0.01, 0.05, 0.1] # seconds
for delay in delays:
start = time.time()
erlang.sleep(delay)
elapsed = time.time() - start
# Allow wide tolerance for CI runners (can be slow/unpredictable)
assert delay * 0.5 <= elapsed <= delay * 10.0, \\
f'{delay}s sleep took {elapsed:.3f}s'
">>),
ct:pal("Sleep accuracy within tolerance"),
ok.
%% Test erlang.run() with asyncio
test_erlang_run_module(_Config) ->
ok = py:exec(<<"
import erlang
import asyncio
# Test erlang module has expected functions for event loop integration
funcs = ['run', 'new_event_loop', 'EventLoopPolicy', 'sleep']
for f in funcs:
assert hasattr(erlang, f), f'erlang missing {f}'
# Test run() with asyncio.sleep
async def test_sleep():
await asyncio.sleep(0.01) # 10ms
return 'done'
result = erlang.run(test_sleep())
assert result == 'done', f'Expected done, got {result}'
">>),
ct:pal("erlang.run() with asyncio works"),
ok.
%% Test asyncio.gather with erlang.run()
test_erlang_asyncio_gather(_Config) ->
ok = py:exec(<<"
import erlang
import asyncio
async def task(n):
await asyncio.sleep(0.01)
return n * 2
async def main():
results = await asyncio.gather(task(1), task(2), task(3))
assert results == [2, 4, 6], f'Expected [2, 4, 6], got {results}'
erlang.run(main())
">>),
ct:pal("asyncio.gather with erlang.run() works"),
ok.
%% Test asyncio.wait_for with timeout
test_erlang_asyncio_wait_for(_Config) ->
ok = py:exec(<<"
import erlang
import asyncio
async def fast_task():
await asyncio.sleep(0.01)
return 'fast'
async def main():
# Should complete before timeout
result = await asyncio.wait_for(fast_task(), timeout=1.0)
assert result == 'fast', f'Expected fast, got {result}'
erlang.run(main())
">>),
ct:pal("asyncio.wait_for with erlang.run() works"),
ok.
%% Test asyncio.create_task with erlang.run()
test_erlang_asyncio_create_task(_Config) ->
ok = py:exec(<<"
import erlang
import asyncio
async def background():
await asyncio.sleep(0.01)
return 'background_done'
async def main():
task = asyncio.create_task(background())
# Do some other work
await asyncio.sleep(0.005)
# Wait for task
result = await task
assert result == 'background_done', f'Expected background_done, got {result}'
erlang.run(main())
">>),
ct:pal("asyncio.create_task with erlang.run() works"),
ok.