Skip to content

Commit c4eea5f

Browse files
committed
Fix Redis queue serializer test pinning
1 parent 9a5c2ed commit c4eea5f

3 files changed

Lines changed: 35 additions & 58 deletions

File tree

tests/Integration/Queue/RedisQueueTest.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hypervel\Queue\Events\JobQueueing;
1313
use Hypervel\Queue\Jobs\RedisJob;
1414
use Hypervel\Queue\RedisQueue;
15+
use Hypervel\Redis\RedisConnection;
1516
use Hypervel\Redis\RedisProxy;
1617
use Hypervel\Support\Facades\Redis;
1718
use Hypervel\Support\InteractsWithTime;
@@ -387,33 +388,40 @@ public function testBulkJobQueuedEvent()
387388
public function testDelayedJobsWorkWithPhpRedisSerializationEnabled()
388389
{
389390
$connection = Redis::connection('default');
390-
$client = $connection->client();
391391

392-
$originalSerializer = $client->getOption(\Redis::OPT_SERIALIZER);
393-
$client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
392+
$connection->withPinnedConnection(function () use ($connection): void {
393+
$client = $connection->withConnection(
394+
fn (RedisConnection $connection): ?\Redis => $connection->client()
395+
);
394396

395-
try {
396-
$this->setQueue($this->app['config']->get('queue.connections.redis.queue'));
397+
$this->assertInstanceOf(\Redis::class, $client);
397398

398-
$job = new RedisQueueIntegrationTestJob(42);
399-
$this->queue->later(-10, $job);
399+
$originalSerializer = $client->getOption(\Redis::OPT_SERIALIZER);
400+
$client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
400401

401-
$poppedJob = $this->queue->pop();
402+
try {
403+
$this->setQueue($this->app['config']->get('queue.connections.redis.queue'));
402404

403-
$this->assertNotNull($poppedJob, 'Delayed job should be retrievable after delay expires');
405+
$job = new RedisQueueIntegrationTestJob(42);
406+
$this->queue->later(-10, $job);
404407

405-
$rawBody = $poppedJob->getRawBody();
406-
$decoded = json_decode($rawBody);
408+
$poppedJob = $this->queue->pop();
407409

408-
$this->assertNotNull($decoded, 'Job payload should be valid JSON');
409-
$this->assertObjectHasProperty('data', $decoded, 'Decoded payload should have data property');
410+
$this->assertNotNull($poppedJob, 'Delayed job should be retrievable after delay expires');
410411

411-
$command = unserialize($decoded->data->command);
412-
$this->assertEquals($job, $command, 'Unserialized job should match original');
413-
$this->assertSame(42, $command->i, 'Job property should be preserved');
414-
} finally {
415-
$client->setOption(\Redis::OPT_SERIALIZER, $originalSerializer);
416-
}
412+
$rawBody = $poppedJob->getRawBody();
413+
$decoded = json_decode($rawBody);
414+
415+
$this->assertNotNull($decoded, 'Job payload should be valid JSON');
416+
$this->assertObjectHasProperty('data', $decoded, 'Decoded payload should have data property');
417+
418+
$command = unserialize($decoded->data->command);
419+
$this->assertEquals($job, $command, 'Unserialized job should match original');
420+
$this->assertSame(42, $command->i, 'Job property should be preserved');
421+
} finally {
422+
$client->setOption(\Redis::OPT_SERIALIZER, $originalSerializer);
423+
}
424+
});
417425
}
418426

419427
private function setQueue(?string $default = null, ?string $connection = null, ?int $retryAfter = 60, ?int $blockFor = null): void

tests/Redis/PhpRedisClusterConnectionTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ public function testScanTransformUsesExplicitNodeOption()
171171

