Skip to content

Commit e9488b1

Browse files
committed
add delay measurement
1 parent 16a63ac commit e9488b1

File tree

1 file changed

+97
-30
lines changed

1 file changed

+97
-30
lines changed

tests/test_v02.py

Lines changed: 97 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23

34
import pytest
45
from codeboxapi import CodeBox, ExecChunk, ExecResult, RemoteFile
@@ -157,65 +158,131 @@ async def test_async_list_operations(codebox: CodeBox):
157158

158159

159160
def test_sync_stream_exec(codebox: CodeBox):
160-
chunks = list(
161-
codebox.stream_exec(
162-
"import time;\nfor i in range(3): time.sleep(0.01); print(i)"
163-
)
164-
)
161+
chunks: list[tuple[ExecChunk, float]] = []
162+
t0 = time.perf_counter()
163+
for chunk in codebox.stream_exec(
164+
"import time;\nfor i in range(3): time.sleep(0.01); print(i)"
165+
):
166+
chunks.append((chunk, time.perf_counter() - t0))
167+
165168
assert (
166169
len(chunks) == 3
167170
), "iterating over stream_exec should produce 3 chunks (ipython)"
168171
assert all(
169-
isinstance(chunk, ExecChunk) for chunk in chunks
172+
isinstance(chunk[0], ExecChunk) for chunk in chunks
170173
), "All items should be ExecChunk instances (ipython)"
171174
assert all(
172-
chunk.type == "txt" for chunk in chunks
175+
chunk[0].type == "txt" for chunk in chunks
173176
), "All chunks should be of type 'txt' (ipython)"
174-
assert [chunk.content.strip() for chunk in chunks] == [
177+
assert [chunk[0].content.strip() for chunk in chunks] == [
175178
"0",
176179
"1",
177180
"2",
178181
], "Chunks should contain correct content (ipython)"
179-
chunks = list(
180-
codebox.stream_exec(
181-
"python -c 'import time\nfor i in range(3): time.sleep(0.01); print(i)'",
182-
kernel="bash",
183-
)
184-
)
182+
# Verify chunks arrive with delay
183+
assert all(
184+
chunks[i][1] < chunks[i + 1][1] for i in range(len(chunks) - 1)
185+
), "Chunks should arrive with delay (ipython)"
186+
# Verify delay is approximately 0.01s
187+
assert all(
188+
abs(chunks[i + 1][1] - chunks[i][1] - 0.01) < 0.005
189+
for i in range(len(chunks) - 1)
190+
), "Delay between chunks should be approximately 0.01s (ipython)"
191+
192+
chunks = []
193+
t0 = time.perf_counter()
194+
for chunk in codebox.stream_exec(
195+
"python -u -c 'import time\nfor i in range(3): time.sleep(0.01); print(i)'",
196+
kernel="bash",
197+
):
198+
chunks.append((chunk, time.perf_counter() - t0))
199+
185200
assert len(chunks) == 3, "iterating over stream_exec should produce 3 chunks (bash)"
186201
assert all(
187-
isinstance(chunk, ExecChunk) for chunk in chunks
202+
isinstance(chunk[0], ExecChunk) for chunk in chunks
188203
), "All items should be ExecChunk instances (bash)"
189204
assert all(
190-
chunk.type == "txt" for chunk in chunks
205+
chunk[0].type == "txt" for chunk in chunks
191206
), "All chunks should be of type 'txt' (bash)"
192-
assert [chunk.content.strip() for chunk in chunks] == [
207+
assert [chunk[0].content.strip() for chunk in chunks] == [
193208
"0",
194209
"1",
195210
"2",
196211
], "Chunks should contain correct content (bash)"
212+
# Verify chunks arrive with delay
213+
assert all(
214+
chunks[i][1] < chunks[i + 1][1] for i in range(len(chunks) - 1)
215+
), "Chunks should arrive with delay (bash)"
216+
# Verify delay is approximately 0.01s
217+
assert all(
218+
abs(chunks[i + 1][1] - chunks[i][1] - 0.01) < 0.005
219+
for i in range(len(chunks) - 1)
220+
), "Delay between chunks should be approximately 0.01s (bash)"
197221

198222

199223
@pytest.mark.asyncio
200224
async def test_async_stream_exec(codebox: CodeBox):
201-
chunks = [
202-
chunk
203-
async for chunk in codebox.astream_exec(
204-
"import time;\nfor i in range(3): time.sleep(0.01); print(i)"
205-
)
206-
]
207-
assert len(chunks) == 3, "Stream should produce 3 chunks"
225+
chunks: list[tuple[ExecChunk, float]] = []
226+
t0 = time.perf_counter()
227+
async for chunk in codebox.astream_exec(
228+
"import time;\nfor i in range(3): time.sleep(0.01); print(i)"
229+
):
230+
chunks.append((chunk, time.perf_counter() - t0))
231+
232+
assert (
233+
len(chunks) == 3
234+
), "iterating over stream_exec should produce 3 chunks (ipython)"
235+
assert all(
236+
isinstance(chunk[0], ExecChunk) for chunk in chunks
237+
), "All items should be ExecChunk instances (ipython)"
238+
assert all(
239+
chunk[0].type == "txt" for chunk in chunks
240+
), "All chunks should be of type 'txt' (ipython)"
241+
assert [chunk[0].content.strip() for chunk in chunks] == [
242+
"0",
243+
"1",
244+
"2",
245+
], "Chunks should contain correct content (ipython)"
246+
# Verify chunks arrive with delay
208247
assert all(
209-
isinstance(chunk, ExecChunk) for chunk in chunks
210-
), "All items should be ExecChunk instances"
248+
chunks[i][1] < chunks[i + 1][1] for i in range(len(chunks) - 1)
249+
), "Chunks should arrive with delay (ipython)"
250+
# Verify delay is approximately 0.03s
251+
211252
assert all(
212-
chunk.type == "txt" for chunk in chunks
213-
), "All chunks should be of type 'txt'"
214-
assert [chunk.content.strip() for chunk in chunks] == [
253+
abs(chunks[i + 1][1] - chunks[i][1] - 0.01) < 0.005
254+
for i in range(len(chunks) - 1)
255+
), "Delay between chunks should be approximately 0.01s (ipython)"
256+
257+
chunks = []
258+
t0 = time.perf_counter()
259+
async for chunk in codebox.astream_exec(
260+
"python -u -c 'import time\nfor i in range(3): time.sleep(0.01); print(i)'",
261+
kernel="bash",
262+
):
263+
chunks.append((chunk, time.perf_counter() - t0))
264+
265+
assert len(chunks) == 3, "iterating over stream_exec should produce 3 chunks (bash)"
266+
assert all(
267+
isinstance(chunk[0], ExecChunk) for chunk in chunks
268+
), "All items should be ExecChunk instances (bash)"
269+
assert all(
270+
chunk[0].type == "txt" for chunk in chunks
271+
), "All chunks should be of type 'txt' (bash)"
272+
assert [chunk[0].content.strip() for chunk in chunks] == [
215273
"0",
216274
"1",
217275
"2",
218-
], "Chunks should contain correct content"
276+
], "Chunks should contain correct content (bash)"
277+
# Verify chunks arrive with delay
278+
assert all(
279+
chunks[i][1] < chunks[i + 1][1] for i in range(len(chunks) - 1)
280+
), "Chunks should arrive with delay (bash)"
281+
# Verify delay is approximately 0.01s
282+
assert all(
283+
abs(chunks[i + 1][1] - chunks[i][1] - 0.01) < 0.005
284+
for i in range(len(chunks) - 1)
285+
), "Delay between chunks should be approximately 0.01s (bash)"
219286

220287

221288
def test_sync_error_handling(codebox: CodeBox):

0 commit comments

Comments
 (0)