jenkins-bot has submitted this change and it was merged.

Change subject: popAtomic doesn't run callback if queue is empty
......................................................................


popAtomic doesn't run callback if queue is empty

Callbacks should expect actual messages and not have to check for
the end of the queue.

Change-Id: I2fd93aa6f7ea6ba796a83a67a33f3bf9e02f72b8
---
M src/PHPQueue/Backend/PDO.php
M src/PHPQueue/Interfaces/AtomicReadBuffer.php
M test/PHPQueue/Backend/PDOBaseTest.php
3 files changed, 20 insertions(+), 3 deletions(-)

Approvals:
  XenoRyet: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/PHPQueue/Backend/PDO.php b/src/PHPQueue/Backend/PDO.php
index c8d4f1c..b17d7ec 100644
--- a/src/PHPQueue/Backend/PDO.php
+++ b/src/PHPQueue/Backend/PDO.php
@@ -157,10 +157,12 @@
             $this->getConnection()->beginTransaction();
             $data = $this->pop();
 
-            if (!is_callable($callback)) {
-                throw new \RuntimeException("Bad callback passed to " . 
__METHOD__);
+            if ($data !== null) {
+                if (!is_callable($callback)) {
+                    throw new \RuntimeException("Bad callback passed to " . 
__METHOD__);
+                }
+                call_user_func($callback, $data);
             }
-            call_user_func($callback, $data);
 
             $this->getConnection()->commit();
             return $data;
diff --git a/src/PHPQueue/Interfaces/AtomicReadBuffer.php 
b/src/PHPQueue/Interfaces/AtomicReadBuffer.php
index bb5dcfd..d6fea1a 100644
--- a/src/PHPQueue/Interfaces/AtomicReadBuffer.php
+++ b/src/PHPQueue/Interfaces/AtomicReadBuffer.php
@@ -23,6 +23,7 @@
      *         Throwing an exception from callback means that we were unable or
      *     chose not to handle the message at all, and it should be considered
      *     unconsumed.  In this case it is not popped when popAtomic returns.
+     *         If there are no messages in the queue, the callback is not run.
      *
      * @return array|null popAtomic returns the currently popped record as a
      *     courtesy.  Note that any atomic processing should happen within
diff --git a/test/PHPQueue/Backend/PDOBaseTest.php 
b/test/PHPQueue/Backend/PDOBaseTest.php
index dd96d66..9e646b7 100644
--- a/test/PHPQueue/Backend/PDOBaseTest.php
+++ b/test/PHPQueue/Backend/PDOBaseTest.php
@@ -141,4 +141,18 @@
         // Punchline: data should still be available for the retry pop.
         $this->assertEquals($data, $this->object->popAtomic(function 
($message) {}));
     }
+
+    /**
+     * popAtomic should not call the callback if there are no messages
+     */
+    public function testPopAtomicEmpty()
+    {
+        $did_run = false;
+        $callback = function ($unused) use (&$did_run) {
+            $did_run = true;
+        };
+        $data = $this->object->popAtomic($callback);
+        $this->assertNull($data, 'Should return null on an empty queue');
+        $this->assertFalse($did_run, 'Should not call callback without a 
message');
+    }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/287126
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2fd93aa6f7ea6ba796a83a67a33f3bf9e02f72b8
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/php-queue
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to