def test_should_change_signature_when_labels_are_added(self): without_labels = quota_request.sign(self.test_quota_request) self.test_op.labels = encoding.PyValueToMessage( sc_messages.QuotaOperation.LabelsValue, { u'key1': u'value1', u'key2': u'value2'}) with_labels = quota_request.sign(self.test_quota_request) expect(with_labels).not_to(equal(without_labels))
def test_expiration(self): req = _make_test_request(self.SERVICE_NAME, self.FAKE_OPERATION_ID) temp_response = sc_messages.AllocateQuotaResponse( operationId=self.FAKE_OPERATION_ID) real_response = sc_messages.AllocateQuotaResponse( operationId=self.FAKE_OPERATION_ID, quotaMetrics=[sc_messages.MetricValueSet( metricName=u'a_float', metricValues=[ metric_value.create( labels={ u'key1': u'value1', u'key2': u'value2'}, doubleValue=1.1, ), ] )] ) agg = self.agg agg.allocate_quota(req) assert len(agg.flush()) == 1 agg.add_response(req, real_response) signature = quota_request.sign(req.allocateQuotaRequest) with agg._cache as cache, agg._out as out: assert len(out) == 0 assert signature in cache self.timer.tick() self.timer.tick() assert len(agg.flush()) == 0 assert len(out) == 0 assert signature in cache self.timer.tick() # expired at 3rd second assert len(agg.flush()) == 0 assert len(out) == 0 assert signature not in cache
def test_should_update_temp_response_with_actual(self): req = _make_test_request(self.SERVICE_NAME, self.FAKE_OPERATION_ID) temp_response = sc_messages.AllocateQuotaResponse( operationId=self.FAKE_OPERATION_ID) real_response = sc_messages.AllocateQuotaResponse( operationId=self.FAKE_OPERATION_ID, quotaMetrics=[sc_messages.MetricValueSet( metricName=u'a_float', metricValues=[ metric_value.create( labels={ u'key1': u'value1', u'key2': u'value2'}, doubleValue=1.1, ), ] )] ) agg = self.agg agg.allocate_quota(req) signature = quota_request.sign(req.allocateQuotaRequest) with agg._cache as cache: item = cache[signature] expect(item.response).to(equal(temp_response)) expect(item.is_in_flight).to(be_true) agg.add_response(req, real_response) item = cache[signature] expect(item.response).to(equal(real_response)) expect(item.is_in_flight).to(be_false)
def test_should_change_signature_when_metric_values_are_added(self): without_mvs = quota_request.sign(self.test_quota_request) self.test_op.quotaMetrics = [ sc_messages.MetricValueSet( metricName=u'a_float', metricValues=[ metric_value.create( labels={ u'key1': u'value1', u'key2': u'value2'}, doubleValue=1.1, ), ] ) ] with_mvs = quota_request.sign(self.test_quota_request) expect(with_mvs).not_to(equal(without_mvs))
def test_aggregated_requests_should_be_sent_on_flush(self): req = _make_test_request(self.SERVICE_NAME, self.FAKE_OPERATION_ID) signature = quota_request.sign(req.allocateQuotaRequest) agg = self.agg agg.allocate_quota(req) expect(len(agg.flush())).to(equal(1)) # initial request simple_response = sc_messages.AllocateQuotaResponse( operationId=self.FAKE_OPERATION_ID) agg.add_response(req, simple_response) agg.allocate_quota(req) agg.allocate_quota(req) expect(len(agg.flush())).to(equal(1)) # aggregated next two requests
def test_request_aggregation(self): req = _make_test_request(self.SERVICE_NAME, self.FAKE_OPERATION_ID) signature = quota_request.sign(req.allocateQuotaRequest) agg = self.agg agg.allocate_quota(req) with agg._cache as cache: item = cache[signature] expect(item._op_aggregator).to(be_none) agg.allocate_quota(req) agg.allocate_quota(req) with agg._out as out: expect(len(out)).to(equal(1)) with agg._cache as cache: item = cache[signature] expect(item._op_aggregator).not_to(be_none)
def test_should_sign_a_valid_quota_request(self): quota_request.sign(self.test_quota_request)
def test_should_fail_if_operation_has_no_consumer_id(self): op = sc_messages.QuotaOperation(methodName=_TEST_OP_NAME) testf = lambda: quota_request.sign( sc_messages.AllocateQuotaRequest(allocateOperation=op)) expect(testf).to(raise_error(ValueError))
def test_should_fail_if_operation_has_no_method_name(self): op = sc_messages.QuotaOperation(consumerId=_TEST_CONSUMER_ID) testf = lambda: quota_request.sign( sc_messages.AllocateQuotaRequest(allocateOperation=op)) expect(testf).to(raise_error(ValueError))
def test_should_fail_on_invalid_input(self): testf = lambda: quota_request.sign(None) expect(testf).to(raise_error(ValueError)) testf = lambda: quota_request.sign(object()) expect(testf).to(raise_error(ValueError))
def test_should_fail_if_operation_is_not_set(self): testf = lambda: quota_request.sign(sc_messages.AllocateQuotaRequest()) expect(testf).to(raise_error(ValueError))