Beispiel #1
0
    def test_use_cost(self):
        ratelimter = RateLimiter(InMemoryContext, max_calls=50, period=1.0)

        calls = []

        count = 100
        for i in range(count):
            with ratelimter.use_cost(2):
                pass

            calls.append(time.time())

        elapsed = calls[-1] - calls[0]

        rate = count / elapsed
        expectedRate = 100
Beispiel #2
0
    async def test_async(self):
        import asyncio

        ratelimter = RateLimiter(InMemoryContext, max_calls=50, period=1.0)

        calls = []

        count = 100
        for i in range(count):
            async with ratelimter.use_cost(2):
                await asyncio.sleep(0.001)

            calls.append(time.time())

        elapsed = calls[-1] - calls[0]

        rate = count / elapsed
        expectedRate = 100