Added generate and degenerate.

This commit is contained in:
李铭昕 2019-08-08 10:00:10 +08:00
parent 1201d946ed
commit 2dae99714a
6 changed files with 157 additions and 3 deletions

View File

@ -12,7 +12,10 @@ declare(strict_types=1);
namespace Hyperf\AsyncQueue;
abstract class Job implements JobInterface
use Hyperf\Contract\CodeDegenerateInterface;
use Hyperf\Contract\CodeGenerateInterface;
abstract class Job implements JobInterface, CodeGenerateInterface, CodeDegenerateInterface
{
/**
* @var int
@ -23,4 +26,20 @@ abstract class Job implements JobInterface
{
return $this->maxAttempts;
}
/**
* @return JobInterface
*/
public function degenerate(): CodeGenerateInterface
{
foreach ($this as $key) {
}
}
/**
* @return JobInterface
*/
public function generate(): CodeDegenerateInterface
{
}
}

View File

@ -16,7 +16,10 @@ use Hyperf\AsyncQueue\Driver\RedisDriver;
use Hyperf\AsyncQueue\Message;
use Hyperf\Utils\Context;
use Hyperf\Utils\Packer\PhpSerializerPacker;
use Hyperf\Utils\Str;
use HyperfTest\AsyncQueue\Stub\DemoJob;
use HyperfTest\AsyncQueue\Stub\DemoModel;
use HyperfTest\AsyncQueue\Stub\DemoModelMeta;
use HyperfTest\AsyncQueue\Stub\Redis;
use Mockery;
use PHPUnit\Framework\TestCase;
@ -49,7 +52,7 @@ class RedisDriverTest extends TestCase
$id = uniqid();
$driver->push(new DemoJob($id));
/** @var Message $class */
$class = $packer->unpack(Context::get('test.async-queue.lpush.value'));
$class = $packer->unpack((string) Context::get('test.async-queue.lpush.value'));
$this->assertSame($id, $class->job()->id);
$key = Context::get('test.async-queue.lpush.key');
$this->assertSame('test:waiting', $key);
@ -57,11 +60,28 @@ class RedisDriverTest extends TestCase
$id = uniqid();
$driver->push(new DemoJob($id), 5);
/** @var Message $class */
$class = $packer->unpack(Context::get('test.async-queue.zadd.value'));
$class = $packer->unpack((string) Context::get('test.async-queue.zadd.value'));
$this->assertSame($id, $class->job()->id);
$key = Context::get('test.async-queue.zadd.key');
$this->assertSame('test:delayed', $key);
$time = Context::get('test.async-queue.zadd.delay');
$this->assertSame(time() + 5, $time);
}
public function testDemoModelGenerate()
{
$content = Str::random(1000);
$model = new DemoModel(1, 'Hyperf', 1, $content);
$s1 = serialize($model);
$this->assertSame(1128, strlen($s1));
$meta = $model->generate();
$s2 = serialize($meta);
$this->assertSame(65, strlen($s2));
$this->assertInstanceOf(DemoModelMeta::class, $meta);
$model2 = $meta->degenerate();
$this->assertEquals($model, $model2);
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue\Stub;
use Hyperf\Contract\CodeDegenerateInterface;
use Hyperf\Contract\CodeGenerateInterface;
use Hyperf\Utils\Context;
class DemoModel implements CodeGenerateInterface
{
public $id;
public $name;
public $gendar;
public $signature;
public function __construct($id, $name, $gendar, $signature)
{
$this->id = $id;
$this->name = $name;
$this->gendar = $gendar;
$this->signature = $signature;
}
public function generate(): CodeDegenerateInterface
{
Context::set('test.async-queue.demo.model.' . $this->id, [
$this->name, $this->gendar, $this->signature,
]);
return new DemoModelMeta($this->id);
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue\Stub;
use Hyperf\Contract\CodeDegenerateInterface;
use Hyperf\Contract\CodeGenerateInterface;
use Hyperf\Utils\Context;
class DemoModelMeta implements CodeDegenerateInterface
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
public function degenerate(): CodeGenerateInterface
{
$data = Context::get('test.async-queue.demo.model.' . $this->id);
return new DemoModel($this->id, ...$data);
}
}

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Contract;
interface CodeDegenerateInterface
{
public function degenerate(): CodeGenerateInterface;
}

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Contract;
interface CodeGenerateInterface
{
public function generate(): CodeDegenerateInterface;
}