|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from statsd import BaseAsyncStatsdClient, DebugAsyncStatsdClient |
| 10 | + |
| 11 | + |
| 12 | +class MockClient(BaseAsyncStatsdClient): |
| 13 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 14 | + super().__init__(*args, **kwargs) |
| 15 | + self.mock = mock.Mock() |
| 16 | + |
| 17 | + async def _emit(self, packets: list[str]) -> None: |
| 18 | + if packets: |
| 19 | + self.mock(packets) |
| 20 | + |
| 21 | + def assert_emitted(self, expected: list[str] | str) -> None: |
| 22 | + self.mock.assert_called_once_with( |
| 23 | + expected if isinstance(expected, list) else [expected], |
| 24 | + ) |
| 25 | + |
| 26 | + def assert_did_not_emit(self) -> None: |
| 27 | + self.mock.assert_not_called() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.asyncio() |
| 31 | +async def test_timed_decorator() -> None: |
| 32 | + client = MockClient() |
| 33 | + |
| 34 | + @client.timed("foo", tags={"foo": "1"}) |
| 35 | + async def fn() -> None: |
| 36 | + pass |
| 37 | + |
| 38 | + with mock.patch( |
| 39 | + "time.perf_counter", |
| 40 | + side_effect=[7.886838544, 20.181117592], |
| 41 | + ): |
| 42 | + await fn() |
| 43 | + |
| 44 | + client.mock.assert_called_once_with(["foo:12294|ms|#foo:1"]) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio() |
| 48 | +async def test_timed_decorator_use_distribution() -> None: |
| 49 | + client = MockClient() |
| 50 | + |
| 51 | + @client.timed("foo", tags={"foo": "1"}, use_distribution=True) |
| 52 | + async def fn() -> None: |
| 53 | + pass |
| 54 | + |
| 55 | + with mock.patch( |
| 56 | + "time.perf_counter", |
| 57 | + side_effect=[7.886838544, 20.181117592], |
| 58 | + ): |
| 59 | + await fn() |
| 60 | + |
| 61 | + client.mock.assert_called_once_with(["foo:12294|d|#foo:1"]) |
| 62 | + |
| 63 | + |
| 64 | +SIMPLE_TEST_CASES: list[ |
| 65 | + tuple[str, tuple[Any, ...], dict[str, Any], list[str] | str] |
| 66 | +] = [ |
| 67 | + ("increment", ("foo",), {}, "foo:1|c"), |
| 68 | + ("increment", ("foo", 10), {}, "foo:10|c"), |
| 69 | +] |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + ("method", "args", "kwargs", "expected"), |
| 74 | + SIMPLE_TEST_CASES, |
| 75 | +) |
| 76 | +@pytest.mark.asyncio() |
| 77 | +async def test_debug_client_no_inner( |
| 78 | + method: str, |
| 79 | + args: tuple[Any, ...], |
| 80 | + kwargs: dict[str, Any], |
| 81 | + expected: list[str] | str, |
| 82 | + caplog: Any, |
| 83 | +) -> None: |
| 84 | + client = DebugAsyncStatsdClient() |
| 85 | + with caplog.at_level(logging.INFO, logger="statsd"): |
| 86 | + await getattr(client, method)(*args, **kwargs) |
| 87 | + |
| 88 | + if isinstance(expected, list): |
| 89 | + assert len(caplog.records) == len(expected) |
| 90 | + for x in expected: |
| 91 | + assert x in caplog.text |
| 92 | + else: |
| 93 | + assert len(caplog.records) == 1 |
| 94 | + assert expected in caplog.text |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + ("method", "args", "kwargs", "expected"), |
| 99 | + SIMPLE_TEST_CASES, |
| 100 | +) |
| 101 | +@pytest.mark.asyncio() |
| 102 | +async def test_debug_client( |
| 103 | + method: str, |
| 104 | + args: tuple[Any, ...], |
| 105 | + kwargs: dict[str, Any], |
| 106 | + expected: str | list[str], |
| 107 | +) -> None: |
| 108 | + inner = MockClient() |
| 109 | + client = DebugAsyncStatsdClient(inner=inner) |
| 110 | + await getattr(client, method)(*args, **kwargs) |
| 111 | + inner.assert_emitted(expected) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize( |
| 115 | + ("method", "args", "kwargs", "expected"), |
| 116 | + SIMPLE_TEST_CASES, |
| 117 | +) |
| 118 | +@pytest.mark.asyncio() |
| 119 | +async def test_debug_client_custom_logger_and_level( |
| 120 | + method: str, |
| 121 | + args: tuple[Any, ...], |
| 122 | + kwargs: dict[str, Any], |
| 123 | + expected: list[str] | str, |
| 124 | + caplog: Any, |
| 125 | +) -> None: |
| 126 | + client = DebugAsyncStatsdClient( |
| 127 | + logger=logging.getLogger("foo"), |
| 128 | + level=logging.DEBUG, |
| 129 | + ) |
| 130 | + with caplog.at_level(logging.DEBUG, logger="foo"): |
| 131 | + await getattr(client, method)(*args, **kwargs) |
| 132 | + |
| 133 | + if isinstance(expected, list): |
| 134 | + assert len(caplog.records) == len(expected) |
| 135 | + for x in expected: |
| 136 | + assert x in caplog.text |
| 137 | + else: |
| 138 | + assert len(caplog.records) == 1 |
| 139 | + assert expected in caplog.text |
0 commit comments