File: test_websocket_data_queue.py

package info (click to toggle)
python-aiohttp 3.11.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,156 kB
  • sloc: python: 51,898; ansic: 20,843; makefile: 395; javascript: 31; sh: 3
file content (36 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import asyncio
from unittest import mock

import pytest

from aiohttp._websocket.models import WSMessage, WSMsgType
from aiohttp._websocket.reader import WebSocketDataQueue
from aiohttp.base_protocol import BaseProtocol


@pytest.fixture
def protocol() -> BaseProtocol:
    return mock.create_autospec(BaseProtocol, spec_set=True, instance=True, _reading_paused=False)  # type: ignore[no-any-return]


@pytest.fixture
def buffer(
    loop: asyncio.AbstractEventLoop, protocol: BaseProtocol
) -> WebSocketDataQueue:
    return WebSocketDataQueue(protocol, limit=1, loop=loop)


class TestWebSocketDataQueue:
    def test_feed_pause(self, buffer: WebSocketDataQueue) -> None:
        buffer._protocol._reading_paused = False
        for _ in range(3):
            buffer.feed_data(WSMessage(data=b"x", type=WSMsgType.BINARY, extra=""), 1)

        assert buffer._protocol.pause_reading.called  # type: ignore[attr-defined]

    async def test_resume_on_read(self, buffer: WebSocketDataQueue) -> None:
        buffer.feed_data(WSMessage(data=b"x", type=WSMsgType.BINARY, extra=""), 1)

        buffer._protocol._reading_paused = True
        await buffer.read()
        assert buffer._protocol.resume_reading.called  # type: ignore[attr-defined]