test_upstreams_rr.py (1396:cb1fac1a237f) test_upstreams_rr.py (1441:b2caecb224f7)
1import os
2import re
3import unittest
4from unit.applications.lang.python import TestApplicationPython
5
6
7class TestUpstreamsRR(TestApplicationPython):
8 prerequisites = {'modules': ['python']}

--- 179 unchanged lines hidden (view full) ---

188 ),
189 'configure weight 2',
190 )
191
192 resps = self.get_resps_sc()
193 self.assertEqual(resps[0], 60, 'weight 2 0')
194 self.assertEqual(resps[2], 40, 'weight 2 1')
195
1import os
2import re
3import unittest
4from unit.applications.lang.python import TestApplicationPython
5
6
7class TestUpstreamsRR(TestApplicationPython):
8 prerequisites = {'modules': ['python']}

--- 179 unchanged lines hidden (view full) ---

188 ),
189 'configure weight 2',
190 )
191
192 resps = self.get_resps_sc()
193 self.assertEqual(resps[0], 60, 'weight 2 0')
194 self.assertEqual(resps[2], 40, 'weight 2 1')
195
196 def test_upstreams_rr_weight_rational(self):
197 def set_weights(w1, w2):
198 self.assertIn(
199 'success',
200 self.conf(
201 {
202 "127.0.0.1:7081": {"weight": w1},
203 "127.0.0.1:7082": {"weight": w2},
204 },
205 'upstreams/one/servers',
206 ),
207 'configure weights',
208 )
209
210 def check_reqs(w1, w2, reqs=10):
211 resps = self.get_resps_sc(req=reqs)
212 self.assertEqual(resps[0], reqs * w1 / (w1 + w2), 'weight 1')
213 self.assertEqual(resps[1], reqs * w2 / (w1 + w2), 'weight 2')
214
215 def check_weights(w1, w2):
216 set_weights(w1, w2)
217 check_reqs(w1, w2)
218
219 check_weights(0, 1)
220 check_weights(0, 999999.0123456)
221 check_weights(1, 9)
222 check_weights(100000, 900000)
223 check_weights(1, .25)
224 check_weights(1, 0.25)
225 check_weights(0.2, .8)
226 check_weights(1, 1.5)
227 check_weights(1e-3, 1E-3)
228 check_weights(1e-20, 1e-20)
229 check_weights(1e4, 1e4)
230 check_weights(1000000, 1000000)
231
232 set_weights(0.25, 0.25)
233 self.assertIn(
234 'success',
235 self.conf_delete('upstreams/one/servers/127.0.0.1:7081/weight'),
236 'delete weight',
237 )
238 check_reqs(1, 0.25)
239
240 self.assertIn(
241 'success',
242 self.conf(
243 {
244 "127.0.0.1:7081": {"weight": 0.1},
245 "127.0.0.1:7082": {"weight": 1},
246 "127.0.0.1:7083": {"weight": 0.9},
247 },
248 'upstreams/one/servers',
249 ),
250 'configure weights',
251 )
252 resps = self.get_resps_sc(req=20)
253 self.assertEqual(resps[0], 1, 'weight 3 1')
254 self.assertEqual(resps[1], 10, 'weight 3 2')
255 self.assertEqual(resps[2], 9, 'weight 3 3')
256
196 def test_upstreams_rr_independent(self):
197 def sum_resps(*args):
198 sum = [0] * len(args[0])
199 for arg in args:
200 sum = [x + y for x, y in zip(sum, arg)]
201
202 return sum
203

--- 220 unchanged lines hidden (view full) ---

424 self.assertEqual(resps[1], 50, 'ipv6 1')
425
426 def test_upstreams_rr_servers_empty(self):
427 self.assertIn(
428 'success',
429 self.conf({}, 'upstreams/one/servers'),
430 'configure servers empty',
431 )
257 def test_upstreams_rr_independent(self):
258 def sum_resps(*args):
259 sum = [0] * len(args[0])
260 for arg in args:
261 sum = [x + y for x, y in zip(sum, arg)]
262
263 return sum
264

--- 220 unchanged lines hidden (view full) ---

485 self.assertEqual(resps[1], 50, 'ipv6 1')
486
487 def test_upstreams_rr_servers_empty(self):
488 self.assertIn(
489 'success',
490 self.conf({}, 'upstreams/one/servers'),
491 'configure servers empty',
492 )
432
433 self.assertEqual(self.get()['status'], 502, 'servers empty')
434
493 self.assertEqual(self.get()['status'], 502, 'servers empty')
494
495 self.assertIn(
496 'success',
497 self.conf(
498 {"127.0.0.1:7081": {"weight": 0}}, 'upstreams/one/servers'
499 ),
500 'configure servers empty one',
501 )
502 self.assertEqual(self.get()['status'], 502, 'servers empty one')
503 self.assertIn(
504 'success',
505 self.conf(
506 {
507 "127.0.0.1:7081": {"weight": 0},
508 "127.0.0.1:7082": {"weight": 0},
509 },
510 'upstreams/one/servers',
511 ),
512 'configure servers empty two',
513 )
514 self.assertEqual(self.get()['status'], 502, 'servers empty two')
515
435 def test_upstreams_rr_invalid(self):
436 self.assertIn(
437 'error', self.conf({}, 'upstreams'), 'upstreams empty',
438 )
439 self.assertIn(
440 'error', self.conf({}, 'upstreams/one'), 'named upstreams empty',
441 )
442 self.assertIn(
443 'error',
444 self.conf({}, 'upstreams/one/servers/127.0.0.1'),
445 'invalid address',
446 )
447 self.assertIn(
448 'error',
449 self.conf({}, 'upstreams/one/servers/127.0.0.1:7081/blah'),
450 'invalid server option',
451 )
516 def test_upstreams_rr_invalid(self):
517 self.assertIn(
518 'error', self.conf({}, 'upstreams'), 'upstreams empty',
519 )
520 self.assertIn(
521 'error', self.conf({}, 'upstreams/one'), 'named upstreams empty',
522 )
523 self.assertIn(
524 'error',
525 self.conf({}, 'upstreams/one/servers/127.0.0.1'),
526 'invalid address',
527 )
528 self.assertIn(
529 'error',
530 self.conf({}, 'upstreams/one/servers/127.0.0.1:7081/blah'),
531 'invalid server option',
532 )
452 self.assertIn(
453 'error',
454 self.conf({}, 'upstreams/one/servers/127.0.0.1:7081/weight'),
455 'invalid weight option',
456 )
457 self.assertIn(
458 'error',
459 self.conf('-1', 'upstreams/one/servers/127.0.0.1:7081/weight'),
460 'invalid negative weight',
461 )
462
533
534 def check_weight(w):
535 self.assertIn(
536 'error',
537 self.conf(w, 'upstreams/one/servers/127.0.0.1:7081/weight'),
538 'invalid weight option',
539 )
540 check_weight({})
541 check_weight('-1')
542 check_weight('1.')
543 check_weight('1.1.')
544 check_weight('.')
545 check_weight('.01234567890123')
546 check_weight('1000001')
547 check_weight('2e6')
463
548
549
464if __name__ == '__main__':
465 TestUpstreamsRR.main()
550if __name__ == '__main__':
551 TestUpstreamsRR.main()