def _test_throttle(self, cfg_name, stype, method): """Test a specific throttling configuration.""" locks = WeakLocks() set_config_data({'cloud_client': {'throttling': {cfg_name: 500}}}) self.addCleanup(set_config_data, {}) clock = Clock() bracket = _default_throttler(locks, clock, stype, method, 'tenant1') if bracket is None: self.fail("No throttler for %s and %s" % (stype, method)) d = bracket(lambda: 'foo') clock.advance(499) self.assertNoResult(d) clock.advance(500) self.assertEqual(self.successResultOf(d), 'foo') # also make sure that the lock is shared between different calls to the # throttler. bracket1 = _default_throttler(locks, clock, stype, method, 'tenant1') result1 = bracket1(lambda: 'bar1') bracket2 = _default_throttler(locks, clock, stype, method, 'tenant1') result2 = bracket2(lambda: 'bar2') clock.advance(499) self.assertNoResult(result1) self.assertNoResult(result2) clock.advance(1) self.assertEqual(self.successResultOf(result1), 'bar1') self.assertNoResult(result2) clock.advance(500) self.assertEqual(self.successResultOf(result2), 'bar2')
def _test_throttle(self, cfg_name, stype, method): """Test a specific throttling configuration.""" locks = WeakLocks() set_config_data( {'cloud_client': {'throttling': {cfg_name: 500}}}) self.addCleanup(set_config_data, {}) clock = Clock() bracket = _default_throttler(locks, clock, stype, method, 'tenant1') if bracket is None: self.fail("No throttler for %s and %s" % (stype, method)) d = bracket(lambda: 'foo') clock.advance(499) self.assertNoResult(d) clock.advance(500) self.assertEqual(self.successResultOf(d), 'foo') # also make sure that the lock is shared between different calls to the # throttler. bracket1 = _default_throttler(locks, clock, stype, method, 'tenant1') result1 = bracket1(lambda: 'bar1') bracket2 = _default_throttler(locks, clock, stype, method, 'tenant1') result2 = bracket2(lambda: 'bar2') clock.advance(499) self.assertNoResult(result1) self.assertNoResult(result2) clock.advance(1) self.assertEqual(self.successResultOf(result1), 'bar1') self.assertNoResult(result2) clock.advance(500) self.assertEqual(self.successResultOf(result2), 'bar2')
def test_post_and_delete_not_the_same(self): """ The throttlers for POST and DELETE to cloud servers are different. """ set_config_data( {"cloud_client": {"throttling": {"create_server_delay": 1, "delete_server_delay": 0.4}}}) clock = Clock() locks = WeakLocks() deleter = _default_throttler( locks, clock, ServiceType.CLOUD_SERVERS, 'delete', 'any-tenant') poster = _default_throttler( locks, clock, ServiceType.CLOUD_SERVERS, 'post', 'any-tenant') self.assertIsNot(deleter, poster)
def test_post_and_delete_not_the_same(self): """ The throttlers for POST and DELETE to cloud servers are different. """ set_config_data({ "cloud_client": { "throttling": { "create_server_delay": 1, "delete_server_delay": 0.4 } } }) clock = Clock() locks = WeakLocks() deleter = _default_throttler(locks, clock, ServiceType.CLOUD_SERVERS, 'delete', 'any-tenant') poster = _default_throttler(locks, clock, ServiceType.CLOUD_SERVERS, 'post', 'any-tenant') self.assertIsNot(deleter, poster)
def _test_tenant(self, cfg_name, stype, method): """ Test a specific throttling configuration, and ensure that locks are per-tenant. """ locks = WeakLocks() set_config_data({'cloud_client': {'throttling': {cfg_name: 500}}}) self.addCleanup(set_config_data, {}) clock = Clock() bracket1 = _default_throttler(locks, clock, stype, method, 'tenant1') if bracket1 is None: self.fail("No throttler for %s and %s" % (stype, method)) result1 = bracket1(lambda: 'bar1') bracket2 = _default_throttler(locks, clock, stype, method, 'tenant2') result2 = bracket2(lambda: 'bar2') self.assertNoResult(result1) self.assertNoResult(result2) clock.advance(500) self.assertEqual(self.successResultOf(result1), 'bar1') self.assertEqual(self.successResultOf(result2), 'bar2')
def _test_tenant(self, cfg_name, stype, method): """ Test a specific throttling configuration, and ensure that locks are per-tenant. """ locks = WeakLocks() set_config_data( {'cloud_client': {'throttling': {cfg_name: 500}}}) self.addCleanup(set_config_data, {}) clock = Clock() bracket1 = _default_throttler(locks, clock, stype, method, 'tenant1') if bracket1 is None: self.fail("No throttler for %s and %s" % (stype, method)) result1 = bracket1(lambda: 'bar1') bracket2 = _default_throttler(locks, clock, stype, method, 'tenant2') result2 = bracket2(lambda: 'bar2') self.assertNoResult(result1) self.assertNoResult(result2) clock.advance(500) self.assertEqual(self.successResultOf(result1), 'bar1') self.assertEqual(self.successResultOf(result2), 'bar2')
def test_no_config(self): """ No config results in no throttling """ bracket = _default_throttler( WeakLocks(), None, ServiceType.CLOUD_SERVERS, 'get', 'any-tenant') self.assertIs(bracket, None)
def test_mismatch(self): """policy doesn't have a throttler for random junk.""" bracket = _default_throttler( WeakLocks(), None, 'foo', 'get', 'any-tenant') self.assertIs(bracket, None)
def test_no_config(self): """ No config results in no throttling """ bracket = _default_throttler(WeakLocks(), None, ServiceType.CLOUD_SERVERS, 'get', 'any-tenant') self.assertIs(bracket, None)
def test_mismatch(self): """policy doesn't have a throttler for random junk.""" bracket = _default_throttler(WeakLocks(), None, 'foo', 'get', 'any-tenant') self.assertIs(bracket, None)