Skip to content

Tests fifo dalayed #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/drivers/redis/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ protected function reserve($timeout)
protected function moveExpired($from)
{
$now = time();
if ($expired = $this->redis->zrevrangebyscore($from, $now, '-inf')) {
if ($expired = $this->redis->zrangebyscore($from, '-inf', $now)) {
$this->redis->zremrangebyscore($from, '-inf', $now);
foreach ($expired as $id) {
$this->redis->rpush("$this->channel.waiting", $id);
$this->redis->lpush("$this->channel.waiting", $id);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/app/HeavyJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace tests\app;

use Yii;
use yii\base\BaseObject;
use yii\queue\JobInterface;

/**
* Simple Job.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
class HeavyJob extends BaseObject implements JobInterface
{
public $uid;
public $load;

public function execute($queue)
{
file_put_contents($this->getFileName(), '');
sleep($this->load);
}

public function getFileName()
{
return Yii::getAlias("@runtime/job-{$this->uid}.lock");
}
}
43 changes: 43 additions & 0 deletions tests/drivers/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace tests\drivers;

use tests\app\HeavyJob;
use Yii;
use tests\app\SimpleJob;
use yii\queue\Queue;
Expand All @@ -33,6 +34,18 @@ protected function createSimpleJob()
]);
}

/**
* @param int $load
* @return HeavyJob
*/
protected function createHeavyJob($load=0)
{
return new HeavyJob([
'uid' => uniqid(),
'load' =>$load
]);
}

/**
* @param SimpleJob $job
*/
Expand Down Expand Up @@ -65,6 +78,36 @@ protected function assertSimpleJobLaterDone(SimpleJob $job, $delay)
$this->assertGreaterThanOrEqual($time, filemtime($job->getFileName()));
}

/**
* @param HeavyJob $job
* @param int $delay
* @param $initialTime
*/
protected function assertHeavyJobLaterDone(HeavyJob $job, $delay, $initialTime)
{
$time = $initialTime + $delay;
sleep($delay);
$timeout = 5000000; // 5 sec
$step = 50000;
while (!file_exists($job->getFileName()) && $timeout > 0) {
usleep($step);
$timeout -= $step;
}
$this->assertFileExists($job->getFileName());
$this->assertGreaterThanOrEqual($time, filemtime($job->getFileName()));
}

/**
* @param HeavyJob $job
* @param int $delay
*/
protected function assertHeavyJobDelayedFifoDone(HeavyJob $job1, HeavyJob $job2, $msg)
{
$this->assertFileExists($job1->getFileName());
$this->assertFileExists($job2->getFileName());
$this->assertGreaterThanOrEqual(filemtime($job1->getFileName()), filemtime($job2->getFileName()), $msg);
}

/**
* @inheritdoc
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/drivers/redis/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ public function testLater()
$this->assertSimpleJobLaterDone($job, 2);
}

public function testHeavyJobDelayedFifo()
{
$this->startProcess(['php', 'yii', 'queue/listen', '1']);

$delay = 1;
$load = $delay+1;
$initialTimeFirstBatch = time();

$job1 = $this->createHeavyJob($load);
$this->getQueue()->delay($delay)->push($job1);

$job2 = $this->createHeavyJob($load);
$this->getQueue()->delay($delay)->push($job2);

sleep($delay);

$initialTimeSecondBatch = time();
$job3 = $this->createHeavyJob($load);
$this->getQueue()->delay($delay)->push($job3);


$this->assertHeavyJobLaterDone($job1, $delay, $initialTimeFirstBatch);
$this->assertHeavyJobLaterDone($job2, $delay, $initialTimeFirstBatch);
$this->assertHeavyJobLaterDone($job3, $delay, $initialTimeSecondBatch);

$this->assertHeavyJobDelayedFifoDone($job1, $job2, 'Job1 < Job2');
$this->assertHeavyJobDelayedFifoDone($job2, $job3, 'Job2 < Job3');
}

public function testRetry()
{
$this->startProcess(['php', 'yii', 'queue/listen', '1']);
Expand Down