Skip to content

Commit 9b3789c

Browse files
authored
Fixed bug that the older versions of parsers cannot parse new messages for async-queue.
1 parent c19f9a2 commit 9b3789c

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/JobMessage.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ public function __serialize(): array
2929
$this->job = $this->job->compress();
3030
}
3131

32-
return [$this->job, $this->attempts];
32+
return [
33+
$this->job, // Compatible with old version, will be removed at v3.2
34+
$this->attempts, // Compatible with old version, will be removed at v3.2
35+
'job' => $this->job,
36+
'attempts' => $this->attempts,
37+
];
3338
}
3439

3540
public function __unserialize(array $data): void

tests/JobMessageTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function testJobMessageSerialize()
3333
);
3434

3535
$serialized = $message->__serialize();
36+
37+
$this->assertEquals($serialized[0], $serialized['job']);
38+
$this->assertEquals($serialized[1], $serialized['attempts']);
3639
$this->assertArrayHasKey('job', $serialized);
3740
$this->assertArrayHasKey('attempts', $serialized);
3841

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

5356
$serialized = $message->__serialize();
5457

58+
$this->assertEquals($serialized[0], $serialized['job']);
59+
$this->assertEquals($serialized[1], $serialized['attempts']);
60+
61+
$message = unserialize(serialize($message));
62+
$this->assertInstanceOf(MessageInterface::class, $message);
63+
$this->assertInstanceOf(JobInterface::class, $message->job());
64+
$this->assertInstanceOf(JobInterface::class, $message->job());
65+
$this->assertInstanceOf(DemoJob::class, $message->job());
66+
$this->assertSame($id, $message->job()->id);
67+
$this->assertSame(0, $message->getAttempts());
68+
5569
$serialized = [
5670
'job' => $serialized['job'] ?? $serialized[0],
5771
'attempts' => 3,
@@ -75,4 +89,27 @@ public function testJobMessageSerializeCompatible()
7589
$this->assertSame($id, $message->job()->id);
7690
$this->assertSame(5, $message->getAttempts());
7791
}
92+
93+
public function testUnserializeAsOldJobMessage()
94+
{
95+
$id = rand(0, 9999);
96+
$message = new JobMessage(
97+
new DemoJob($id)
98+
);
99+
100+
$serialized = serialize($message);
101+
$serialized = str_replace(
102+
sprintf('O:%d:"%s', strlen(\Hyperf\AsyncQueue\JobMessage::class), \Hyperf\AsyncQueue\JobMessage::class),
103+
sprintf('O:%d:"%s', strlen(\HyperfTest\AsyncQueue\Stub\OldJobMessage::class), \HyperfTest\AsyncQueue\Stub\OldJobMessage::class),
104+
$serialized
105+
);
106+
$message = unserialize($serialized);
107+
108+
$this->assertInstanceOf(MessageInterface::class, $message);
109+
$this->assertInstanceOf(JobInterface::class, $message->job());
110+
$this->assertInstanceOf(JobInterface::class, $message->job());
111+
$this->assertInstanceOf(DemoJob::class, $message->job());
112+
$this->assertSame($id, $message->job()->id);
113+
$this->assertSame(0, $message->getAttempts());
114+
}
78115
}

tests/RedisDriverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testAsyncQueueJobGenerate()
115115
$driver->push(new DemoJob($id, $model));
116116

117117
$serialized = (string) Context::get('test.async-queue.lpush.value');
118-
$this->assertSame(248, strlen($serialized));
118+
$this->assertSame(264, strlen($serialized));
119119

120120
/** @var JobMessage $class */
121121
$class = $packer->unpack($serialized);

tests/Stub/OldJobMessage.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\AsyncQueue\Stub;
13+
14+
use Hyperf\Contract\UnCompressInterface;
15+
16+
class OldJobMessage extends \Hyperf\AsyncQueue\JobMessage
17+
{
18+
public function __unserialize(array $data): void
19+
{
20+
[$job, $attempts] = $data;
21+
22+
if ($job instanceof UnCompressInterface) {
23+
$job = $job->uncompress();
24+
}
25+
26+
$this->job = $job;
27+
$this->attempts = $attempts;
28+
}
29+
}

0 commit comments

Comments
 (0)