Fixed bug that the older versions of parsers cannot parse new messages for async-queue.

This commit is contained in:
Deeka Wong 2024-02-01 14:16:45 +08:00 committed by GitHub
parent ff178141e8
commit 894e8d2410
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 77 additions and 2 deletions

View File

@ -1,5 +1,9 @@
# v3.1.9 - TBD
## Optimized
[#6517](https://github.com/hyperf/hyperf/pull/6517) Fixed bug that the older versions of parsers cannot parse new messages for `async-queue`.
# v3.1.8 - 2024-02-01
## Fixed

View File

@ -29,7 +29,12 @@ class JobMessage implements MessageInterface
$this->job = $this->job->compress();
}
return [$this->job, $this->attempts];
return [
$this->job, // Compatible with old version, will be removed at v3.2
$this->attempts, // Compatible with old version, will be removed at v3.2
'job' => $this->job,
'attempts' => $this->attempts,
];
}
public function __unserialize(array $data): void

View File

@ -33,6 +33,9 @@ class JobMessageTest extends TestCase
);
$serialized = $message->__serialize();
$this->assertEquals($serialized[0], $serialized['job']);
$this->assertEquals($serialized[1], $serialized['attempts']);
$this->assertArrayHasKey('job', $serialized);
$this->assertArrayHasKey('attempts', $serialized);
@ -52,6 +55,17 @@ class JobMessageTest extends TestCase
$serialized = $message->__serialize();
$this->assertEquals($serialized[0], $serialized['job']);
$this->assertEquals($serialized[1], $serialized['attempts']);
$message = unserialize(serialize($message));
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(0, $message->getAttempts());
$serialized = [
'job' => $serialized['job'] ?? $serialized[0],
'attempts' => 3,
@ -75,4 +89,27 @@ class JobMessageTest extends TestCase
$this->assertSame($id, $message->job()->id);
$this->assertSame(5, $message->getAttempts());
}
public function testUnserializeAsOldJobMessage()
{
$id = rand(0, 9999);
$message = new JobMessage(
new DemoJob($id)
);
$serialized = serialize($message);
$serialized = str_replace(
sprintf('O:%d:"%s', strlen(\Hyperf\AsyncQueue\JobMessage::class), \Hyperf\AsyncQueue\JobMessage::class),
sprintf('O:%d:"%s', strlen(\HyperfTest\AsyncQueue\Stub\OldJobMessage::class), \HyperfTest\AsyncQueue\Stub\OldJobMessage::class),
$serialized
);
$message = unserialize($serialized);
$this->assertInstanceOf(MessageInterface::class, $message);
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(JobInterface::class, $message->job());
$this->assertInstanceOf(DemoJob::class, $message->job());
$this->assertSame($id, $message->job()->id);
$this->assertSame(0, $message->getAttempts());
}
}

View File

@ -115,7 +115,7 @@ class RedisDriverTest extends TestCase
$driver->push(new DemoJob($id, $model));
$serialized = (string) Context::get('test.async-queue.lpush.value');
$this->assertSame(248, strlen($serialized));
$this->assertSame(264, strlen($serialized));
/** @var JobMessage $class */
$class = $packer->unpack($serialized);

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\AsyncQueue\Stub;
use Hyperf\Contract\UnCompressInterface;
class OldJobMessage extends \Hyperf\AsyncQueue\JobMessage
{
public function __unserialize(array $data): void
{
[$job, $attempts] = $data;
if ($job instanceof UnCompressInterface) {
$job = $job->uncompress();
}
$this->job = $job;
$this->attempts = $attempts;
}
}