-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathtest_async_callbacks.py
More file actions
54 lines (39 loc) · 1.52 KB
/
test_async_callbacks.py
File metadata and controls
54 lines (39 loc) · 1.52 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
from e2b_code_interpreter.code_interpreter_async import AsyncSandbox
def async_append_fn(items):
async def async_append(item):
items.append(item)
return async_append
async def test_results(async_sandbox: AsyncSandbox):
results = []
execution = await async_sandbox.run_code(
"x = 1;x", on_result=async_append_fn(results)
)
assert len(results) == 1
assert execution.results[0].text == "1"
async def test_results_sync_callback(async_sandbox: AsyncSandbox):
results = []
execution = await async_sandbox.run_code(
"x = 1;x", on_result=lambda result: results.append(result)
)
assert len(results) == 1
assert execution.results[0].text == "1"
async def test_error(async_sandbox: AsyncSandbox):
errors = []
execution = await async_sandbox.run_code("xyz", on_error=async_append_fn(errors))
assert len(errors) == 1
assert execution.error.name == "NameError"
async def test_stdout(async_sandbox: AsyncSandbox):
stdout = []
execution = await async_sandbox.run_code(
"print('Hello from e2b')", on_stdout=async_append_fn(stdout)
)
assert len(stdout) == 1
assert execution.logs.stdout == ["Hello from e2b\n"]
async def test_stderr(async_sandbox: AsyncSandbox):
stderr = []
execution = await async_sandbox.run_code(
'import sys;print("This is an error message", file=sys.stderr)',
on_stderr=async_append_fn(stderr),
)
assert len(stderr) == 1
assert execution.logs.stderr == ["This is an error message\n"]