From 2e1baa4c15fadb043b0e83a5c383a611ae68b19d Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Mon, 14 May 2018 12:29:15 +0200 Subject: [PATCH 1/4] Add keepalive method --- Service/BroadcastManager.php | 12 ++++++++++++ Tests/Service/BroadcastManagerTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Service/BroadcastManager.php b/Service/BroadcastManager.php index 63339d0..1d470b4 100644 --- a/Service/BroadcastManager.php +++ b/Service/BroadcastManager.php @@ -47,6 +47,18 @@ public function __construct(EntityManager $entityManager, ChannelApiStack $apiSt $this->apiStack = $apiStack; } + /** + * Keep a remote connection alive + */ + public function keepConnectionAlive(): void + { + $connection = $this->entityManager->getConnection(); + if (!$connection->ping()) { + $connection->close(); + $connection->connect(); + } + } + /** * Get a broadcast by it's id * diff --git a/Tests/Service/BroadcastManagerTest.php b/Tests/Service/BroadcastManagerTest.php index 3e18c64..174d6d6 100644 --- a/Tests/Service/BroadcastManagerTest.php +++ b/Tests/Service/BroadcastManagerTest.php @@ -8,7 +8,9 @@ namespace Martin1982\LiveBroadcastBundle\Tests\Service; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\ORMException; @@ -39,6 +41,29 @@ class BroadcastManagerTest extends TestCase */ protected $stack; + public function testKeepConnectionAlive(): void + { + $connection = $this->createMock(Connection::class); + $connection->expects(self::atLeastOnce()) + ->method('ping') + ->willReturn(false); + $connection->expects(self::atLeastOnce()) + ->method('close') + ->willReturn(true); + $connection->expects(self::atLeastOnce()) + ->method('connect') + ->willReturn(true); + + /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManager $entityManager */ + $entityManager = $this->createMock(EntityManagerInterface::class); + $entityManager->expects(self::atLeastOnce()) + ->method('getConnection') + ->willReturn($connection); + + $manager = new BroadcastManager($entityManager, $this->stack); + $manager->keepConnectionAlive(); + } + /** * Test getting a broadcast entity by id */ From f60322a0e1c18bac9040052836c07d1a50117169 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Mon, 14 May 2018 13:10:22 +0200 Subject: [PATCH 2/4] Use entitymanger instead of the interface in the test class --- Tests/Service/BroadcastManagerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/Service/BroadcastManagerTest.php b/Tests/Service/BroadcastManagerTest.php index 174d6d6..5ff252b 100644 --- a/Tests/Service/BroadcastManagerTest.php +++ b/Tests/Service/BroadcastManagerTest.php @@ -10,7 +10,6 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\ORMException; @@ -55,7 +54,7 @@ public function testKeepConnectionAlive(): void ->willReturn(true); /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManager $entityManager */ - $entityManager = $this->createMock(EntityManagerInterface::class); + $entityManager = $this->createMock(EntityManager::class); $entityManager->expects(self::atLeastOnce()) ->method('getConnection') ->willReturn($connection); From c5a4e2a8f8fe6b2a8c6689b8f39ce02d784dc2fb Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Mon, 14 May 2018 13:14:17 +0200 Subject: [PATCH 3/4] Fix test comment --- Tests/Service/BroadcastManagerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/Service/BroadcastManagerTest.php b/Tests/Service/BroadcastManagerTest.php index 5ff252b..67555c7 100644 --- a/Tests/Service/BroadcastManagerTest.php +++ b/Tests/Service/BroadcastManagerTest.php @@ -40,6 +40,9 @@ class BroadcastManagerTest extends TestCase */ protected $stack; + /** + * Test connection keepalive method + */ public function testKeepConnectionAlive(): void { $connection = $this->createMock(Connection::class); From 8b6c06b5f48a0384ecc377b5f5ee5cab34c48d47 Mon Sep 17 00:00:00 2001 From: Martin de Keijzer Date: Mon, 14 May 2018 13:14:38 +0200 Subject: [PATCH 4/4] Use keepalive in scheduler --- Broadcaster/Scheduler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Broadcaster/Scheduler.php b/Broadcaster/Scheduler.php index 2340bec..0477dff 100644 --- a/Broadcaster/Scheduler.php +++ b/Broadcaster/Scheduler.php @@ -65,6 +65,7 @@ public function __construct(BroadcastStarter $starter, BroadcastManager $broadca */ public function applySchedule(): void { + $this->broadcastManager->keepConnectionAlive(); $this->stopExpiredBroadcasts(); $this->startPlannedBroadcasts(); $this->sendEndSignals();