示例#1
0
    def _call(self, assertion, msg=None, obj=None):
        """
        assertion: what we're asserting
        msg: a static message describing the error
        obj: the specific piece of data, if any, that tripped the assertion

        returns the assertion itself
        """
        if not assertion:
            if self.debug or is_hard_mode():
                raise AssertionError(msg)
            short_tb = get_traceback(skip=self.tb_skip, limit=self.key_limit)
            full_tb = get_traceback(skip=self.tb_skip)
            line = short_tb.strip().split('\n')[-1].strip()
            tb_id = hashlib.md5(short_tb).hexdigest()
            count = ExponentialBackoff.increment(tb_id)
            if not self.use_exponential_backoff or not ExponentialBackoff.should_backoff(tb_id):
                if msg:
                    msg = msg.replace('\n', '\\n')
                self.send(SoftAssertInfo(traceback=full_tb,
                                         short_traceback=short_tb,
                                         count=count,
                                         msg=msg,
                                         key=tb_id, line=line, obj=obj))
        return assertion
示例#2
0
    def _call(self, assertion, msg=None, obj=None):
        """
        assertion: what we're asserting
        msg: a static message describing the error
        obj: the specific piece of data, if any, that tripped the assertion

        returns the assertion itself
        """
        if not assertion:
            if self.debug or is_hard_mode():
                raise AssertionError(msg)
            short_tb = get_traceback(skip=self.tb_skip, limit=self.key_limit)
            full_tb = get_traceback(skip=self.tb_skip)
            line = short_tb.strip().split('\n')[-1].strip()
            tb_id = hashlib.md5(short_tb).hexdigest()
            count = ExponentialBackoff.increment(tb_id)
            if not self.use_exponential_backoff or not ExponentialBackoff.should_backoff(
                    tb_id):
                if msg:
                    msg = msg.replace('\n', '\\n')
                breadcrumbs = get_breadcrumbs(
                ) if self.include_breadcrumbs else None
                self.send(
                    SoftAssertInfo(traceback=full_tb,
                                   short_traceback=short_tb,
                                   count=count,
                                   msg=msg,
                                   key=tb_id,
                                   line=line,
                                   obj=obj,
                                   breadcrumbs=breadcrumbs))
        return assertion
 def test_number_is_power_of_two(self):
     powers_of_two = [2**i for i in range(10)]
     for n in range(100):
         actual = ExponentialBackoff._number_is_power_of_two(n)
         expected = n in powers_of_two
         self.assertEqual(actual, expected,
                          '_number_is_power_of_two: {}'.format(actual))
示例#4
0
 def test_number_is_power_of_two(self):
     powers_of_two = [2**i for i in range(10)]
     for n in range(100):
         actual = ExponentialBackoff._number_is_power_of_two(n)
         expected = n in powers_of_two
         self.assertEqual(actual, expected,
                          '_number_is_power_of_two: {}'.format(actual))
示例#5
0
def jserror(request):
    stack = request.POST.get('stack', None)
    message = request.POST.get('message', None)
    if stack:
        cache_key = ' '.join(map(lambda l: l.strip(), stack.split('\n'))[:3])
    else:
        cache_key = message

    count = ExponentialBackoff.increment(cache_key)
    if not ExponentialBackoff.should_backoff(cache_key):
        notify_js_exception(request,
                            message=message,
                            details={
                                'filename': request.POST.get('filename', None),
                                'line': request.POST.get('line', None),
                                'page': request.POST.get('page', None),
                                'agent':
                                request.META.get('HTTP_USER_AGENT', None),
                                'js_stack': stack,
                                'count': count,
                            })

    return HttpResponse('')
示例#6
0
def jserror(request):
    stack = request.POST.get('stack', None)
    message = request.POST.get('message', None)
    if stack:
        cache_key = ' '.join(map(lambda l: l.strip(), stack.split('\n'))[:3])
    else:
        cache_key = message

    count = ExponentialBackoff.increment(cache_key)
    if not ExponentialBackoff.should_backoff(cache_key):
        notify_js_exception(
            request,
            message=message,
            details={
                'filename': request.POST.get('filename', None),
                'line': request.POST.get('line', None),
                'page': request.POST.get('page', None),
                'agent': request.META.get('HTTP_USER_AGENT', None),
                'js_stack': stack,
                'count': count,
            }
        )

    return HttpResponse('')
示例#7
0
 def test_backoff_none(self):
     key = None
     ExponentialBackoff.increment(key)  # first incr is 1
     self.assertFalse(ExponentialBackoff.should_backoff(key))
示例#8
0
    def test_backoff(self):
        key = 'new key'

        self.assertFalse(ExponentialBackoff.should_backoff(key))
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # first incr is 1
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 2
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 3
        self.assertTrue(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 4
        self.assertFalse(ExponentialBackoff.should_backoff(key))
 def test_backoff_none(self):
     key = None
     ExponentialBackoff.increment(key)  # first incr is 1
     self.assertFalse(ExponentialBackoff.should_backoff(key))
示例#10
0
    def test_backoff(self):
        key = 'new key'

        self.assertFalse(ExponentialBackoff.should_backoff(key))
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # first incr is 1
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 2
        self.assertFalse(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 3
        self.assertTrue(ExponentialBackoff.should_backoff(key))

        ExponentialBackoff.increment(key)  # incr to 4
        self.assertFalse(ExponentialBackoff.should_backoff(key))
示例#11
0
def _is_rate_limited(rate_limit_key):
    exponential_backoff_key = '{}_down'.format(rate_limit_key)
    ExponentialBackoff.increment(exponential_backoff_key)
    return ExponentialBackoff.should_backoff(exponential_backoff_key)