@@ -145,129 +145,146 @@ async def test_async_list_operations(codebox: CodeBox):
145145def test_sync_stream_exec (codebox : CodeBox ):
146146 chunks : list [tuple [ExecChunk , float ]] = []
147147 t0 = time .perf_counter ()
148+ sleep = 0.5
149+ if codebox .api_key == "local" :
150+ sleep = 0.01
151+ if codebox .api_key == "docker" :
152+ sleep = 0.05
148153 for chunk in codebox .stream_exec (
149- "import time;\n for i in range(3): time.sleep(0.01 ); print(i)"
154+ f "import time;\n for i in range(3): time.sleep({ sleep } ); print(i)"
150155 ):
151156 chunks .append ((chunk , time .perf_counter () - t0 ))
152157
153- assert (
154- len (chunks ) == 3
155- ), "iterating over stream_exec should produce 3 chunks (ipython)"
158+ assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks"
156159 assert all (
157160 isinstance (chunk [0 ], ExecChunk ) for chunk in chunks
158- ), "All items should be ExecChunk instances (ipython) "
161+ ), "All items should be ExecChunk instances"
159162 assert all (
160163 chunk [0 ].type == "txt" for chunk in chunks
161- ), "All chunks should be of type 'txt' (ipython) "
164+ ), "All chunks should be of type 'txt'"
162165 assert [chunk [0 ].content .strip () for chunk in chunks ] == [
163166 "0" ,
164167 "1" ,
165168 "2" ,
166- ], "Chunks should contain correct content (ipython) "
169+ ], "Chunks should contain correct content"
167170 # Verify chunks arrive with delay
168171 assert all (
169172 chunks [i ][1 ] < chunks [i + 1 ][1 ] for i in range (len (chunks ) - 1 )
170- ), "Chunks should arrive with delay (ipython) "
171- # Verify delay is approximately 0.01s
172- assert all (
173- abs ( chunks [i + 1 ][1 ] - chunks [i ][1 ] - 0.01 ) < 0.005
174- for i in range ( len ( chunks ) - 1 )
175- ), "Delay between chunks should be approximately 0.01s (ipython)"
173+ ), "Chunks should arrive with delay"
174+ # Verify chunks don't arrive all at once
175+ assert any (
176+ chunks [i + 1 ][1 ] - chunks [i ][1 ] > 0.005 for i in range ( len ( chunks ) - 1 )
177+ ), "At least some chunks should have noticeable delay between them"
178+
176179
180+ @pytest .mark .asyncio
181+ async def test_sync_stream_exec_ipython (codebox : CodeBox ):
177182 chunks = []
178183 t0 = time .perf_counter ()
184+ sleep = 0.5
185+ if codebox .api_key == "local" :
186+ sleep = 0.01
187+ if codebox .api_key == "docker" :
188+ sleep = 0.05
179189 for chunk in codebox .stream_exec (
180- "python -u -c 'import time\n for i in range(3): time.sleep(0.01 ); print(i)'" ,
190+ f "python -u -c 'import time\n for i in range(3): time.sleep({ sleep } ); print(i)'" ,
181191 kernel = "bash" ,
182192 ):
183193 chunks .append ((chunk , time .perf_counter () - t0 ))
184194
185- assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks (bash) "
195+ assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks"
186196 assert all (
187197 isinstance (chunk [0 ], ExecChunk ) for chunk in chunks
188- ), "All items should be ExecChunk instances (bash) "
198+ ), "All items should be ExecChunk instances"
189199 assert all (
190200 chunk [0 ].type == "txt" for chunk in chunks
191- ), "All chunks should be of type 'txt' (bash) "
201+ ), "All chunks should be of type 'txt'"
192202 assert [chunk [0 ].content .strip () for chunk in chunks ] == [
193203 "0" ,
194204 "1" ,
195205 "2" ,
196- ], "Chunks should contain correct content (bash) "
206+ ], "Chunks should contain correct content"
197207 # Verify chunks arrive with delay
198208 assert all (
199209 chunks [i ][1 ] < chunks [i + 1 ][1 ] for i in range (len (chunks ) - 1 )
200- ), "Chunks should arrive with delay (bash)"
201- # Verify delay is approximately 0.01s
202- assert all (
203- abs (chunks [i + 1 ][1 ] - chunks [i ][1 ] - 0.01 ) < 0.005
204- for i in range (len (chunks ) - 1 )
205- ), "Delay between chunks should be approximately 0.01s (bash)"
210+ ), "Chunks should arrive with delay"
211+ # Verify chunks don't arrive all at once
212+ assert any (
213+ chunks [i + 1 ][1 ] - chunks [i ][1 ] > 0.005 for i in range (len (chunks ) - 1 )
214+ ), "At least some chunks should have noticeable delay between them"
206215
207216
208217@pytest .mark .asyncio
209- async def test_async_stream_exec (codebox : CodeBox ):
218+ async def test_async_stream_exec_ipython (codebox : CodeBox ):
210219 chunks : list [tuple [ExecChunk , float ]] = []
211220 t0 = time .perf_counter ()
221+ sleep = 0.5
222+ if codebox .api_key == "local" :
223+ sleep = 0.01
224+ if codebox .api_key == "docker" :
225+ sleep = 0.05
212226 async for chunk in codebox .astream_exec (
213- "import time;\n for i in range(3): time.sleep(0.01 ); print(i)"
227+ f "import time;\n for i in range(3): time.sleep({ sleep } ); print(i)",
214228 ):
215229 chunks .append ((chunk , time .perf_counter () - t0 ))
216230
217- assert (
218- len (chunks ) == 3
219- ), "iterating over stream_exec should produce 3 chunks (ipython)"
231+ assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks"
220232 assert all (
221233 isinstance (chunk [0 ], ExecChunk ) for chunk in chunks
222- ), "All items should be ExecChunk instances (ipython) "
234+ ), "All items should be ExecChunk instances"
223235 assert all (
224236 chunk [0 ].type == "txt" for chunk in chunks
225- ), "All chunks should be of type 'txt' (ipython) "
237+ ), "All chunks should be of type 'txt'"
226238 assert [chunk [0 ].content .strip () for chunk in chunks ] == [
227239 "0" ,
228240 "1" ,
229241 "2" ,
230- ], "Chunks should contain correct content (ipython) "
242+ ], "Chunks should contain correct content"
231243 # Verify chunks arrive with delay
232244 assert all (
233245 chunks [i ][1 ] < chunks [i + 1 ][1 ] for i in range (len (chunks ) - 1 )
234- ), "Chunks should arrive with delay (ipython)"
235- # Verify delay is approximately 0.01s
236- print ([abs (chunks [i + 1 ][1 ] - chunks [i ][1 ] - 0.01 ) for i in range (len (chunks ) - 1 )])
237- assert all (
238- abs (chunks [i + 1 ][1 ] - chunks [i ][1 ] - 0.01 ) < 0.005
239- for i in range (len (chunks ) - 1 )
240- ), "Delay between chunks should be approximately 0.01s (ipython)"
246+ ), "Chunks should arrive with delay"
247+ # Verify chunks don't arrive all at once
248+ assert any (
249+ chunks [i + 1 ][1 ] - chunks [i ][1 ] > 0.005 for i in range (len (chunks ) - 1 )
250+ ), "At least some chunks should have noticeable delay between them"
251+
241252
253+ @pytest .mark .asyncio
254+ async def test_async_stream_exec_bash (codebox : CodeBox ):
242255 chunks = []
243256 t0 = time .perf_counter ()
257+ sleep = 0.5
258+ if codebox .api_key == "local" :
259+ sleep = 0.01
260+ if codebox .api_key == "docker" :
261+ sleep = 0.05
244262 async for chunk in codebox .astream_exec (
245- "python -u -c 'import time\n for i in range(3): time.sleep(0.01 ); print(i)'" ,
263+ f "python -u -c 'import time\n for i in range(3): time.sleep({ sleep } ); print(i)'" ,
246264 kernel = "bash" ,
247265 ):
248266 chunks .append ((chunk , time .perf_counter () - t0 ))
249267
250- assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks (bash) "
268+ assert len (chunks ) == 3 , "iterating over stream_exec should produce 3 chunks"
251269 assert all (
252270 isinstance (chunk [0 ], ExecChunk ) for chunk in chunks
253- ), "All items should be ExecChunk instances (bash) "
271+ ), "All items should be ExecChunk instances"
254272 assert all (
255273 chunk [0 ].type == "txt" for chunk in chunks
256- ), "All chunks should be of type 'txt' (bash) "
274+ ), "All chunks should be of type 'txt'"
257275 assert [chunk [0 ].content .strip () for chunk in chunks ] == [
258276 "0" ,
259277 "1" ,
260278 "2" ,
261- ], "Chunks should contain correct content (bash) "
279+ ], "Chunks should contain correct content"
262280 # Verify chunks arrive with delay
263281 assert all (
264282 chunks [i ][1 ] < chunks [i + 1 ][1 ] for i in range (len (chunks ) - 1 )
265- ), "Chunks should arrive with delay (bash)"
266- # Verify delay is approximately 0.01s
267- assert all (
268- abs (chunks [i + 1 ][1 ] - chunks [i ][1 ] - 0.01 ) < 0.005
269- for i in range (len (chunks ) - 1 )
270- ), "Delay between chunks should be approximately 0.01s (bash)"
283+ ), "Chunks should arrive with delay"
284+ # Verify chunks don't arrive all at once
285+ assert any (
286+ chunks [i + 1 ][1 ] - chunks [i ][1 ] > 0.005 for i in range (len (chunks ) - 1 )
287+ ), "At least some chunks should have noticeable delay between them"
271288
272289
273290def test_sync_error_handling (codebox : CodeBox ):
0 commit comments