summary refs log tree commit diff
path: root/tests/api/test_ratelimiting.py
blob: dbdd427cac2233ef0c65005ad219b02f413d1eb3 (plain) (blame)
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
37
38
39
from synapse.api.ratelimiting import Ratelimiter

from tests import unittest


class TestRatelimiter(unittest.TestCase):
    def test_allowed(self):
        limiter = Ratelimiter()
        allowed, time_allowed = limiter.can_do_action(
            key="test_id", time_now_s=0, rate_hz=0.1, burst_count=1
        )
        self.assertTrue(allowed)
        self.assertEquals(10.0, time_allowed)

        allowed, time_allowed = limiter.can_do_action(
            key="test_id", time_now_s=5, rate_hz=0.1, burst_count=1
        )
        self.assertFalse(allowed)
        self.assertEquals(10.0, time_allowed)

        allowed, time_allowed = limiter.can_do_action(
            key="test_id", time_now_s=10, rate_hz=0.1, burst_count=1
        )
        self.assertTrue(allowed)
        self.assertEquals(20.0, time_allowed)

    def test_pruning(self):
        limiter = Ratelimiter()
        allowed, time_allowed = limiter.can_do_action(
            key="test_id_1", time_now_s=0, rate_hz=0.1, burst_count=1
        )

        self.assertIn("test_id_1", limiter.message_counts)

        allowed, time_allowed = limiter.can_do_action(
            key="test_id_2", time_now_s=10, rate_hz=0.1, burst_count=1
        )

        self.assertNotIn("test_id_1", limiter.message_counts)