def test_get_usage_uses_refund(self): timestamp = time.time() self.get_project_quota.return_value = (200, 60) self.get_organization_quota.return_value = (300, 60) n = 10 for _ in xrange(n): self.quota.is_rate_limited(self.project, timestamp=timestamp) self.quota.refund(self.project, timestamp=timestamp) quotas = self.quota.get_quotas(self.project) assert self.quota.get_usage( self.project.organization_id, quotas + [ BasicRedisQuota(prefix="unlimited", limit=None, window=60, reason_code="unlimited"), BasicRedisQuota( prefix="dummy", limit=10, window=60, reason_code="dummy"), ], timestamp=timestamp, # the - 1 is because we refunded once ) == [n - 1 for _ in quotas] + [0, 0]
def test_refund(self, mock_get_quotas): timestamp = time.time() mock_get_quotas.return_value = ( BasicRedisQuota( key='p:1', limit=1, window=1, reason_code='project_quota', enforce=False, ), BasicRedisQuota( key='p:2', limit=1, window=1, reason_code='project_quota', enforce=True, ), ) self.quota.refund(self.project, timestamp=timestamp) client = self.quota.cluster.get_local_client_for_key( six.text_type(self.project.organization.pk)) keys = client.keys('r:quota:p:?:*') assert len(keys) == 2 for key in keys: assert client.get(key) == '1'
def test_refund(self, mock_get_quotas): timestamp = time.time() mock_get_quotas.return_value = ( BasicRedisQuota(prefix="p", subscope=1, limit=None, window=1, reason_code="project_quota"), BasicRedisQuota(prefix="p", subscope=2, limit=1, window=1, reason_code="project_quota"), ) self.quota.refund(self.project, timestamp=timestamp) client = self.quota.cluster.get_local_client_for_key( six.text_type(self.project.organization.pk)) keys = client.keys("r:quota:p:?:*") assert len(keys) == 2 for key in keys: assert client.get(key) == "1"
def test_get_usage(self): timestamp = time.time() self.get_project_quota.return_value = (200, 60) self.get_organization_quota.return_value = (300, 60) n = 10 for _ in xrange(n): self.quota.is_rate_limited(self.project, timestamp=timestamp) quotas = self.quota.get_quotas(self.project) assert self.quota.get_usage( self.project.organization_id, quotas + [ BasicRedisQuota( key='unlimited', limit=0, window=60, reason_code='unlimited', ), BasicRedisQuota( key='dummy', limit=10, window=60, reason_code='dummy', ), ], timestamp=timestamp, ) == [n for _ in quotas] + [None, 0]
def test_limited_with_unlimited_quota(self, mock_is_rate_limited, mock_get_quotas): mock_get_quotas.return_value = ( BasicRedisQuota(prefix="p", subscope=1, limit=None, window=1, reason_code="project_quota"), BasicRedisQuota(prefix="p", subscope=2, limit=1, window=1, reason_code="project_quota"), ) assert self.quota.is_rate_limited(self.project).is_limited
def test_limited_without_enforce(self, mock_is_rate_limited, mock_get_quotas): mock_get_quotas.return_value = ( BasicRedisQuota(key="p:1", limit=1, window=1, reason_code="project_quota", enforce=False), BasicRedisQuota(key="p:2", limit=1, window=1, reason_code="project_quota", enforce=True), ) assert self.quota.is_rate_limited(self.project).is_limited
def test_not_limited_without_enforce(self, mock_is_rate_limited, mock_get_quotas): mock_get_quotas.return_value = ( BasicRedisQuota(prefix="p", subscope=1, limit=1, window=1, reason_code="project_quota", enforce=False), BasicRedisQuota(prefix="p", subscope=2, limit=1, window=1, reason_code="project_quota", enforce=True), ) assert not self.quota.is_rate_limited(self.project).is_limited
reason_code="unlimited"), BasicRedisQuota( prefix="dummy", limit=10, window=60, reason_code="dummy"), ], timestamp=timestamp, # the - 1 is because we refunded once ) == [n - 1 for _ in quotas] + [0, 0] @pytest.mark.parametrize( "obj,json", [ ( BasicRedisQuota(prefix="p", subscope=1, limit=None, window=1, reason_code="go_away"), { "prefix": "p", "subscope": "1", "window": 1, "reasonCode": "go_away" }, ), (BasicRedisQuota(limit=0, reason_code="go_away"), { "limit": 0, "reasonCode": "go_away" }), ], )