|
7 | 7 | use Cake\Console\ConsoleIo; |
8 | 8 | use Cake\Core\Configure; |
9 | 9 | use Cake\Datasource\ConnectionManager; |
| 10 | +use Cake\Event\EventList; |
| 11 | +use Cake\Event\EventManager; |
10 | 12 | use Cake\TestSuite\TestCase; |
11 | 13 | use Psr\Log\NullLogger; |
12 | 14 | use Queue\Console\Io; |
13 | 15 | use Queue\Queue\Processor; |
| 16 | +use Queue\Queue\Task\RetryExampleTask; |
| 17 | +use RuntimeException; |
14 | 18 | use Shim\TestSuite\ConsoleOutput; |
15 | 19 | use Shim\TestSuite\TestTrait; |
16 | 20 |
|
@@ -111,4 +115,51 @@ protected function _needsConnection() { |
111 | 115 | $this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.'); |
112 | 116 | } |
113 | 117 |
|
| 118 | + /** |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function testMaxAttemptsExhaustedEvent() { |
| 122 | + // Set up event tracking |
| 123 | + $eventList = new EventList(); |
| 124 | + EventManager::instance()->setEventList($eventList); |
| 125 | + |
| 126 | + // Create a job that will fail |
| 127 | + $QueuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs'); |
| 128 | + $job = $QueuedJobs->createJob('Queue.RetryExample', [], ['priority' => 1]); |
| 129 | + |
| 130 | + // Manually set attempts to 5 (simulating previous failed attempts) |
| 131 | + // The default RetryExampleTask has retries=4, so 5 attempts exceeds it |
| 132 | + $job->attempts = 5; |
| 133 | + $QueuedJobs->saveOrFail($job); |
| 134 | + |
| 135 | + // Create processor |
| 136 | + $out = new ConsoleOutput(); |
| 137 | + $err = new ConsoleOutput(); |
| 138 | + $processor = new Processor(new Io(new ConsoleIo($out, $err)), new NullLogger()); |
| 139 | + |
| 140 | + // Create a mock task that always fails |
| 141 | + $mockTask = $this->getMockBuilder(RetryExampleTask::class) |
| 142 | + ->setConstructorArgs([new Io(new ConsoleIo($out, $err)), new NullLogger()]) |
| 143 | + ->onlyMethods(['run']) |
| 144 | + ->getMock(); |
| 145 | + $mockTask->method('run')->willThrowException(new RuntimeException('Task failed')); |
| 146 | + |
| 147 | + // Mock only the loadTask method |
| 148 | + $processor = $this->getMockBuilder(Processor::class) |
| 149 | + ->setConstructorArgs([new Io(new ConsoleIo($out, $err)), new NullLogger()]) |
| 150 | + ->onlyMethods(['loadTask']) |
| 151 | + ->getMock(); |
| 152 | + $processor->method('loadTask')->willReturn($mockTask); |
| 153 | + |
| 154 | + // Run the job (it will fail and should trigger the event) |
| 155 | + $this->invokeMethod($processor, 'runJob', [$job, 'test-pid']); |
| 156 | + |
| 157 | + // Check that the event was dispatched |
| 158 | + $this->assertEventFired('Queue.Job.maxAttemptsExhausted'); |
| 159 | + |
| 160 | + // Verify event data |
| 161 | + // The event was fired successfully (assertEventFired passed) |
| 162 | + // We don't need to check the event data again since assertEventFired confirms it was fired |
| 163 | + } |
| 164 | + |
114 | 165 | } |
0 commit comments