|
1 | 1 | """Test GatewayClient"""
|
| 2 | +import asyncio |
2 | 3 | import json
|
| 4 | +import logging |
3 | 5 | import os
|
4 | 6 | import uuid
|
5 | 7 | from datetime import datetime
|
6 | 8 | from io import BytesIO
|
7 |
| -from unittest.mock import patch |
| 9 | +from queue import Empty |
| 10 | +from unittest.mock import MagicMock, patch |
8 | 11 |
|
9 | 12 | import pytest
|
10 | 13 | import tornado
|
11 | 14 | from tornado.httpclient import HTTPRequest, HTTPResponse
|
12 | 15 | from tornado.web import HTTPError
|
13 | 16 |
|
14 |
| -from jupyter_server.gateway.managers import GatewayClient |
| 17 | +from jupyter_server.gateway.managers import ChannelQueue, GatewayClient |
15 | 18 | from jupyter_server.utils import ensure_async
|
16 | 19 |
|
17 | 20 | from .utils import expected_http_error
|
@@ -318,6 +321,37 @@ async def test_gateway_shutdown(init_gateway, jp_serverapp, jp_fetch, missing_ke
|
318 | 321 | assert await is_kernel_running(jp_fetch, k2) is False
|
319 | 322 |
|
320 | 323 |
|
| 324 | +async def test_channel_queue_get_msg_with_invalid_timeout(): |
| 325 | + queue = ChannelQueue("iopub", MagicMock(), logging.getLogger()) |
| 326 | + |
| 327 | + with pytest.raises(ValueError): |
| 328 | + await queue.get_msg(timeout=-1) |
| 329 | + |
| 330 | + |
| 331 | +async def test_channel_queue_get_msg_raises_empty_after_timeout(): |
| 332 | + queue = ChannelQueue("iopub", MagicMock(), logging.getLogger()) |
| 333 | + |
| 334 | + with pytest.raises(Empty): |
| 335 | + await asyncio.wait_for(queue.get_msg(timeout=0.1), 2) |
| 336 | + |
| 337 | + |
| 338 | +async def test_channel_queue_get_msg_without_timeout(): |
| 339 | + queue = ChannelQueue("iopub", MagicMock(), logging.getLogger()) |
| 340 | + |
| 341 | + with pytest.raises(asyncio.TimeoutError): |
| 342 | + await asyncio.wait_for(queue.get_msg(timeout=None), 1) |
| 343 | + |
| 344 | + |
| 345 | +async def test_channel_queue_get_msg_with_existing_item(): |
| 346 | + sent_message = {"msg_id": 1, "msg_type": 2} |
| 347 | + queue = ChannelQueue("iopub", MagicMock(), logging.getLogger()) |
| 348 | + queue.put_nowait(sent_message) |
| 349 | + |
| 350 | + received_message = await asyncio.wait_for(queue.get_msg(timeout=None), 1) |
| 351 | + |
| 352 | + assert received_message == sent_message |
| 353 | + |
| 354 | + |
321 | 355 | #
|
322 | 356 | # Test methods below...
|
323 | 357 | #
|
|
0 commit comments