def testSetErrorRateZero(self): """Test setting the error rate to Zero.""" request1 = FakePB() response1 = FakePB() error = apiproxy_errors.OverQuotaError() self.stub.SetError(error, error_rate=0) self.stub.MakeSyncCall('fake', 'method1', request1, response1)
def testSetErrorNone(self): """Test what happens when setting error to None.""" request1 = FakePB() response1 = FakePB() error = apiproxy_errors.OverQuotaError() self.stub.SetError(error) self.stub.SetError(None) self.stub.MakeSyncCall('fake', 'method1', request1, response1)
def testSetMethodError(self): """Test setting an error for a method.""" request1 = FakePB() response1 = FakePB() error = apiproxy_errors.OverQuotaError() self.stub.SetError(error, method='method1') self.assertRaises(apiproxy_errors.OverQuotaError, self.stub.MakeSyncCall, 'fake', 'method1', request1, response1)
def testSetError(self): """Tests ability to set stub to always raise and error.""" request1 = FakePB() response1 = FakePB() error = apiproxy_errors.OverQuotaError() self.stub.SetError(error) self.assertRaises(apiproxy_errors.OverQuotaError, self.stub.MakeSyncCall, 'fake', 'method1', request1, response1)
def expect(self, method, url, response_code, response_data, response_headers=None, request_payload='', request_headers=None, urlfetch_error=False, apiproxy_error=False, deadline_error=False, urlfetch_size_error=False): """Expects a certain request and response. Overrides any existing expectations for this stub. Args: method: The expected method. url: The expected URL to access. response_code: The expected response code. response_data: The expected response data. response_headers: Headers to serve back, if any. request_payload: The expected request payload, if any. request_headers: Any expected request headers. urlfetch_size_error: Set to True if this call should raise a urlfetch_errors.ResponseTooLargeError urlfetch_error: Set to True if this call should raise a urlfetch_errors.Error exception when made. apiproxy_error: Set to True if this call should raise an apiproxy_errors.Error exception when made. deadline_error: Set to True if this call should raise a google.appengine.runtime.DeadlineExceededError error. """ error_instance = None if urlfetch_error: error_instance = apiproxy_errors.ApplicationError( urlfetch_service_pb.URLFetchServiceError.FETCH_ERROR, 'mock error') elif urlfetch_size_error: error_instance = apiproxy_errors.ApplicationError( urlfetch_service_pb.URLFetchServiceError.RESPONSE_TOO_LARGE, 'mock error') elif apiproxy_error: error_instance = apiproxy_errors.OverQuotaError() elif deadline_error: error_instance = runtime.DeadlineExceededError() self._expectations[(method.lower(), url)] = (request_payload, request_headers, response_code, response_data, response_headers, error_instance)
def _MakeCallDone(self): self._state = RPC.FINISHING self.cpu_usage_mcycles = self._result_dict['cpu_usage_mcycles'] if self._result_dict['error'] == APPLICATION_ERROR: appl_err = self._result_dict['application_error'] if appl_err == MEMCACHE_UNAVAILABLE and self.package == 'memcache': self._exception = apiproxy_errors.CapabilityDisabledError( 'The memcache service is temporarily unavailable. %s' % self._result_dict['error_detail']) else: self._exception = apiproxy_errors.ApplicationError( appl_err, self._result_dict['error_detail']) elif self._result_dict['error'] == CAPABILITY_DISABLED: if self._result_dict['error_detail']: self._exception = apiproxy_errors.CapabilityDisabledError( self._result_dict['error_detail']) else: self._exception = apiproxy_errors.CapabilityDisabledError( "The API call %s.%s() is temporarily unavailable." % (self.package, self.call)) elif self._result_dict['error'] == FEATURE_DISABLED: self._exception = apiproxy_errors.FeatureNotEnabledError( self._result_dict['error_detail']) elif self._result_dict['error'] == OVER_QUOTA: if self._result_dict['error_detail']: self._exception = apiproxy_errors.OverQuotaError(( 'The API call %s.%s() required more quota than is available. %s' % (self.package, self.call, self._result_dict['error_detail']))) else: exception_entry = _ExceptionsMap[self._result_dict['error']] self._exception = exception_entry[0](exception_entry[1] % (self.package, self.call)) elif self._result_dict['error'] in _ExceptionsMap: exception_entry = _ExceptionsMap[self._result_dict['error']] self._exception = exception_entry[0](exception_entry[1] % (self.package, self.call)) else: try: self.response.ParseFromString( self._result_dict['result_string']) except Exception, e: self._exception = e
def testSetMethodRandomError(self): """Test setting random rates for a method.""" request1 = FakePB() response1 = FakePB() error = apiproxy_errors.OverQuotaError() self.stub.SetError(error, method='method1', error_rate=0.5) old_random = random.random def FakeRandom(): yield 0.50 yield 0.51 try: fakeRandom = FakeRandom() random.random = lambda: next(fakeRandom) self.assertRaises(apiproxy_errors.OverQuotaError, self.stub.MakeSyncCall, 'fake', 'method1', request1, response1) self.stub.MakeSyncCall('fake', 'method1', request1, response1) finally: random.random = old_random