Example #1
0
 def test_no_memcache(self):
     current_rate = 13
     num_calls = 5
     conf_dict = {'account_ratelimit': current_rate}
     self.test_ratelimit = ratelimit.filter_factory(conf_dict)(FakeApp())
     ratelimit.http_connect = mock_http_connect(204)
     req = Request.blank('/v/a')
     req.environ['chase.cache'] = None
     make_app_call = lambda: self.test_ratelimit(req.environ,
                                                 start_response)
     begin = time.time()
     self._run(make_app_call, num_calls, current_rate, check_time=False)
     time_took = time.time() - begin
     self.assertEquals(round(time_took, 1), 0) # no memcache, no limiting
Example #2
0
 def test_ratelimit_set_incr(self):
     current_rate = 5
     num_calls = 50
     conf_dict = {'account_ratelimit': current_rate}
     self.test_ratelimit = ratelimit.filter_factory(conf_dict)(FakeApp())
     ratelimit.http_connect = mock_http_connect(204)
     req = Request.blank('/v/a/c')
     req.method = 'PUT'
     req.environ['chase.cache'] = FakeMemcache()
     req.environ['chase.cache'].init_incr_return_neg = True
     make_app_call = lambda: self.test_ratelimit(req.environ,
                                                 start_response)
     begin = time.time()
     self._run(make_app_call, num_calls, current_rate, check_time=False)
     self.assertEquals(round(time.time() - begin, 1), 9.8)
Example #3
0
 def test_account_ratelimit(self):
     current_rate = 5
     num_calls = 50
     conf_dict = {'account_ratelimit': current_rate}
     self.test_ratelimit = ratelimit.filter_factory(conf_dict)(FakeApp())
     ratelimit.http_connect = mock_http_connect(204)
     for meth, exp_time in [('DELETE', 9.8), ('GET', 0),
                        ('POST', 0), ('PUT', 9.8)]:
         req = Request.blank('/v/a%s/c' % meth)
         req.method = meth
         req.environ['chase.cache'] = FakeMemcache()
         make_app_call = lambda: self.test_ratelimit(req.environ,
                                                     start_response)
         begin = time.time()
         self._run(make_app_call, num_calls, current_rate,
                   check_time=bool(exp_time))
         self.assertEquals(round(time.time() - begin, 1), exp_time)
         self._reset_time()