def _test_get_metric_history_as_columns(self, slugs, granularity):
        """Test that R.get_metric_history_as_columns makes calls to the
        following functions:

        * ``R.r.mget``
        * ``R.get_metric_history``
        * ``templatetags.metric_slug``
        * ``templatetags.strip_metric_prefix``

        """
        keys = self._metric_history_keys(slugs, granularity=granularity)
        self.r.get_metric_history_as_columns(slugs, granularity=granularity)

        # Verifies the correct call to redis
        self.redis.assert_has_calls([call.mget(keys)])

        # Verify that the method gets called correctly
        with patch('redis_metrics.models.R') as mock_r:
            r = mock_r.return_value  # Get an instance of our Mocked R class
            r.get_metric_history_as_columns(slugs, granularity=granularity)
            mock_r.assert_has_calls([
                call().get_metric_history_as_columns(
                    slugs, granularity=granularity
                )
            ])
 def _test_get_metric_history(self, slugs, since=None, to=None,
                              granularity=None):
     """actual test code for ``R.get_metric_history``."""
     keys = self._metric_history_keys(slugs, since, to, granularity)
     self.r.get_metric_history(
         slugs, since=since, to=to, granularity=granularity)
     self.redis.assert_has_calls([call.mget(keys)])
Esempio n. 3
0
 def _test_get_metric_history(self,
                              slugs,
                              since=None,
                              to=None,
                              granularity=None):
     """actual test code for ``R.get_metric_history``."""
     keys = self._metric_history_keys(slugs, since, to, granularity)
     self.r.get_metric_history(slugs,
                               since=since,
                               to=to,
                               granularity=granularity)
     self.redis.assert_has_calls([call.mget(keys)])
Esempio n. 4
0
    def _test_get_metric_history_as_columns(self, slugs, granularity):
        """Test that R.get_metric_history_as_columns makes calls to the
        following functions:

        * ``R.r.mget``
        * ``R.get_metric_history``
        * ``templatetags.metric_slug``
        * ``templatetags.strip_metric_prefix``

        """
        keys = self._metric_history_keys(slugs, granularity=granularity)
        self.r.get_metric_history_as_columns(slugs, granularity=granularity)

        # Verifies the correct call to redis
        self.redis.assert_has_calls([call.mget(keys)])

        # Verify that the method gets called correctly
        with patch('redis_metrics.models.R') as mock_r:
            r = mock_r.return_value  # Get an instance of our Mocked R class
            r.get_metric_history_as_columns(slugs, granularity=granularity)
            mock_r.assert_has_calls([
                call().get_metric_history_as_columns(slugs,
                                                     granularity=granularity)
            ])
Esempio n. 5
0
def test_transactional_mock_redis(mock_redis, mock_pipeline):
    rl = RedisTransactionalRateLimiter(mock_redis, codec=DummyCodec())
    rl.configure(k1=RateLimit(Zone('z1', 2)),
                 k2=RateLimit(Zone('z2', 1, expiry=10)))

    t0 = T0
    mock_pipeline.mget.return_value = [None]
    mock_pipeline.time.return_value = _to_redis_time(t0)
    assert rl.request(k1='foo') == (True, 0)

    t1 = T0 + 0.1
    mock_pipeline.mget.return_value = [None, None]
    mock_pipeline.time.return_value = _to_redis_time(t1)
    assert rl.request(k1='bar', k2='baz') == (True, 0)

    t2 = T0 + 0.3
    mock_pipeline.mget.return_value = [State(t0, 1)]
    mock_pipeline.time.return_value = _to_redis_time(t2)
    assert rl.request(k1='foo') == (False, None)

    t3 = T0 + 0.51
    mock_pipeline.time.return_value = _to_redis_time(t3)
    assert rl.request(k1='foo') == (True, 0)

    assert mock_redis.mock_calls == [
        call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True),
        call.transaction(ANY, 'redbucket:z1:bar', 'redbucket:z2:baz',
                         value_from_callable=True),
        call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True),
        call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True),
    ]

    assert mock_pipeline.mock_calls == [
        # request 1
        call.watch('redbucket:z1:foo'),
        call.mget(['redbucket:z1:foo']),
        call.time(),
        call.multi(),
        call.setex('redbucket:z1:foo', 60, State(approx(t0), 1)),
        call.execute(),
        # request 2
        call.watch('redbucket:z1:bar', 'redbucket:z2:baz'),
        call.mget(['redbucket:z1:bar', 'redbucket:z2:baz']),
        call.time(),
        call.multi(),
        call.setex('redbucket:z1:bar', 60, State(approx(t1), 1)),
        call.setex('redbucket:z2:baz', 10, State(approx(t1), 1)),
        call.execute(),
        # request 3
        call.watch('redbucket:z1:foo'),
        call.mget(['redbucket:z1:foo']),
        call.time(),
        call.unwatch(),
        call.execute(),
        # request 4
        call.watch('redbucket:z1:foo'),
        call.mget(['redbucket:z1:foo']),
        call.time(),
        call.multi(),
        call.setex('redbucket:z1:foo', 60, State(approx(t3), 1)),
        call.execute(),
    ]