From 1b0a682cfdacc9f470ffcd61e997ac36908bd88a Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Wed, 11 May 2016 17:31:46 +0200 Subject: [PATCH] [backend] Improve tests to be faster yet more reliable --- beat/web/backend/tests.py | 44 +++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/beat/web/backend/tests.py b/beat/web/backend/tests.py index 2f6785947..8b83f072a 100644 --- a/beat/web/backend/tests.py +++ b/beat/web/backend/tests.py @@ -52,16 +52,17 @@ from .management.commands import qsetup from .schedule import schedule -def _sleep(seconds): +def _sleep(tries, condition): """For some reason, time.sleep is not reliable on this test unit. Use this""" - slept = 0 - while slept < seconds: - #print("Slept %g seconds" % slept) - start = time.time() - time.sleep(seconds - slept) - slept += time.time() - start - #print("End special _sleep after %g seconds" % slept) + seconds = 1.0 #between tries + for i in range(tries): + if condition(): return + slept = 0 + while slept < seconds: + start = time.time() + time.sleep(seconds - slept) + slept += time.time() - start # Example configuration with 3 queues with an increasing amount of resources @@ -2568,7 +2569,12 @@ class WorkingExternally(TransactionTestCase): # actually runs the job (non-blocking) worker.work(self.environments, self.cpulimit, self.process) - _sleep(5) + + def condition(): + xp.refresh_from_db() + block = xp.blocks.first() + return block.status == Block.CACHED + _sleep(20, condition) # at this point, split should have been successful which shall # trigger job deletion and block update @@ -2607,7 +2613,11 @@ class WorkingExternally(TransactionTestCase): # actually runs the job (non-blocking) worker.work(self.environments, self.cpulimit, self.process) - _sleep(5) #wait job completion + + def condition(): + xp.refresh_from_db() + return xp.status == Experiment.DONE + _sleep(20, condition) #wait job completion # checks the number of statistics objects has increased by 1 self.assertEqual(HourlyStatistics.objects.count(), current_stats + 1) @@ -2615,7 +2625,7 @@ class WorkingExternally(TransactionTestCase): # at this point, split should have been successful which shall # trigger job deletion and block update xp.refresh_from_db() - block = xp.blocks.first() + block = xp.blocks.last() self.assertEqual(block.status, Block.CACHED) self.assertEqual(xp.status, Experiment.DONE) @@ -2661,7 +2671,11 @@ class WorkingExternally(TransactionTestCase): # actually runs the job (non-blocking) worker.work(self.environments, self.cpulimit, self.process) - _sleep(5) #wait till job has started (will sleep for 30 seconds) + + def condition(): + xp.refresh_from_db() + return xp.status == Experiment.RUNNING + _sleep(20, condition) # cancels the experiment xp.cancel() @@ -2670,7 +2684,11 @@ class WorkingExternally(TransactionTestCase): # launch another working cycle to kill the process worker.work(self.environments, self.cpulimit, self.process) - _sleep(5) #wait till done (sqlite doesn't impl. select_for_update()) + + def condition(): + xp.refresh_from_db() + return xp.status == Experiment.FAILED and Job.objects.count() == 0 + _sleep(20, condition) xp.refresh_from_db() # assert we have no database traces after the last block is done -- GitLab