Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_scheduling.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from __future__ import absolute_import
  2. from kombu.transport.virtual.scheduling import FairCycle
  3. from kombu.tests.case import Case
  4. class MyEmpty(Exception):
  5. pass
  6. def consume(fun, n):
  7. r = []
  8. for i in range(n):
  9. r.append(fun())
  10. return r
  11. class test_FairCycle(Case):
  12. def test_cycle(self):
  13. resources = ['a', 'b', 'c', 'd', 'e']
  14. def echo(r, timeout=None):
  15. return r
  16. # cycle should be ['a', 'b', 'c', 'd', 'e', ... repeat]
  17. cycle = FairCycle(echo, resources, MyEmpty)
  18. for i in range(len(resources)):
  19. self.assertEqual(cycle.get(), (resources[i],
  20. resources[i]))
  21. for i in range(len(resources)):
  22. self.assertEqual(cycle.get(), (resources[i],
  23. resources[i]))
  24. def test_cycle_breaks(self):
  25. resources = ['a', 'b', 'c', 'd', 'e']
  26. def echo(r):
  27. if r == 'c':
  28. raise MyEmpty(r)
  29. return r
  30. cycle = FairCycle(echo, resources, MyEmpty)
  31. self.assertEqual(
  32. consume(cycle.get, len(resources)),
  33. [('a', 'a'), ('b', 'b'), ('d', 'd'),
  34. ('e', 'e'), ('a', 'a')],
  35. )
  36. self.assertEqual(
  37. consume(cycle.get, len(resources)),
  38. [('b', 'b'), ('d', 'd'), ('e', 'e'),
  39. ('a', 'a'), ('b', 'b')],
  40. )
  41. cycle2 = FairCycle(echo, ['c', 'c'], MyEmpty)
  42. with self.assertRaises(MyEmpty):
  43. consume(cycle2.get, 3)
  44. def test_cycle_no_resources(self):
  45. cycle = FairCycle(None, [], MyEmpty)
  46. cycle.pos = 10
  47. with self.assertRaises(MyEmpty):
  48. cycle._next()
  49. def test__repr__(self):
  50. self.assertTrue(repr(FairCycle(lambda x: x, [1, 2, 3], MyEmpty)))