Exemplo n.º 1
0
    def test_results_in(self):
        limiter = RateLimiter(storage.MemoryStorage(), "1 per minute")

        assert limiter.resets_in("foo") is None

        while limiter.hit("foo"):
            pass

        assert limiter.resets_in("foo") > datetime.timedelta(seconds=0)
        assert limiter.resets_in("foo") < datetime.timedelta(seconds=60)
Exemplo n.º 2
0
    def test_results_in(self, metrics):
        limiter = RateLimiter(storage.MemoryStorage(), "1 per minute", metrics=metrics)

        assert limiter.resets_in("foo") is None

        while limiter.hit("foo"):
            pass

        assert limiter.resets_in("foo") > datetime.timedelta(seconds=0)
        assert limiter.resets_in("foo") < datetime.timedelta(seconds=60)
Exemplo n.º 3
0
    def test_basic(self):
        limiter = RateLimiter(
            storage.MemoryStorage(),
            "1 per minute",
            identifiers=["foo"],
        )

        assert limiter.test("foo")
        assert limiter.test("bar")

        while limiter.hit("bar"):
            pass

        assert limiter.test("foo")
        assert not limiter.test("bar")
Exemplo n.º 4
0
    def test_namespacing(self):
        storage_ = storage.MemoryStorage()
        limiter1 = RateLimiter(storage_, "1 per minute", identifiers=["foo"])
        limiter2 = RateLimiter(storage_, "1 per minute")

        assert limiter1.test("bar")
        assert limiter2.test("bar")

        while limiter1.hit("bar"):
            pass

        assert limiter2.test("bar")
        assert not limiter1.test("bar")
Exemplo n.º 5
0
    def test_results_in_expired(self):
        limiter = RateLimiter(storage.MemoryStorage(),
                              "1 per minute; 1 per hour; 1 per day")

        current = datetime.datetime.now(tz=datetime.timezone.utc)
        stats = iter([
            (0, 0),
            ((current + datetime.timedelta(seconds=60)).timestamp(), 0),
            ((current + datetime.timedelta(seconds=5)).timestamp(), 0),
        ])

        limiter._window = pretend.stub(
            get_window_stats=lambda l, *a: next(stats))

        resets_in = limiter.resets_in("foo")

        assert resets_in > datetime.timedelta(seconds=0)
        assert resets_in <= datetime.timedelta(seconds=5)
Exemplo n.º 6
0
    def test_results_in_expired(self):
        limiter = RateLimiter(
            storage.MemoryStorage(),
            "1 per minute; 1 per hour; 1 per day",
        )

        current = datetime.datetime.now(tz=datetime.timezone.utc)
        stats = iter([
            (0, 0),
            ((current + datetime.timedelta(seconds=60)).timestamp(), 0),
            ((current + datetime.timedelta(seconds=5)).timestamp(), 0),
        ])

        limiter._window = pretend.stub(
            get_window_stats=lambda l, *a: next(stats),
        )

        resets_in = limiter.resets_in("foo")

        assert resets_in > datetime.timedelta(seconds=0)
        assert resets_in <= datetime.timedelta(seconds=5)
Exemplo n.º 7
0
    def test_error(self, metrics):
        limiter = RateLimiter(
            storage.MemoryStorage(),
            "1 per minute",
            identifiers=["foo"],
            metrics=metrics,
        )

        def raiser(*args, **kwargs):
            raise redis.ConnectionError()

        limiter._window = pretend.stub(hit=raiser,
                                       test=raiser,
                                       get_window_stats=raiser)

        assert limiter.test("foo")
        assert limiter.hit("foo")
        assert limiter.resets_in("foo") is None

        assert metrics.increment.calls == [
            pretend.call("warehouse.ratelimiter.error", tags=["call:test"]),
            pretend.call("warehouse.ratelimiter.error", tags=["call:hit"]),
            pretend.call("warehouse.ratelimiter.error",
                         tags=["call:resets_in"]),
        ]
Exemplo n.º 8
0
    def test_error(self, metrics):
        limiter = RateLimiter(
            storage.MemoryStorage(),
            "1 per minute",
            identifiers=["foo"],
            metrics=metrics,
        )

        def raiser(*args, **kwargs):
            raise redis.ConnectionError()

        limiter._window = pretend.stub(hit=raiser, test=raiser, get_window_stats=raiser)

        assert limiter.test("foo")
        assert limiter.hit("foo")
        assert limiter.resets_in("foo") is None

        assert metrics.increment.calls == [
            pretend.call("warehouse.ratelimiter.error", tags=["call:test"]),
            pretend.call("warehouse.ratelimiter.error", tags=["call:hit"]),
            pretend.call("warehouse.ratelimiter.error", tags=["call:resets_in"]),
        ]
Exemplo n.º 9
0
    def test_clear(self, metrics):
        limiter = RateLimiter(storage.MemoryStorage(),
                              "1 per minute",
                              metrics=metrics)

        assert limiter.test("foo")

        while limiter.hit("foo"):
            pass

        assert not limiter.test("foo")

        limiter.clear("foo")

        assert limiter.test("foo")
Exemplo n.º 10
0
    def test_namespacing(self):
        storage_ = storage.MemoryStorage()
        limiter1 = RateLimiter(storage_, "1 per minute", identifiers=["foo"])
        limiter2 = RateLimiter(storage_, "1 per minute")

        assert limiter1.test("bar")
        assert limiter2.test("bar")

        while limiter1.hit("bar"):
            pass

        assert limiter2.test("bar")
        assert not limiter1.test("bar")
Exemplo n.º 11
0
    def test_basic(self):
        limiter = RateLimiter(storage.MemoryStorage(),
                              "1 per minute",
                              identifiers=["foo"])

        assert limiter.test("foo")
        assert limiter.test("bar")

        while limiter.hit("bar"):
            pass

        assert limiter.test("foo")
        assert not limiter.test("bar")