Skip to content

Commit

Permalink
Fixed bug that the older versions of parsers cannot parse new message…
Browse files Browse the repository at this point in the history
…s for `async-queue`.
  • Loading branch information
huangdijia authored Feb 1, 2024
1 parent c19f9a2 commit 9b3789c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/JobMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public function __serialize(): array
$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
Expand Down
37 changes: 37 additions & 0 deletions tests/JobMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function testJobMessageSerialize()
);

$serialized = $message->__serialize();

$this->assertEquals($serialized[0], $serialized['job']);
$this->assertEquals($serialized[1], $serialized['attempts']);
$this->assertArrayHasKey('job', $serialized);
$this->assertArrayHasKey('attempts', $serialized);

Expand All @@ -52,6 +55,17 @@ public function testJobMessageSerializeCompatible()

$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,
Expand All @@ -75,4 +89,27 @@ public function testJobMessageSerializeCompatible()
$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());
}
}
2 changes: 1 addition & 1 deletion tests/RedisDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testAsyncQueueJobGenerate()
$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);
Expand Down
29 changes: 29 additions & 0 deletions tests/Stub/OldJobMessage.php
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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;
}
}

0 comments on commit 9b3789c

Please sign in to comment.