|
10 | 10 | use tests\app\RetryJob;
|
11 | 11 | use tests\drivers\CliTestCase;
|
12 | 12 | use Yii;
|
| 13 | +use yii\di\Instance; |
13 | 14 | use yii\queue\redis\Queue;
|
14 | 15 |
|
15 | 16 | /**
|
@@ -137,4 +138,53 @@ protected function tearDown()
|
137 | 138 | $this->getQueue()->redis->flushdb();
|
138 | 139 | parent::tearDown();
|
139 | 140 | }
|
| 141 | + |
| 142 | + /** |
| 143 | + * Verify that Redis data persists when process crashes during moveExpired. |
| 144 | + * |
| 145 | + * Steps: |
| 146 | + * 1. Push a delayed job into queue |
| 147 | + * 2. Wait for the job to expire |
| 148 | + * 3. Mock Redis to simulate crash during moveExpired |
| 149 | + * 4. Successfully process job after recovery |
| 150 | + */ |
| 151 | + public function testConsumeDelayedMessageAtLeastOnce() |
| 152 | + { |
| 153 | + $job = $this->createSimpleJob(); |
| 154 | + $this->getQueue()->delay(1)->push($job); |
| 155 | + // Expect a single message to be received. |
| 156 | + $messageCount = 0; |
| 157 | + $this->getQueue()->messageHandler = function () use(&$messageCount) { |
| 158 | + $messageCount++; |
| 159 | + }; |
| 160 | + |
| 161 | + // Ensure the delayed message can be consumed when more time passed than the delay is. |
| 162 | + sleep(2); |
| 163 | + |
| 164 | + // Based on the implemention, emulate a crash when redis "rpush" |
| 165 | + // command should be executed. |
| 166 | + $mockRedis = Instance::ensure([ |
| 167 | + 'class' => RedisCrashMock::class, |
| 168 | + 'hostname' => getenv('REDIS_HOST') ?: 'localhost', |
| 169 | + 'database' => getenv('REDIS_DB') ?: 1, |
| 170 | + 'crashOnCommand' => 'rpush' // Crash when trying to move job to waiting queue. |
| 171 | + ], 'yii\redis\Connection'); |
| 172 | + |
| 173 | + $queue = $this->getQueue(); |
| 174 | + $old = $queue->redis; |
| 175 | + $queue->redis = $mockRedis; |
| 176 | + |
| 177 | + try { |
| 178 | + $queue->run(false); |
| 179 | + } catch (\Exception $e) { |
| 180 | + // Ignore exceptions. |
| 181 | + } finally { |
| 182 | + $queue->redis = $old; |
| 183 | + } |
| 184 | + |
| 185 | + // Ensure the red lock is invalid. The red lock is valid for 1s. |
| 186 | + sleep(2); |
| 187 | + $this->getQueue()->run(false); |
| 188 | + $this->assertEquals(1, $messageCount); |
| 189 | + } |
140 | 190 | }
|
0 commit comments