diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 7fa85c4aa..ea0402221 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -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 diff --git a/src/async-queue/src/JobMessage.php b/src/async-queue/src/JobMessage.php index 72f0c0b50..bdd0384fc 100644 --- a/src/async-queue/src/JobMessage.php +++ b/src/async-queue/src/JobMessage.php @@ -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 diff --git a/src/async-queue/tests/JobMessageTest.php b/src/async-queue/tests/JobMessageTest.php index 8667bc583..6775c0aec 100644 --- a/src/async-queue/tests/JobMessageTest.php +++ b/src/async-queue/tests/JobMessageTest.php @@ -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()); + } } diff --git a/src/async-queue/tests/RedisDriverTest.php b/src/async-queue/tests/RedisDriverTest.php index b3eb7d736..c8cf90595 100644 --- a/src/async-queue/tests/RedisDriverTest.php +++ b/src/async-queue/tests/RedisDriverTest.php @@ -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); diff --git a/src/async-queue/tests/Stub/OldJobMessage.php b/src/async-queue/tests/Stub/OldJobMessage.php new file mode 100644 index 000000000..a364c3af9 --- /dev/null +++ b/src/async-queue/tests/Stub/OldJobMessage.php @@ -0,0 +1,29 @@ +uncompress(); + } + + $this->job = $job; + $this->attempts = $attempts; + } +}