|
1 | 1 | import os |
| 2 | +import time |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | from codeboxapi import CodeBox, ExecChunk, ExecResult, RemoteFile |
@@ -157,65 +158,131 @@ async def test_async_list_operations(codebox: CodeBox): |
157 | 158 |
|
158 | 159 |
|
159 | 160 | 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 | + |
165 | 168 | assert ( |
166 | 169 | len(chunks) == 3 |
167 | 170 | ), "iterating over stream_exec should produce 3 chunks (ipython)" |
168 | 171 | assert all( |
169 | | - isinstance(chunk, ExecChunk) for chunk in chunks |
| 172 | + isinstance(chunk[0], ExecChunk) for chunk in chunks |
170 | 173 | ), "All items should be ExecChunk instances (ipython)" |
171 | 174 | assert all( |
172 | | - chunk.type == "txt" for chunk in chunks |
| 175 | + chunk[0].type == "txt" for chunk in chunks |
173 | 176 | ), "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] == [ |
175 | 178 | "0", |
176 | 179 | "1", |
177 | 180 | "2", |
178 | 181 | ], "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 | + |
185 | 200 | assert len(chunks) == 3, "iterating over stream_exec should produce 3 chunks (bash)" |
186 | 201 | assert all( |
187 | | - isinstance(chunk, ExecChunk) for chunk in chunks |
| 202 | + isinstance(chunk[0], ExecChunk) for chunk in chunks |
188 | 203 | ), "All items should be ExecChunk instances (bash)" |
189 | 204 | assert all( |
190 | | - chunk.type == "txt" for chunk in chunks |
| 205 | + chunk[0].type == "txt" for chunk in chunks |
191 | 206 | ), "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] == [ |
193 | 208 | "0", |
194 | 209 | "1", |
195 | 210 | "2", |
196 | 211 | ], "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)" |
197 | 221 |
|
198 | 222 |
|
199 | 223 | @pytest.mark.asyncio |
200 | 224 | 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 |
208 | 247 | 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 | + |
211 | 252 | 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] == [ |
215 | 273 | "0", |
216 | 274 | "1", |
217 | 275 | "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)" |
219 | 286 |
|
220 | 287 |
|
221 | 288 | def test_sync_error_handling(codebox: CodeBox): |
|
0 commit comments