timing_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import unittest
  4. import timing
  5. import threading
  6. import time
  7. exec_num = 0
  8. class TimingTest(unittest.TestCase):
  9. def func(self):
  10. global exec_num
  11. self.name = 'TANGS TIMING TEST'
  12. exec_num += 1
  13. print(self.name)
  14. @staticmethod
  15. def reset():
  16. global exec_num
  17. exec_num = 0
  18. @staticmethod
  19. def timing_start():
  20. t = threading.Thread(target=timing.run)
  21. t.setDaemon(True)
  22. t.start()
  23. print('New Thread', t.name)
  24. return t
  25. def test_timing_run(self):
  26. # add task but invalid task_id
  27. err = None
  28. try:
  29. timing.add_task(1, 1, self.func, 1, 1)
  30. except Exception as e:
  31. err = str(e)
  32. self.assertEqual(err, 'task_id must be str')
  33. # add task but invalid interval
  34. err = None
  35. try:
  36. timing.add_task(1, '1', self.func, 1, 'generate_id_1')
  37. except Exception as e:
  38. err = str(e)
  39. self.assertEqual(err, 'interval must be int')
  40. # add task but interval must be bigger than 0
  41. err = None
  42. try:
  43. timing.add_task(1, -1, self.func, 1, 'generate_id_2')
  44. except Exception as e:
  45. err = str(e)
  46. self.assertEqual(err, 'interval must be bigger than 0')
  47. # add task but func is not function
  48. err = None
  49. try:
  50. timing.add_task(1, 1, 1, 1, 'generate_id_3')
  51. except Exception as e:
  52. err = str(e)
  53. self.assertEqual(err, 'func must be func')
  54. # add task but count is not int
  55. err = None
  56. try:
  57. timing.add_task(1, 1, self.func, '1', 'generate_id_4')
  58. except Exception as e:
  59. err = str(e)
  60. self.assertEqual(err, 'count must be int')
  61. timing.set_interval(1)
  62. timer = timing.Timing()
  63. timing.defaultTiming = timer
  64. # add task success
  65. self.timing_start()
  66. timing.add_task(1, 1, self.func, 1, 'generate_id_5')
  67. self.assertEqual(len(timer.task), 1)
  68. time.sleep(1.5)
  69. timing.stop()
  70. self.assertEqual(exec_num, 1)
  71. self.assertEqual(len(timer.task), 0)
  72. time.sleep(1)
  73. self.reset()
  74. # add task with count 3, wait 3.5, success
  75. self.timing_start()
  76. timing.add_task(1, 1, self.func, 3, 'generate_id_6')
  77. self.assertEqual(len(timer.task), 1)
  78. time.sleep(3.5)
  79. timing.stop()
  80. self.assertEqual(exec_num, 3)
  81. self.assertEqual(len(timer.task), 0)
  82. time.sleep(1)
  83. self.reset()
  84. # add task with count 4, wait 3.5, success
  85. t = self.timing_start()
  86. timing.add_task('name_4', 1, self.func, 4, 'generate_id_7')
  87. time.sleep(3.5)
  88. timing.stop()
  89. # self.assertAlmostEqual(exec_num, 3, delta=1)
  90. time.sleep(5)
  91. self.assertEqual(exec_num, 3)
  92. self.assertEqual(len(timer.task), 1)
  93. self.assertEqual(t.is_alive(), False)
  94. timer.task = {}
  95. self.reset()
  96. # add task with count less than 0, success
  97. print('-----------------------------------------')
  98. t = self.timing_start()
  99. timing.add_task('name', 1, self.func, -1, 'generate_id_8')
  100. time.sleep(3.5)
  101. timing.stop()
  102. time.sleep(5)
  103. self.assertEqual(exec_num, 3)
  104. self.assertEqual(len(timer.task), 1)
  105. self.assertEqual(t.is_alive(), False)
  106. timer.task = {}
  107. self.reset()
  108. # add multi task, success
  109. self.timing_start()
  110. timing.add_task('name_9', 1, self.func, 0, 'generate_id_9')
  111. timing.add_task('name_10', 1, self.func, 0, 'generate_id_10')
  112. timing.add_task('name_11', 1, self.func, 0, 'generate_id_11')
  113. timing.add_task('name_12', 1, self.func, 0, 'generate_id_12')
  114. timing.add_task('name_13', 1, self.func, 0, 'generate_id_13')
  115. timing.add_task('name_14', 1, self.func, 0, 'generate_id_14')
  116. timing.add_task('name_15', 1, self.func, 0, 'generate_id_15')
  117. timing.add_task('name_16', 1, self.func, 0, 'generate_id_16')
  118. timing.add_task('name_17', 1, self.func, 0, 'generate_id_17')
  119. timing.add_task('name_18', 1, self.func, 0, 'generate_id_18')
  120. timing.add_task('name_19', 1, self.func, 0, 'generate_id_19')
  121. timing.add_task('name_20', 1, self.func, 0, 'generate_id_20')
  122. time.sleep(10)
  123. self.assertAlmostEqual(exec_num, 100, delta=10)
  124. timing.stop()
  125. print(exec_num)
  126. if __name__ == '__main__':
  127. unittest.main()