Esempio n. 1
0
    def test_lock(self):
        import random
        import threading
        from .utils import ThreadWithReturnValue
        from before_after import before

        @cached_as(Post, lock=True, timeout=60)
        def func():
            return random.random()

        results = []
        locked = threading.Event()
        thread = [None]

        def second_thread():
            def _target():
                try:
                    with before('redis.StrictRedis.brpoplpush', lambda *a, **kw: locked.set()):
                        results.append(func())
                except Exception:
                    locked.set()
                    raise

            thread[0] = ThreadWithReturnValue(target=_target)
            thread[0].start()
            assert locked.wait(1)  # Wait until right before the block

        with before('random.random', second_thread):
            results.append(func())

        thread[0].join()

        self.assertEqual(results[0], results[1])
Esempio n. 2
0
 def _target():
     try:
         with before('redis.StrictRedis.brpoplpush', lambda *a, **kw: locked.set()):
             results.append(func())
     except Exception:
         locked.set()
         raise
Esempio n. 3
0
 def _target():
     try:
         with before('redis.StrictRedis.brpoplpush', lambda *a, **kw: locked.set()):
             results.append(func())
     except Exception:
         locked.set()
         raise
Esempio n. 4
0
    def test_lock(self):
        import random
        import threading
        from .utils import ThreadWithReturnValue
        from before_after import before

        @cached_as(Post, lock=True, timeout=60)
        def func():
            return random.random()

        results = []
        locked = threading.Event()
        thread = [None]

        def second_thread():
            def _target():
                try:
                    with before('redis.StrictRedis.brpoplpush', lambda *a, **kw: locked.set()):
                        results.append(func())
                except Exception:
                    locked.set()
                    raise

            thread[0] = ThreadWithReturnValue(target=_target)
            thread[0].start()
            assert locked.wait(1)  # Wait until right before the block

        with before('random.random', second_thread):
            results.append(func())

        thread[0].join()

        self.assertEquals(results[0], results[1])
    def test_before(self):
        def before_fn(*a):
            test_functions.test_list.append(1)

        with before('before_after.tests.test_functions.sample_fn', before_fn):
            test_functions.sample_fn(2)

        self.assertEqual(test_functions.test_list, [1, 2])
    def test_before_once(self):
        def before_fn(*a):
            test_functions.test_list.append(1)

        with before('before_after.tests.test_functions.sample_fn',
                    before_fn,
                    once=True):
            test_functions.sample_fn(2)
            test_functions.sample_fn(3)

        self.assertEqual(test_functions.test_list, [1, 2, 3])
Esempio n. 7
0
    def test_before_method(self):
        sample_instance = test_functions.Sample()

        def before_fn(self, *a):
            sample_instance.instance_list.append(1)

        with before('before_after.tests.test_functions.Sample.method',
                    before_fn):
            sample_instance.method(2)

        self.assertEqual(sample_instance.instance_list, [1, 2])
Esempio n. 8
0
    def test_header_race_condition(self):
        """Make sure concurrent requests don't affect each other's rate limit"""
        def parallel_request(*args, **kwargs):
            self.client.get(reverse("race-condition-endpoint"))

        with before(
                "tests.sentry.middleware.test_ratelimit_middleware.RateLimitHeaderTestEndpoint.inject_call",
                parallel_request,
        ):
            response = self.get_success_response()

        assert int(response["X-Sentry-Rate-Limit-Remaining"]) == 1
        assert int(response["X-Sentry-Rate-Limit-Limit"]) == 2