From d732617bb174dbb522fd95de657773e248268749 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <gohr@cosmocode.de>
Date: Thu, 1 Dec 2016 10:02:28 +0100
Subject: [PATCH] new helper method waitForTick() in DokuWikiTest

Some of our tests require that at least one second has passed before
they can continue because our revisions depend on the Unix Timestamp.

Currently we use a sleep(1) for this. However this always waits a whole
second, even if the next second is already much closer or maybe already
here - especially when some processing has been done since the last
operation.

This new method waits for the next second by checking the time every
10th of a second. This might speed up some of our tests a bit.
---
 _test/core/DokuWikiTest.php | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/_test/core/DokuWikiTest.php b/_test/core/DokuWikiTest.php
index 4e8c8fcef..89a98a45e 100644
--- a/_test/core/DokuWikiTest.php
+++ b/_test/core/DokuWikiTest.php
@@ -156,4 +156,26 @@ abstract class DokuWikiTest extends PHPUnit_Framework_TestCase {
             return $this->getMock($originalClassName, $methods);
         }
     }
+
+    /**
+     * Waits until a new second has passed
+     *
+     * The very first call will return immeadiately, proceeding calls will return
+     * only after at least 1 second after the last call has passed.
+     *
+     * When passing $init=true it will not return immeadiately but use the current
+     * second as initialization. It might still return faster than a second.
+     *
+     * @param bool $init wait from now on, not from last time
+     * @return int new timestamp
+     */
+    protected function waitForTick($init = false) {
+        static $last = 0;
+        if($init) $last = time();
+        while($last === $now = time()) {
+            usleep(100000); //recheck in a 10th of a second
+        }
+        $last = $now;
+        return $now;
+    }
 }
-- 
GitLab