|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespaceSymfony\Component\Messenger\Tests\Handler; |
| 13 | + |
| 14 | +useAmp\Future; |
| 15 | +usePHPUnit\Framework\TestCase; |
| 16 | +useSymfony\Component\Messenger\Envelope; |
| 17 | +useSymfony\Component\Messenger\Handler\Acknowledger; |
| 18 | +useSymfony\Component\Messenger\Handler\BatchAsyncHandlerTrait; |
| 19 | +useSymfony\Component\Messenger\ParallelMessageBus; |
| 20 | +useSymfony\Component\Messenger\Stamp\FutureStamp; |
| 21 | + |
| 22 | +class BatchAsyncHandlerTraitTestextends TestCase |
| 23 | +{ |
| 24 | +publicfunctiontestHandleWillProcessMessagesAfterReachingBatchSize() |
| 25 | + { |
| 26 | +$handler =newTestBatchAsyncHandler(); |
| 27 | +$handler->setBatchSize(2); |
| 28 | + |
| 29 | +$message1 =new \stdClass(); |
| 30 | +$message2 =new \stdClass(); |
| 31 | +$message3 =new \stdClass(); |
| 32 | + |
| 33 | +$result1 =$handler->handle($message1,newAcknowledger('TestBatchAsyncHandler')); |
| 34 | +$this->assertSame(1,$result1); |
| 35 | +$this->assertEmpty($handler->getProcessedJobs()); |
| 36 | + |
| 37 | +$result2 =$handler->handle($message2,newAcknowledger('TestBatchAsyncHandler')); |
| 38 | +$this->assertSame(0,$result2); |
| 39 | +$this->assertCount(2,$handler->getProcessedJobs()[0]); |
| 40 | + |
| 41 | +$result3 =$handler->handle($message3,newAcknowledger('TestBatchAsyncHandler')); |
| 42 | +$this->assertSame(1,$result3); |
| 43 | +$this->assertCount(2,$handler->getProcessedJobs()[0]); |
| 44 | + } |
| 45 | + |
| 46 | +publicfunctiontestHandleWithNullAcknowledgerProcessesImmediately() |
| 47 | + { |
| 48 | +$handler =newTestBatchAsyncHandler(); |
| 49 | +$handler->setBatchSize(5); |
| 50 | + |
| 51 | +$message =new \stdClass(); |
| 52 | +$message->payload ='test'; |
| 53 | + |
| 54 | +$result =$handler->handle($message,null); |
| 55 | +$this->assertSame('processed:test',$result); |
| 56 | +$this->assertCount(1,$handler->getProcessedJobs()[0]); |
| 57 | + } |
| 58 | + |
| 59 | +publicfunctiontestFlushWithForceTrueProcessesRegardlessOfBatchSize() |
| 60 | + { |
| 61 | +$handler =newTestBatchAsyncHandler(); |
| 62 | +$handler->setBatchSize(5); |
| 63 | + |
| 64 | +$message1 =new \stdClass(); |
| 65 | +$message2 =new \stdClass(); |
| 66 | + |
| 67 | +$handler->handle($message1,newAcknowledger('TestBatchAsyncHandler')); |
| 68 | +$handler->handle($message2,newAcknowledger('TestBatchAsyncHandler')); |
| 69 | + |
| 70 | +$this->assertCount(0,$handler->getProcessedJobs()); |
| 71 | + |
| 72 | +$handler->flush(true); |
| 73 | +$this->assertCount(2,$handler->getProcessedJobs()[0]); |
| 74 | + } |
| 75 | + |
| 76 | +publicfunctiontestParallelDispatch() |
| 77 | + { |
| 78 | +$message1 =new \stdClass(); |
| 79 | +$message1->payload ='test1'; |
| 80 | + |
| 81 | +$message2 =new \stdClass(); |
| 82 | +$message2->payload ='test2'; |
| 83 | + |
| 84 | +$future1 =$this->createMock(Future::class); |
| 85 | +$future1->expects($this->once()) |
| 86 | + ->method('await') |
| 87 | + ->willReturn('future_result1'); |
| 88 | + |
| 89 | +$future2 =$this->createMock(Future::class); |
| 90 | +$future2->expects($this->once()) |
| 91 | + ->method('await') |
| 92 | + ->willReturn('future_result2'); |
| 93 | + |
| 94 | +$futureStamp1 =newFutureStamp($future1); |
| 95 | +$futureStamp2 =newFutureStamp($future2); |
| 96 | + |
| 97 | +$envelope1 =newEnvelope($message1, [$futureStamp1]); |
| 98 | +$envelope2 =newEnvelope($message2, [$futureStamp2]); |
| 99 | + |
| 100 | +$parallelBus =$this->createMock(ParallelMessageBus::class); |
| 101 | +$parallelBus->expects($this->exactly(2)) |
| 102 | + ->method('dispatch') |
| 103 | + ->willReturnOnConsecutiveCalls($envelope1,$envelope2); |
| 104 | + |
| 105 | +$handler =newTestBatchAsyncHandler(); |
| 106 | +$handler->setBatchSize(2); |
| 107 | +$handler->setParallelMessageBus($parallelBus); |
| 108 | + |
| 109 | +$ack1 =$this->createMock(Acknowledger::class); |
| 110 | +$ack1->expects($this->once()) |
| 111 | + ->method('ack') |
| 112 | + ->with('future_result1'); |
| 113 | + |
| 114 | +$ack2 =$this->createMock(Acknowledger::class); |
| 115 | +$ack2->expects($this->once()) |
| 116 | + ->method('ack') |
| 117 | + ->with('future_result2'); |
| 118 | + |
| 119 | +$handler->handle($message1,$ack1); |
| 120 | +$handler->handle($message2,$ack2); |
| 121 | + |
| 122 | +// Jobs should have been dispatched via the parallel bus |
| 123 | +$this->assertEmpty($handler->getProcessedJobs()); |
| 124 | + } |
| 125 | + |
| 126 | +publicfunctiontestFallbackToSyncProcessingWhenNoBusAvailable() |
| 127 | + { |
| 128 | +$handler =newTestBatchAsyncHandler(); |
| 129 | +$handler->setBatchSize(2); |
| 130 | + |
| 131 | +$message1 =new \stdClass(); |
| 132 | +$message2 =new \stdClass(); |
| 133 | + |
| 134 | +$handler->handle($message1,newAcknowledger('TestBatchAsyncHandler')); |
| 135 | +$handler->handle($message2,newAcknowledger('TestBatchAsyncHandler')); |
| 136 | + |
| 137 | +$this->assertCount(2,$handler->getProcessedJobs()[0]); |
| 138 | + } |
| 139 | + |
| 140 | +publicfunctiontestParallelDispatchWithException() |
| 141 | + { |
| 142 | +$message =new \stdClass(); |
| 143 | +$message->payload ='test_exception'; |
| 144 | + |
| 145 | +$future =$this->createMock(Future::class); |
| 146 | +$future->expects($this->once()) |
| 147 | + ->method('await') |
| 148 | + ->willThrowException(new \RuntimeException('Test exception')); |
| 149 | + |
| 150 | +$futureStamp =newFutureStamp($future); |
| 151 | +$envelope =newEnvelope($message, [$futureStamp]); |
| 152 | + |
| 153 | +$parallelBus =$this->createMock(ParallelMessageBus::class); |
| 154 | +$parallelBus->expects($this->once()) |
| 155 | + ->method('dispatch') |
| 156 | + ->willReturn($envelope); |
| 157 | + |
| 158 | +$handler =newTestBatchAsyncHandler(); |
| 159 | +$handler->setParallelMessageBus($parallelBus); |
| 160 | + |
| 161 | +$ack =$this->createMock(Acknowledger::class); |
| 162 | +$ack->expects($this->once()) |
| 163 | + ->method('nack') |
| 164 | + ->with($this->isInstanceOf(\RuntimeException::class)); |
| 165 | + |
| 166 | +$handler->handle($message,$ack); |
| 167 | +$handler->flush(true); |
| 168 | + } |
| 169 | + |
| 170 | +publicfunctiontestCleanupWorker() |
| 171 | + { |
| 172 | +$handler =newTestBatchAsyncHandler(); |
| 173 | + |
| 174 | +$message =new \stdClass(); |
| 175 | +$handler->handle($message,newAcknowledger('TestBatchAsyncHandler')); |
| 176 | + |
| 177 | +$this->assertEmpty($handler->getProcessedJobs()); |
| 178 | + |
| 179 | +$handler->cleanupWorker(); |
| 180 | + |
| 181 | +$this->assertCount(1,$handler->getProcessedJobs()[0]); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Test implementation of BatchAsyncHandlerTrait. |
| 187 | + */ |
| 188 | +class TestBatchAsyncHandler |
| 189 | +{ |
| 190 | +use BatchAsyncHandlerTrait { |
| 191 | + BatchAsyncHandlerTrait::getCurrentWorkerIdasprivate traitGetCurrentWorkerId; |
| 192 | + } |
| 193 | + |
| 194 | +privateint$batchSize =10; |
| 195 | +privatearray$processedJobs = []; |
| 196 | + |
| 197 | +publicfunctionhandle(object$message, ?Acknowledger$ack):mixed |
| 198 | + { |
| 199 | +return$this->{'handle'}($message,$ack); |
| 200 | + } |
| 201 | + |
| 202 | +privatefunctionprocess(array$jobs):void |
| 203 | + { |
| 204 | +$this->processedJobs[] =$jobs; |
| 205 | + |
| 206 | +foreach ($jobsas [$message,$ack]) { |
| 207 | +if (property_exists($message,'payload')) { |
| 208 | +$result ='processed:' .$message->payload; |
| 209 | +$ack->ack($result); |
| 210 | + }else { |
| 211 | +$ack->ack(true); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | +publicfunctionsetBatchSize(int$size):void |
| 217 | + { |
| 218 | +$this->batchSize =$size; |
| 219 | + } |
| 220 | + |
| 221 | +publicfunctiongetProcessedJobs():array |
| 222 | + { |
| 223 | +return$this->processedJobs; |
| 224 | + } |
| 225 | + |
| 226 | +privatefunctiongetBatchSize():int |
| 227 | + { |
| 228 | +return$this->batchSize; |
| 229 | + } |
| 230 | + |
| 231 | +privatefunctiongetCurrentWorkerId():string |
| 232 | + { |
| 233 | +return'test-worker-id'; |
| 234 | + } |
| 235 | +} |