Ejemplo n.º 1
0
    def test_redis_for_multiple_instance(self):
        limiter = Limiter(
            storage_uri='redis://127.0.0.1:6379/'
        )

        @limiter.limit('3/minute', 'multiple-instance-key')
        def func():
            pass

        processes = list()
        i = 0

        try:
            for i in range(4):
                processes.append(Process(target=func))

            for process in processes:
                process.start()

            for process in processes:
                process.join()
        except Exception as e:
            self.assertIsInstance(e, RateLimitExceeded)

        self.assertEqual(3, i)
Ejemplo n.º 2
0
    def test_five_call(self):
        limiter = Limiter()

        @limiter.limit('5/minute', 'key')
        def func():
            pass

        i = 0
        for i in range(5):
            func()

        self.assertEqual(4, i)
Ejemplo n.º 3
0
    def test_wrong_type_limitations_format(self):
        limiter = Limiter()

        @limiter.limit(0, 'key')
        def func():
            pass

        i = 0

        for i in range(10):
            func()

        self.assertEqual(9, i)
Ejemplo n.º 4
0
    def test_more_than_limitation_call_for_exempt(self):
        limiter = Limiter()

        @limiter.limit('3/minute', 'key', exempt=self.exempt)
        def func():
            pass

        i = 0

        for i in range(4):
            func()

        self.assertEqual(3, i)
Ejemplo n.º 5
0
    def test_callable_function_for_limitations(self):
        limiter = Limiter()

        @limiter.limit(self.limitations, 'key')
        def func():
            pass

        i = 0

        for i in range(3):
            func()

        self.assertEqual(2, i)
Ejemplo n.º 6
0
    def test_exempt_key(self):
        limiter = Limiter(
            default_key='key'
        )

        @limiter.limit('3/minute', 'key', exempt='key')
        def func():
            pass

        i = 0
        for i in range(10):
            func()

        self.assertEqual(9, i)
Ejemplo n.º 7
0
    def test_more_than_limitation_call(self):
        limiter = Limiter()

        @limiter.limit('5/minute', 'key')
        def func():
            pass

        i = 0

        with self.assertRaises(RateLimitExceeded):
            for i in range(6):
                func()

        self.assertEqual(5, i)
Ejemplo n.º 8
0
    def test_more_than_limitation_call_for_key(self):
        limiter = Limiter()

        @limiter.limit('3/minute', self.key)
        def func():
            pass

        i = 0

        with self.assertRaises(RateLimitExceeded):
            for i in range(4):
                func()

        self.assertEqual(3, i)
Ejemplo n.º 9
0
    def test_single_line_limitations_more_than_first_limitation(self):
        limiter = Limiter()

        @limiter.limit('1/second;3/minute', 'key')
        def func():
            pass

        i = 0

        with self.assertRaises(RateLimitExceeded):
            for i in range(3):
                func()

        self.assertEqual(1, i)
Ejemplo n.º 10
0
    def test_garbage_collector_none_garbage(self):
        limiter = Limiter()

        @limiter.limit('3/minute', 'key')
        def func():
            pass

        i = 0

        with self.assertRaises(RateLimitExceeded):
            for i in range(4):
                func()

        self.assertEqual(3, i)
Ejemplo n.º 11
0
    def test_single_line_limitations(self):
        limiter = Limiter()

        @limiter.limit('1/second;3/minute', 'key')
        def func():
            pass

        i = 0

        for i in range(3):
            func()
            time.sleep(1)

        self.assertEqual(2, i)
Ejemplo n.º 12
0
    def test_garbage_collector_with_garbage(self):
        limiter = Limiter()

        @limiter.limit('1/second', 'key')
        def func():
            pass

        i = 0

        for i in range(4):
            func()
            time.sleep(1)

        self.assertEqual(3, i)
Ejemplo n.º 13
0
    def test_redis_for_single_instance(self):
        limiter = Limiter(
            storage_uri='redis://127.0.0.1:6379/'
        )

        @limiter.limit('3/minute', 'key')
        def func():
            pass

        i = 0

        for i in range(3):
            func()

        self.assertEqual(2, i)
Ejemplo n.º 14
0
    def test_default_limitations_key(self):
        limiter = Limiter(
            default_key='key'
        )

        @limiter.limit('3/minute')
        def func():
            pass

        i = 0
        with self.assertRaises(RateLimitExceeded):
            for i in range(4):
                func()

        self.assertEqual(3, i)
Ejemplo n.º 15
0
    def test_exempt_key_not_equal(self):
        limiter = Limiter(
            default_key='key'
        )

        @limiter.limit('3/minute', 'key', exempt='other-key')
        def func():
            pass

        i = 0
        with self.assertRaises(RateLimitExceeded):
            for i in range(4):
                func()

        self.assertEqual(3, i)
Ejemplo n.º 16
0
    def test_redis_for_single_instance_over_rate(self):
        limiter = Limiter(
            storage_uri='redis://127.0.0.1:6379/'
        )

        @limiter.limit('3/minute', 'over-key')
        def func():
            pass

        i = 0

        with self.assertRaises(RateLimitExceeded):
            for i in range(4):
                func()

        self.assertEqual(3, i)
Ejemplo n.º 17
0
    def test_redis_custom_database_name(self):
        limiter = Limiter(
            database_name='custom_database_name',
            storage_uri='redis://127.0.0.1:6379/'
        )

        @limiter.limit('3/minute', 'key-k')
        def func():
            pass

        i = 0
        try:
            for i in range(3):
                func()
        except Exception as e:
            self.assertIsInstance(e, RateLimitExceeded)
        self.assertEqual(2, i)