From 5fc76f069fd1d3ff3d87640adc279fe5f2aa66a4 Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.dos.anjos@gmail.com>
Date: Sun, 8 May 2016 17:59:51 +0200
Subject: [PATCH] [backend] Patch unreliable sleep issue

---
 beat/web/backend/tests.py | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/beat/web/backend/tests.py b/beat/web/backend/tests.py
index be2cc170e..4ea21ef7a 100644
--- a/beat/web/backend/tests.py
+++ b/beat/web/backend/tests.py
@@ -52,6 +52,18 @@ from .management.commands import qsetup
 from .schedule import schedule
 
 
+def _sleep(seconds):
+  """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)
+
+
 # Example configuration with 3 queues with an increasing amount of resources
 # running on the same host
 QUEUES_WITHOUT_PRIORITY = {
@@ -2556,7 +2568,7 @@ class WorkingExternally(TransactionTestCase):
 
         # actually runs the job (non-blocking)
         worker.work(self.environments, self.cpulimit, self.process)
-        time.sleep(5) #wait job completion
+        _sleep(5)
 
         # at this point, split should have been successful which shall
         # trigger job deletion and block update
@@ -2595,7 +2607,7 @@ class WorkingExternally(TransactionTestCase):
 
         # actually runs the job (non-blocking)
         worker.work(self.environments, self.cpulimit, self.process)
-        time.sleep(5) #wait job completion
+        _sleep(5) #wait job completion
 
         # checks the number of statistics objects has increased by 1
         self.assertEqual(HourlyStatistics.objects.count(), current_stats + 1)
@@ -2650,7 +2662,7 @@ class WorkingExternally(TransactionTestCase):
 
         # actually runs the job (non-blocking)
         worker.work(self.environments, self.cpulimit, self.process)
-        time.sleep(5) #wait till done (sqlite doesn't impl. select_for_update())
+        _sleep(5) #wait till done (sqlite doesn't impl. select_for_update())
 
         # cancels the experiment
         xp.cancel()
@@ -2659,7 +2671,7 @@ class WorkingExternally(TransactionTestCase):
 
         # launch another working cycle to kill the process
         worker.work(self.environments, self.cpulimit, self.process)
-        time.sleep(5) #wait till done (sqlite doesn't impl. select_for_update())
+        _sleep(5) #wait till done (sqlite doesn't impl. select_for_update())
         xp.refresh_from_db()
 
         # assert we have no database traces after the last block is done
-- 
GitLab