|
9 | 9 | from http.cookies import SimpleCookie
|
10 | 10 | from io import BytesIO
|
11 | 11 | from queue import Empty
|
12 |
| -from typing import Any, Union |
| 12 | +from typing import Any, Dict, Union |
13 | 13 | from unittest.mock import MagicMock, patch
|
14 | 14 |
|
15 | 15 | import pytest
|
@@ -62,6 +62,12 @@ def generate_kernelspec(name):
|
62 | 62 | # maintain a dictionary of expected running kernels. Key = kernel_id, Value = model.
|
63 | 63 | running_kernels = {}
|
64 | 64 |
|
| 65 | +# Dictionary of kernels to transiently omit from list results. |
| 66 | +# |
| 67 | +# This is used to simulate inconsistency in list results from the Gateway server |
| 68 | +# due to issues like race conditions, bugs, etc. |
| 69 | +omitted_kernels: Dict[str, bool] = {} |
| 70 | + |
65 | 71 |
|
66 | 72 | def generate_model(name):
|
67 | 73 | """Generate a mocked kernel model. Caller is responsible for adding model to running_kernels dictionary."""
|
@@ -131,8 +137,11 @@ async def mock_gateway_request(url, **kwargs): # noqa
|
131 | 137 | if endpoint.endswith("/api/kernels") and method == "GET":
|
132 | 138 | kernels = []
|
133 | 139 | for kernel_id in running_kernels:
|
134 |
| - model = running_kernels.get(kernel_id) |
135 |
| - kernels.append(model) |
| 140 | + if kernel_id in omitted_kernels: |
| 141 | + omitted_kernels.pop(kernel_id) |
| 142 | + else: |
| 143 | + model = running_kernels.get(kernel_id) |
| 144 | + kernels.append(model) |
136 | 145 | response_buf = BytesIO(json.dumps(kernels).encode("utf-8"))
|
137 | 146 | response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
|
138 | 147 | return response
|
@@ -453,6 +462,7 @@ async def test_gateway_session_lifecycle(init_gateway, jp_root_dir, jp_fetch, cu
|
453 | 462 |
|
454 | 463 | assert await is_session_active(jp_fetch, session_id) is True
|
455 | 464 |
|
| 465 | + omitted_kernels[kernel_id] = True |
456 | 466 | if cull_kernel:
|
457 | 467 | running_kernels.pop(kernel_id)
|
458 | 468 |
|
@@ -501,6 +511,7 @@ async def test_gateway_kernel_lifecycle(
|
501 | 511 | # ensure kernel still considered running
|
502 | 512 | assert await is_kernel_running(jp_fetch, kernel_id) is True
|
503 | 513 |
|
| 514 | + omitted_kernels[kernel_id] = True |
504 | 515 | if cull_kernel:
|
505 | 516 | running_kernels.pop(kernel_id)
|
506 | 517 |
|
|
0 commit comments