Exemple #1
0
    def test_subscription_update_too_many_retry(self, get_fitbit_data,
                                                mock_retry):
        # Check that retry is called with the right arguments
        exc = fitbit_exceptions.HTTPTooManyRequests(self._error_response())
        exc.retry_after_secs = 21
        get_fitbit_data.side_effect = exc
        # This return value is just to keep celery from throwing up
        mock_retry.return_value = Exception()
        category = getattr(TimeSeriesDataType, self.category)
        _type = TimeSeriesDataType.objects.filter(category=category)[0]

        self.assertEqual(TimeSeriesData.objects.count(), 0)

        result = get_time_series_data.apply_async(
            (
                self.fbuser.fitbit_user,
                _type.category,
                _type.resource,
            ), {'date': parser.parse(self.date)})

        # 22 = 21 + x ** 0
        get_time_series_data.retry.assert_called_once_with(countdown=22,
                                                           exc=exc)
        self.assertRaises(Exception, result.get)
        self.assertEqual(get_fitbit_data.call_count, 1)
        self.assertEqual(TimeSeriesData.objects.count(), 0)
Exemple #2
0
    def test_subscription_update_too_many(self, get_fitbit_data):
        # Check that celery tasks get postponed if the rate limit is hit
        cat_id = getattr(TimeSeriesDataType, self.category)
        _type = TimeSeriesDataType.objects.filter(category=cat_id)[0]
        lock_id = 'fitapp.tasks-lock-{0}-{1}-{2}'.format(
            self.fbuser.fitbit_user, _type, self.date)
        exc = fitbit_exceptions.HTTPTooManyRequests(self._error_response())
        exc.retry_after_secs = 21

        def side_effect(*args, **kwargs):
            # Delete the cache lock after the first try and adjust the
            # get_fitbit_data mock to be successful
            cache.delete(lock_id)
            get_fitbit_data.side_effect = None
            get_fitbit_data.return_value = [{
                'dateTime': self.date,
                'value': '34'
            }]
            raise exc

        get_fitbit_data.side_effect = side_effect
        category = getattr(TimeSeriesDataType, self.category)
        resources = TimeSeriesDataType.objects.filter(category=category)
        self.assertEqual(TimeSeriesData.objects.count(), 0)
        result = get_time_series_data.apply_async(
            (self.fbuser.fitbit_user, _type.category, _type.resource,),
            {'date': parser.parse(self.date)})
        result.get()
        # Since celery is in eager mode, we expect a Retry exception first
        # and then a second task execution that is successful
        self.assertEqual(get_fitbit_data.call_count, 2)
        self.assertEqual(TimeSeriesData.objects.count(), 1)
        self.assertEqual(TimeSeriesData.objects.get().value, '34')
 def test_subscription_update_too_many(self, mock_retry, get_fitbit_data):
     # Check that celery tasks get postponed if the rate limit is hit
     mock_retry.return_value = celery.exceptions.Retry()
     exc = fitbit_exceptions.HTTPTooManyRequests(self._error_response())
     exc.retry_after_secs = 21
     get_fitbit_data.side_effect = exc
     category = getattr(TimeSeriesDataType, self.category)
     resources = TimeSeriesDataType.objects.filter(category=category)
     self.assertEqual(TimeSeriesData.objects.count(), 0)
     self._receive_fitbit_updates()
     self.assertEqual(get_fitbit_data.call_count, resources.count())
     self.assertEqual(TimeSeriesData.objects.count(), 0)
     mock_retry.assert_called_with(exc, countdown=21)