Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add missing types to tests.util. #14597

Merged
merged 16 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add missing types to test_batching_queue.
  • Loading branch information
clokep committed Dec 1, 2022
commit b320a12480bbaa93e5db4586a7cebad04be84545
4 changes: 3 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ exclude = (?x)
|tests/server_notices/test_resource_limits_server_notices.py
|tests/test_state.py
|tests/test_terms_auth.py
|tests/util/test_batching_queue.py
|tests/util/test_dict_cache.py
|tests/util/test_expiring_cache.py
|tests/util/test_file_consumer.py
Expand Down Expand Up @@ -139,6 +138,9 @@ disallow_untyped_defs = False
[mypy-tests.util.test_async_helpers]
disallow_untyped_defs = True

[mypy-tests.util.test_batching_queue]
disallow_untyped_defs = True

[mypy-tests.utils]
disallow_untyped_defs = True

Expand Down
30 changes: 18 additions & 12 deletions tests/util/test_batching_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List, Tuple

from prometheus_client import Gauge

from twisted.internet import defer

from synapse.logging.context import make_deferred_yieldable
Expand All @@ -26,7 +30,7 @@


class BatchingQueueTestCase(TestCase):
def setUp(self):
def setUp(self) -> None:
self.clock, hs_clock = get_clock()

# We ensure that we remove any existing metrics for "test_queue".
Expand All @@ -37,25 +41,27 @@ def setUp(self):
except KeyError:
pass

self._pending_calls = []
self.queue = BatchingQueue("test_queue", hs_clock, self._process_queue)
self._pending_calls: List[Tuple[List[str], defer.Deferred]] = []
self.queue: BatchingQueue[str, str] = BatchingQueue(
"test_queue", hs_clock, self._process_queue
)

async def _process_queue(self, values):
d = defer.Deferred()
async def _process_queue(self, values: List[str]) -> str:
d: "defer.Deferred[str]" = defer.Deferred()
self._pending_calls.append((values, d))
return await make_deferred_yieldable(d)

def _get_sample_with_name(self, metric, name) -> int:
def _get_sample_with_name(self, metric: Gauge, name: str) -> float:
"""For a prometheus metric get the value of the sample that has a
matching "name" label.
"""
for sample in metric.collect()[0].samples:
for sample in next(iter(metric.collect())).samples:
if sample.labels.get("name") == name:
return sample.value

self.fail("Found no matching sample")

def _assert_metrics(self, queued, keys, in_flight):
def _assert_metrics(self, queued: int, keys: int, in_flight: int) -> None:
"""Assert that the metrics are correct"""

sample = self._get_sample_with_name(number_queued, self.queue._name)
Expand All @@ -75,7 +81,7 @@ def _assert_metrics(self, queued, keys, in_flight):
"number_in_flight",
)

def test_simple(self):
def test_simple(self) -> None:
"""Tests the basic case of calling `add_to_queue` once and having
`_process_queue` return.
"""
Expand Down Expand Up @@ -106,7 +112,7 @@ def test_simple(self):

self._assert_metrics(queued=0, keys=0, in_flight=0)

def test_batching(self):
def test_batching(self) -> None:
"""Test that multiple calls at the same time get batched up into one
call to `_process_queue`.
"""
Expand Down Expand Up @@ -134,7 +140,7 @@ def test_batching(self):
self.assertEqual(self.successResultOf(queue_d2), "bar")
self._assert_metrics(queued=0, keys=0, in_flight=0)

def test_queuing(self):
def test_queuing(self) -> None:
"""Test that we queue up requests while a `_process_queue` is being
called.
"""
Expand Down Expand Up @@ -184,7 +190,7 @@ def test_queuing(self):
self.assertEqual(self.successResultOf(queue_d3), "bar2")
self._assert_metrics(queued=0, keys=0, in_flight=0)

def test_different_keys(self):
def test_different_keys(self) -> None:
"""Test that calls to different keys get processed in parallel."""

self.assertFalse(self._pending_calls)
Expand Down