@@ -72,12 +72,20 @@ def delete(self, key):
72
72
73
73
74
74
class FakeApp (object ):
75
+ skip_handled_check = False
75
76
76
77
def __call__ (self , env , start_response ):
78
+ assert self .skip_handled_check or env .get ('swift.ratelimit.handled' )
77
79
start_response ('200 OK' , [])
78
80
return [b'Some Content' ]
79
81
80
82
83
+ class FakeReq (object ):
84
+ def __init__ (self , method , env = None ):
85
+ self .method = method
86
+ self .environ = env or {}
87
+
88
+
81
89
def start_response (* args ):
82
90
pass
83
91
@@ -160,36 +168,29 @@ def test_get_ratelimitable_key_tuples(self):
160
168
{'object_count' : '5' }
161
169
the_app = ratelimit .filter_factory (conf_dict )(FakeApp ())
162
170
the_app .memcache_client = fake_memcache
163
- req = lambda : None
164
- req .environ = {'swift.cache' : fake_memcache , 'PATH_INFO' : '/v1/a/c/o' }
171
+ environ = {'swift.cache' : fake_memcache , 'PATH_INFO' : '/v1/a/c/o' }
165
172
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
166
173
lambda * args , ** kwargs : {}):
167
- req .method = 'DELETE'
168
174
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
169
- req , 'a' , None , None )), 0 )
170
- req .method = 'PUT'
175
+ FakeReq ('DELETE' , environ ), 'a' , None , None )), 0 )
171
176
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
172
- req , 'a' , 'c' , None )), 1 )
173
- req .method = 'DELETE'
177
+ FakeReq ('PUT' , environ ), 'a' , 'c' , None )), 1 )
174
178
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
175
- req , 'a' , 'c' , None )), 1 )
176
- req .method = 'GET'
179
+ FakeReq ('DELETE' , environ ), 'a' , 'c' , None )), 1 )
177
180
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
178
- req , 'a' , 'c' , 'o' )), 0 )
179
- req .method = 'PUT'
181
+ FakeReq ('GET' , environ ), 'a' , 'c' , 'o' )), 0 )
180
182
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
181
- req , 'a' , 'c' , 'o' )), 1 )
183
+ FakeReq ( 'PUT' , environ ) , 'a' , 'c' , 'o' )), 1 )
182
184
183
- req .method = 'PUT'
184
185
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
185
- req , 'a' , 'c' , None , global_ratelimit = 10 )), 2 )
186
+ FakeReq ( 'PUT' , environ ) , 'a' , 'c' , None , global_ratelimit = 10 )), 2 )
186
187
self .assertEqual (the_app .get_ratelimitable_key_tuples (
187
- req , 'a' , 'c' , None , global_ratelimit = 10 )[1 ],
188
+ FakeReq ( 'PUT' , environ ) , 'a' , 'c' , None , global_ratelimit = 10 )[1 ],
188
189
('ratelimit/global-write/a' , 10 ))
189
190
190
- req .method = 'PUT'
191
191
self .assertEqual (len (the_app .get_ratelimitable_key_tuples (
192
- req , 'a' , 'c' , None , global_ratelimit = 'notafloat' )), 1 )
192
+ FakeReq ('PUT' , environ ), 'a' , 'c' , None ,
193
+ global_ratelimit = 'notafloat' )), 1 )
193
194
194
195
def test_memcached_container_info_dict (self ):
195
196
mdict = headers_to_container_info ({'x-container-object-count' : '45' })
@@ -204,9 +205,8 @@ def test_ratelimit_old_memcache_format(self):
204
205
{'container_size' : 5 }
205
206
the_app = ratelimit .filter_factory (conf_dict )(FakeApp ())
206
207
the_app .memcache_client = fake_memcache
207
- req = lambda : None
208
- req .method = 'PUT'
209
- req .environ = {'PATH_INFO' : '/v1/a/c/o' , 'swift.cache' : fake_memcache }
208
+ req = FakeReq ('PUT' , {
209
+ 'PATH_INFO' : '/v1/a/c/o' , 'swift.cache' : fake_memcache })
210
210
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
211
211
lambda * args , ** kwargs : {}):
212
212
tuples = the_app .get_ratelimitable_key_tuples (req , 'a' , 'c' , 'o' )
@@ -227,8 +227,8 @@ def test_account_ratelimit(self):
227
227
req = Request .blank ('/v1/a%s/c' % meth )
228
228
req .method = meth
229
229
req .environ ['swift.cache' ] = FakeMemcache ()
230
- make_app_call = lambda : self .test_ratelimit (req . environ ,
231
- start_response )
230
+ make_app_call = lambda : self .test_ratelimit (
231
+ req . environ . copy (), start_response )
232
232
begin = time .time ()
233
233
self ._run (make_app_call , num_calls , current_rate ,
234
234
check_time = bool (exp_time ))
@@ -244,7 +244,7 @@ def test_ratelimit_set_incr(self):
244
244
req .method = 'PUT'
245
245
req .environ ['swift.cache' ] = FakeMemcache ()
246
246
req .environ ['swift.cache' ].init_incr_return_neg = True
247
- make_app_call = lambda : self .test_ratelimit (req .environ ,
247
+ make_app_call = lambda : self .test_ratelimit (req .environ . copy () ,
248
248
start_response )
249
249
begin = time .time ()
250
250
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
@@ -260,15 +260,15 @@ def test_ratelimit_old_white_black_list(self):
260
260
'account_whitelist' : 'a' ,
261
261
'account_blacklist' : 'b' }
262
262
self .test_ratelimit = ratelimit .filter_factory (conf_dict )(FakeApp ())
263
- req = Request .blank ('/' )
264
263
with mock .patch .object (self .test_ratelimit ,
265
264
'memcache_client' , FakeMemcache ()):
266
265
self .assertEqual (
267
- self .test_ratelimit .handle_ratelimit (req , 'a' , 'c' , 'o' ),
266
+ self .test_ratelimit .handle_ratelimit (
267
+ Request .blank ('/' ), 'a' , 'c' , 'o' ),
268
268
None )
269
269
self .assertEqual (
270
270
self .test_ratelimit .handle_ratelimit (
271
- req , 'b' , 'c' , 'o' ).status_int ,
271
+ Request . blank ( '/' ) , 'b' , 'c' , 'o' ).status_int ,
272
272
497 )
273
273
274
274
def test_ratelimit_whitelist_sysmeta (self ):
@@ -331,7 +331,7 @@ def __init__(self, parent):
331
331
self .parent = parent
332
332
333
333
def run (self ):
334
- self .result = self .parent .test_ratelimit (req .environ ,
334
+ self .result = self .parent .test_ratelimit (req .environ . copy () ,
335
335
start_response )
336
336
337
337
def get_fake_ratelimit (* args , ** kwargs ):
@@ -370,18 +370,17 @@ def test_ratelimit_max_rate_double(self):
370
370
# simulates 4 requests coming in at same time, then sleeping
371
371
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
372
372
lambda * args , ** kwargs : {}):
373
- r = self .test_ratelimit (req .environ , start_response )
373
+ r = self .test_ratelimit (req .environ . copy () , start_response )
374
374
mock_sleep (.1 )
375
- r = self .test_ratelimit (req .environ , start_response )
375
+ r = self .test_ratelimit (req .environ . copy () , start_response )
376
376
mock_sleep (.1 )
377
- r = self .test_ratelimit (req .environ , start_response )
377
+ r = self .test_ratelimit (req .environ . copy () , start_response )
378
378
self .assertEqual (r [0 ], b'Slow down' )
379
379
mock_sleep (.1 )
380
- r = self .test_ratelimit (req .environ , start_response )
380
+ r = self .test_ratelimit (req .environ . copy () , start_response )
381
381
self .assertEqual (r [0 ], b'Slow down' )
382
382
mock_sleep (.1 )
383
- r = self .test_ratelimit (req .environ , start_response )
384
- print (repr (r ))
383
+ r = self .test_ratelimit (req .environ .copy (), start_response )
385
384
self .assertEqual (r [0 ], b'Some Content' )
386
385
387
386
def test_ratelimit_max_rate_double_container (self ):
@@ -404,17 +403,17 @@ def test_ratelimit_max_rate_double_container(self):
404
403
# simulates 4 requests coming in at same time, then sleeping
405
404
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
406
405
lambda * args , ** kwargs : {}):
407
- r = self .test_ratelimit (req .environ , start_response )
406
+ r = self .test_ratelimit (req .environ . copy () , start_response )
408
407
mock_sleep (.1 )
409
- r = self .test_ratelimit (req .environ , start_response )
408
+ r = self .test_ratelimit (req .environ . copy () , start_response )
410
409
mock_sleep (.1 )
411
- r = self .test_ratelimit (req .environ , start_response )
410
+ r = self .test_ratelimit (req .environ . copy () , start_response )
412
411
self .assertEqual (r [0 ], b'Slow down' )
413
412
mock_sleep (.1 )
414
- r = self .test_ratelimit (req .environ , start_response )
413
+ r = self .test_ratelimit (req .environ . copy () , start_response )
415
414
self .assertEqual (r [0 ], b'Slow down' )
416
415
mock_sleep (.1 )
417
- r = self .test_ratelimit (req .environ , start_response )
416
+ r = self .test_ratelimit (req .environ . copy () , start_response )
418
417
self .assertEqual (r [0 ], b'Some Content' )
419
418
420
419
def test_ratelimit_max_rate_double_container_listing (self ):
@@ -437,17 +436,17 @@ def test_ratelimit_max_rate_double_container_listing(self):
437
436
lambda * args , ** kwargs : {}):
438
437
time_override = [0 , 0 , 0 , 0 , None ]
439
438
# simulates 4 requests coming in at same time, then sleeping
440
- r = self .test_ratelimit (req .environ , start_response )
439
+ r = self .test_ratelimit (req .environ . copy () , start_response )
441
440
mock_sleep (.1 )
442
- r = self .test_ratelimit (req .environ , start_response )
441
+ r = self .test_ratelimit (req .environ . copy () , start_response )
443
442
mock_sleep (.1 )
444
- r = self .test_ratelimit (req .environ , start_response )
443
+ r = self .test_ratelimit (req .environ . copy () , start_response )
445
444
self .assertEqual (r [0 ], b'Slow down' )
446
445
mock_sleep (.1 )
447
- r = self .test_ratelimit (req .environ , start_response )
446
+ r = self .test_ratelimit (req .environ . copy () , start_response )
448
447
self .assertEqual (r [0 ], b'Slow down' )
449
448
mock_sleep (.1 )
450
- r = self .test_ratelimit (req .environ , start_response )
449
+ r = self .test_ratelimit (req .environ . copy () , start_response )
451
450
self .assertEqual (r [0 ], b'Some Content' )
452
451
mc = self .test_ratelimit .memcache_client
453
452
try :
@@ -466,9 +465,6 @@ def test_ratelimit_max_rate_multiple_acc(self):
466
465
467
466
the_app = ratelimit .filter_factory (conf_dict )(FakeApp ())
468
467
the_app .memcache_client = fake_memcache
469
- req = lambda : None
470
- req .method = 'PUT'
471
- req .environ = {}
472
468
473
469
class rate_caller (threading .Thread ):
474
470
@@ -478,8 +474,8 @@ def __init__(self, name):
478
474
479
475
def run (self ):
480
476
for j in range (num_calls ):
481
- self .result = the_app .handle_ratelimit (req , self . myname ,
482
- 'c' , None )
477
+ self .result = the_app .handle_ratelimit (
478
+ FakeReq ( 'PUT' ), self . myname , 'c' , None )
483
479
484
480
with mock .patch ('swift.common.middleware.ratelimit.get_account_info' ,
485
481
lambda * args , ** kwargs : {}):
@@ -541,7 +537,9 @@ def test_no_memcache(self):
541
537
current_rate = 13
542
538
num_calls = 5
543
539
conf_dict = {'account_ratelimit' : current_rate }
544
- self .test_ratelimit = ratelimit .filter_factory (conf_dict )(FakeApp ())
540
+ fake_app = FakeApp ()
541
+ fake_app .skip_handled_check = True
542
+ self .test_ratelimit = ratelimit .filter_factory (conf_dict )(fake_app )
545
543
req = Request .blank ('/v1/a' )
546
544
req .environ ['swift.cache' ] = None
547
545
make_app_call = lambda : self .test_ratelimit (req .environ ,
@@ -551,6 +549,24 @@ def test_no_memcache(self):
551
549
time_took = time .time () - begin
552
550
self .assertEqual (round (time_took , 1 ), 0 ) # no memcache, no limiting
553
551
552
+ def test_already_handled (self ):
553
+ current_rate = 13
554
+ num_calls = 5
555
+ conf_dict = {'container_listing_ratelimit_0' : current_rate }
556
+ self .test_ratelimit = ratelimit .filter_factory (conf_dict )(FakeApp ())
557
+ fake_cache = FakeMemcache ()
558
+ fake_cache .set (
559
+ get_cache_key ('a' , 'c' ),
560
+ {'object_count' : 1 })
561
+ req = Request .blank ('/v1/a/c' , environ = {'swift.cache' : fake_cache })
562
+ req .environ ['swift.ratelimit.handled' ] = True
563
+ make_app_call = lambda : self .test_ratelimit (req .environ ,
564
+ start_response )
565
+ begin = time .time ()
566
+ self ._run (make_app_call , num_calls , current_rate , check_time = False )
567
+ time_took = time .time () - begin
568
+ self .assertEqual (round (time_took , 1 ), 0 ) # no memcache, no limiting
569
+
554
570
def test_restarting_memcache (self ):
555
571
current_rate = 2
556
572
num_calls = 5
0 commit comments