172172
public function testFormatClusterPasswordReturnsArrayWhenUsernameAndPasswordProvided()
173173
{
174-
$connection = new class($this->getContainer(), $this->getMockedPool(), [
175-
'username' => 'myuser',
176-
'password' => 'mypass',
177-
]) extends PhpRedisClusterConnectionStub {
174+
$connection = new class($this->getContainer(), $this->getMockedPool(), ['username' => 'myuser', 'password' => 'mypass']) extends PhpRedisClusterConnectionStub {
178175
public function formatClusterPasswordForTest(): mixed
179176
{
180177
return $this->formatClusterPassword();
@@ -186,9 +183,7 @@ public function formatClusterPasswordForTest(): mixed
186183

187184
public function testFormatClusterPasswordReturnsPlainPasswordWithoutUsername()
188185
{
189-
$connection = new class($this->getContainer(), $this->getMockedPool(), [
190-
'password' => 'mypass',
191-
]) extends PhpRedisClusterConnectionStub {
186+
$connection = new class($this->getContainer(), $this->getMockedPool(), ['password' => 'mypass']) extends PhpRedisClusterConnectionStub {
192187
public function formatClusterPasswordForTest(): mixed
193188
{
194189
return $this->formatClusterPassword();
@@ -212,10 +207,7 @@ public function formatClusterPasswordForTest(): mixed
212207

213208
public function testFormatClusterPasswordReturnsPlainPasswordWhenUsernameIsEmpty()
214209
{
215-
$connection = new class($this->getContainer(), $this->getMockedPool(), [
216-
'username' => '',
217-
'password' => 'mypass',
218-
]) extends PhpRedisClusterConnectionStub {
210+
$connection = new class($this->getContainer(), $this->getMockedPool(), ['username' => '', 'password' => 'mypass']) extends PhpRedisClusterConnectionStub {
219211
public function formatClusterPasswordForTest(): mixed
220212
{
221213
return $this->formatClusterPassword();
@@ -227,10 +219,7 @@ public function formatClusterPasswordForTest(): mixed
227219

228220
public function testFormatClusterPasswordReturnsPlainPasswordWhenPasswordIsNotString()
229221
{
230-
$connection = new class($this->getContainer(), $this->getMockedPool(), [
231-
'username' => 'myuser',
232-
'password' => ['mypass'],
233-
]) extends PhpRedisClusterConnectionStub {
222+
$connection = new class($this->getContainer(), $this->getMockedPool(), ['username' => 'myuser', 'password' => ['mypass']]) extends PhpRedisClusterConnectionStub {
234223
public function formatClusterPasswordForTest(): mixed
235224
{
236225
return $this->formatClusterPassword();

tests/Redis/RedisConnectionTest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,15 +1854,7 @@ public function testReconnectSetsConnectionLevelPhpRedisOptions()
18541854
->once()
18551855
->with(Redis::OPT_BACKOFF_CAP, 2000);
18561856

1857-
new class($this->getContainer(), $pool, [
1858-
'host' => '127.0.0.1',
1859-
'port' => 6379,
1860-
'read_timeout' => 5.0,
1861-
'max_retries' => 4,
1862-
'backoff_algorithm' => 'constant',
1863-
'backoff_base' => 200,
1864-
'backoff_cap' => 2000,
1865-
], $redis) extends PhpRedisConnection {
1857+
new class($this->getContainer(), $pool, ['host' => '127.0.0.1', 'port' => 6379, 'read_timeout' => 5.0, 'max_retries' => 4, 'backoff_algorithm' => 'constant', 'backoff_base' => 200, 'backoff_cap' => 2000], $redis) extends PhpRedisConnection {
18661858
public function __construct(
18671859
ContainerContract $container,
18681860
PoolInterface $pool,
@@ -1885,11 +1877,7 @@ public function testReconnectDoesNotSetReadTimeoutOptionWhenEmpty()
18851877
$redis = m::mock(Redis::class);
18861878
$redis->shouldReceive('setOption')->never();
18871879

1888-
new class($this->getContainer(), $pool, [
1889-
'host' => '127.0.0.1',
1890-
'port' => 6379,
1891-
'read_timeout' => 0.0,
1892-
], $redis) extends PhpRedisConnection {
1880+
new class($this->getContainer(), $pool, ['host' => '127.0.0.1', 'port' => 6379, 'read_timeout' => 0.0], $redis) extends PhpRedisConnection {
18931881
public function __construct(
18941882
ContainerContract $container,
18951883
PoolInterface $pool,
@@ -1914,11 +1902,7 @@ public function testReconnectSetsNumericBackoffAlgorithmAsIs()
19141902
->once()
19151903
->with(Redis::OPT_BACKOFF_ALGORITHM, Redis::BACKOFF_ALGORITHM_DEFAULT);
19161904

1917-
new class($this->getContainer(), $pool, [
1918-
'host' => '127.0.0.1',
1919-
'port' => 6379,
1920-
'backoff_algorithm' => Redis::BACKOFF_ALGORITHM_DEFAULT,
1921-
], $redis) extends PhpRedisConnection {
1905+
new class($this->getContainer(), $pool, ['host' => '127.0.0.1', 'port' => 6379, 'backoff_algorithm' => Redis::BACKOFF_ALGORITHM_DEFAULT], $redis) extends PhpRedisConnection {
19221906
public function __construct(
19231907
ContainerContract $container,
19241908
PoolInterface $pool,
@@ -1943,11 +1927,7 @@ public function testReconnectThrowsOnUnknownBackoffAlgorithm()
19431927
$this->expectException(\Hypervel\Redis\Exceptions\InvalidRedisOptionException::class);
19441928
$this->expectExceptionMessage('Algorithm [bogus] is not a valid PhpRedis backoff algorithm.');
19451929

1946-
new class($this->getContainer(), $pool, [
1947-
'host' => '127.0.0.1',
1948-
'port' => 6379,
1949-
'backoff_algorithm' => 'bogus',
1950-
], $redis) extends PhpRedisConnection {
1930+
new class($this->getContainer(), $pool, ['host' => '127.0.0.1', 'port' => 6379, 'backoff_algorithm' => 'bogus'], $redis) extends PhpRedisConnection {
19511931
public function __construct(
19521932
ContainerContract $container,
19531933
PoolInterface $pool,

0 commit comments

Comments
 (0)