From 89d0bf038dba5026e3569e2fc00bcd8e77308c3d Mon Sep 17 00:00:00 2001 From: tienvx Date: Mon, 11 Apr 2022 21:30:13 +0700 Subject: [PATCH] Allow record video for bug manually --- src/Entity/Bug.php | 5 +++++ src/Model/Bug.php | 11 +++++++++++ src/Model/BugInterface.php | 4 ++++ src/Repository/BugRepository.php | 14 ++++++++++++++ src/Repository/BugRepositoryInterface.php | 4 ++++ src/Service/Bug/BugHelper.php | 10 ++++++++++ src/Service/Task/TaskHelper.php | 6 ++++-- tests/Model/BugTest.php | 2 ++ tests/Repository/BugRepositoryTest.php | 20 ++++++++++++++++++++ tests/Service/Bug/BugHelperTest.php | 12 ++++++++++++ tests/Service/Task/TaskHelperTest.php | 6 +++--- 11 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/Entity/Bug.php b/src/Entity/Bug.php index 121222fd..cc6972fb 100644 --- a/src/Entity/Bug.php +++ b/src/Entity/Bug.php @@ -60,6 +60,11 @@ class Bug extends BugModel */ protected bool $closed = false; + /** + * @ORM\Column(type="boolean") + */ + protected bool $recording = false; + /** * @ORM\Column(name="created_at", type="datetime") */ diff --git a/src/Model/Bug.php b/src/Model/Bug.php index 937938d7..c12d249e 100644 --- a/src/Model/Bug.php +++ b/src/Model/Bug.php @@ -14,6 +14,7 @@ class Bug implements BugInterface protected string $message; protected ProgressInterface $progress; protected bool $closed = false; + protected bool $recording = false; protected DateTimeInterface $updatedAt; protected DateTimeInterface $createdAt; @@ -100,6 +101,16 @@ public function setClosed(bool $closed): void $this->closed = $closed; } + public function isRecording(): bool + { + return $this->recording; + } + + public function setRecording(bool $recording): void + { + $this->recording = $recording; + } + public function setCreatedAt(DateTimeInterface $createdAt): void { $this->createdAt = $createdAt; diff --git a/src/Model/BugInterface.php b/src/Model/BugInterface.php index 730d85e6..91a3417c 100644 --- a/src/Model/BugInterface.php +++ b/src/Model/BugInterface.php @@ -40,6 +40,10 @@ public function isClosed(): bool; public function setClosed(bool $closed): void; + public function isRecording(): bool; + + public function setRecording(bool $recording): void; + public function setCreatedAt(DateTimeInterface $createdAt): void; public function getCreatedAt(): DateTimeInterface; diff --git a/src/Repository/BugRepository.php b/src/Repository/BugRepository.php index 7b08e485..f0c6b466 100644 --- a/src/Repository/BugRepository.php +++ b/src/Repository/BugRepository.php @@ -51,4 +51,18 @@ public function increaseTotal(BugInterface $bug, int $total): void $bug->getProgress()->setTotal($bug->getProgress()->getTotal() + $total); }); } + + public function startRecording(BugInterface $bug): void + { + $bug->setRecording(true); + $this->getEntityManager()->flush(); + } + + public function stopRecording(BugInterface $bug): void + { + $bug->setRecording(false); + // Recording bug may take long time. Reconnect to flush changes. + $this->getEntityManager()->getConnection()->connect(); + $this->getEntityManager()->flush(); + } } diff --git a/src/Repository/BugRepositoryInterface.php b/src/Repository/BugRepositoryInterface.php index 39a8c393..f834c7c8 100644 --- a/src/Repository/BugRepositoryInterface.php +++ b/src/Repository/BugRepositoryInterface.php @@ -12,4 +12,8 @@ public function updateSteps(BugInterface $bug, array $newSteps): void; public function increaseProcessed(BugInterface $bug, int $processed = 1): void; public function increaseTotal(BugInterface $bug, int $total): void; + + public function startRecording(BugInterface $bug): void; + + public function stopRecording(BugInterface $bug): void; } diff --git a/src/Service/Bug/BugHelper.php b/src/Service/Bug/BugHelper.php index 4ceadc99..b8aa9e28 100644 --- a/src/Service/Bug/BugHelper.php +++ b/src/Service/Bug/BugHelper.php @@ -2,6 +2,7 @@ namespace Tienvx\Bundle\MbtBundle\Service\Bug; +use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; use Tienvx\Bundle\MbtBundle\Entity\Bug; use Tienvx\Bundle\MbtBundle\Exception\ExceptionInterface; @@ -85,7 +86,16 @@ public function reportBug(int $bugId): void public function recordVideo(int $bugId): void { $bug = $this->getBug($bugId, 'record video for bug'); + + if ($bug->isRecording()) { + throw new RecoverableMessageHandlingException( + sprintf('Can not record video for bug %d: bug is recording. Will retry later', $bug->getId()) + ); + } + + $this->bugRepository->startRecording($bug); $this->stepsRunner->run($bug->getSteps(), $bug, true); + $this->bugRepository->stopRecording($bug); } public function createBug(array $steps, string $message): BugInterface diff --git a/src/Service/Task/TaskHelper.php b/src/Service/Task/TaskHelper.php index fb8b4bbc..52128fa1 100644 --- a/src/Service/Task/TaskHelper.php +++ b/src/Service/Task/TaskHelper.php @@ -2,8 +2,8 @@ namespace Tienvx\Bundle\MbtBundle\Service\Task; +use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; use Throwable; -use Tienvx\Bundle\MbtBundle\Exception\RuntimeException; use Tienvx\Bundle\MbtBundle\Exception\UnexpectedValueException; use Tienvx\Bundle\MbtBundle\Generator\GeneratorManagerInterface; use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface; @@ -44,7 +44,9 @@ public function run(int $taskId): void } if ($task->isRunning()) { - throw new RuntimeException(sprintf('Can not run task %d: task is already running', $task->getId())); + throw new RecoverableMessageHandlingException( + sprintf('Can not run task %d: task is running. Will retry later', $task->getId()) + ); } $this->taskRepository->startRunning($task); diff --git a/tests/Model/BugTest.php b/tests/Model/BugTest.php index 8b975b7a..8b712a2c 100644 --- a/tests/Model/BugTest.php +++ b/tests/Model/BugTest.php @@ -45,6 +45,7 @@ protected function setUp(): void $this->bug->setMessage('bug message'); $this->bug->setProgress($this->progress); $this->bug->setClosed(true); + $this->bug->setRecording(true); } public function testProperties(): void @@ -56,6 +57,7 @@ public function testProperties(): void $this->assertSame('bug message', $this->bug->getMessage()); $this->assertSame($this->progress, $this->bug->getProgress()); $this->assertSame(true, $this->bug->isClosed()); + $this->assertSame(true, $this->bug->isRecording()); } protected function createBug(): BugInterface diff --git a/tests/Repository/BugRepositoryTest.php b/tests/Repository/BugRepositoryTest.php index b7a89409..21d97f30 100644 --- a/tests/Repository/BugRepositoryTest.php +++ b/tests/Repository/BugRepositoryTest.php @@ -2,6 +2,7 @@ namespace Tienvx\Bundle\MbtBundle\Tests\Repository; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\LockMode; use Doctrine\ORM\Decorator\EntityManagerDecorator; use Doctrine\ORM\Mapping\ClassMetadata; @@ -150,4 +151,23 @@ public function testIncreaseTotal(): void $this->assertSame(5, $this->bug->getProgress()->getProcessed()); $this->assertSame(13, $this->bug->getProgress()->getTotal()); } + + public function testStartRecordingBug(): void + { + $this->bug->setRecording(false); + $this->manager->expects($this->once())->method('flush'); + $this->bugRepository->startRecording($this->bug); + $this->assertTrue($this->bug->isRecording()); + } + + public function testStopRecordingBug(): void + { + $connection = $this->createMock(Connection::class); + $connection->expects($this->once())->method('connect'); + $this->bug->setRecording(true); + $this->manager->expects($this->once())->method('flush'); + $this->manager->expects($this->once())->method('getConnection')->willReturn($connection); + $this->bugRepository->stopRecording($this->bug); + $this->assertFalse($this->bug->isRecording()); + } } diff --git a/tests/Service/Bug/BugHelperTest.php b/tests/Service/Bug/BugHelperTest.php index b604ab54..9af48b9e 100644 --- a/tests/Service/Bug/BugHelperTest.php +++ b/tests/Service/Bug/BugHelperTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; use Tienvx\Bundle\MbtBundle\Entity\Bug; use Tienvx\Bundle\MbtBundle\Entity\Progress; @@ -232,6 +233,15 @@ public function testRecordVideoMissingBug(): void $this->helper->recordVideo(123); } + public function testRunTaskAlreadyRunning(): void + { + $this->expectException(RecoverableMessageHandlingException::class); + $this->expectExceptionMessage('Can not record video for bug 123: bug is recording. Will retry later'); + $this->bug->setRecording(true); + $this->bugRepository->expects($this->once())->method('find')->with(123)->willReturn($this->bug); + $this->helper->recordVideo(123); + } + public function testRecordVideo(): void { $this->stepsRunner @@ -239,6 +249,8 @@ public function testRecordVideo(): void ->method('run') ->with($this->bug->getSteps(), $this->bug, true); $this->bugRepository->expects($this->once())->method('find')->with(123)->willReturn($this->bug); + $this->bugRepository->expects($this->once())->method('startRecording')->with($this->bug); + $this->bugRepository->expects($this->once())->method('stopRecording')->with($this->bug); $this->helper->recordVideo(123); } } diff --git a/tests/Service/Task/TaskHelperTest.php b/tests/Service/Task/TaskHelperTest.php index f8d4b74d..d68de505 100644 --- a/tests/Service/Task/TaskHelperTest.php +++ b/tests/Service/Task/TaskHelperTest.php @@ -5,10 +5,10 @@ use Exception; use PHPUnit\Framework\TestCase; use SingleColorPetrinet\Model\Color; +use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException; use Throwable; use Tienvx\Bundle\MbtBundle\Entity\Bug; use Tienvx\Bundle\MbtBundle\Entity\Task; -use Tienvx\Bundle\MbtBundle\Exception\RuntimeException; use Tienvx\Bundle\MbtBundle\Exception\UnexpectedValueException; use Tienvx\Bundle\MbtBundle\Generator\GeneratorInterface; use Tienvx\Bundle\MbtBundle\Generator\GeneratorManagerInterface; @@ -82,8 +82,8 @@ public function testRunNoTask(): void public function testRunTaskAlreadyRunning(): void { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Can not run task 123: task is already running'); + $this->expectException(RecoverableMessageHandlingException::class); + $this->expectExceptionMessage('Can not run task 123: task is running. Will retry later'); $this->task->setRunning(true); $this->taskRepository->expects($this->once())->method('find')->with(123)->willReturn($this->task); $this->taskHelper->run(123);