Exemple #1
0
    def test_request_extraction_with_aggregation(self):
        req = _make_test_request(self.SERVICE_NAME, self.FAKE_OPERATION_ID)
        req = req.allocateQuotaRequest
        req.allocateOperation.quotaMetrics = [
            sc_messages.MetricValueSet(
                metricName=u'a_float',
                metricValues=[
                    metric_value.create(
                        labels={
                            u'key1': u'value1',
                            u'key2': u'value2'},
                        int64Value=12,
                    ),
                ]
            )
        ]

        resp = sc_messages.AllocateQuotaResponse(
            operationId=self.FAKE_OPERATION_ID)
        item = quota_request.CachedItem(req, resp, self.SERVICE_NAME, None)
        expect(item._op_aggregator).to(be_none)
        item.aggregate(req)
        item.aggregate(req)

        extracted = item.extract_request()
        op = extracted.allocateQuotaRequest.allocateOperation
        expect(op.quotaMetrics[0].metricValues[0].int64Value).to(equal(24))
Exemple #2
0
 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
Exemple #3
0
 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)
Exemple #4
0
 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))
Exemple #5
0
 def test_should_cache_responses(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
     expect(agg.allocate_quota(req)).to(equal(temp_response))
     agg.add_response(req, real_response)
     expect(agg.allocate_quota(req)).to(equal(real_response))
Exemple #6
0
             sc_messages.LogEntry(textPayload=u'agg2')
         ]
     )
 },
 {
     u'description': u'combines the metric value using the default kind',
     u'kinds': None,
     u'initial': sc_messages.Operation(
         startTime=_EARLY,
         endTime=_LATER,
         metricValueSets = [
             sc_messages.MetricValueSet(
                 metricName=u'some_floats',
                 metricValues=[
                     metric_value.create(
                         labels=_TEST_LABELS,
                         doubleValue=_A_FLOAT_VALUE,
                         endTime=_EARLY
                     ),
                 ]
             ),
             sc_messages.MetricValueSet(
                 metricName=u'other_floats',
                 metricValues=[
                     metric_value.create(
                         labels=_TEST_LABELS,
                         doubleValue=_A_FLOAT_VALUE,
                         endTime=_EARLY
                     ),
                 ]
             )
         ]
Exemple #7
0
  sc_messages.Operation(
      importance=sc_messages.Operation.ImportanceValueValuesEnum.LOW,
      logEntries=[],
      labels=encoding.PyValueToMessage(
          sc_messages.Operation.LabelsValue, {
              u'servicecontrol.googleapis.com/service_agent':
                  _WANTED_SERVICE_AGENT,
              u'servicecontrol.googleapis.com/user_agent':
                  _WANTED_USER_AGENT,
              u'servicecontrol.googleapis.com/platform':
                  _WANTED_PLATFORM,
          }),
      metricValueSets = [
          sc_messages.MetricValueSet(
              metricName=_EXPECTED_OK_METRIC.metric_name,
              metricValues=[
                  metric_value.create(int64Value=1),
              ]
          ),
      ],
      consumerId=u'api_key:' + _TEST_API_KEY,
      operationId=u'an_op_id',
      operationName=u'an_op_name',
      startTime=_START_OF_EPOCH_TIMESTAMP,
      endTime=_START_OF_EPOCH_TIMESTAMP)
 ),
 (report_request.Info(
     response_code=404,
     operation_id=u'an_op_id',
     operation_name=u'an_op_name',
     method=u'GET',
     referer=u'a_referer',