Skip to content

Commit

Permalink
Merge pull request #87 from Martin1982/issue-86-sql-gone-away
Browse files Browse the repository at this point in the history
Issue 86 sql gone away
  • Loading branch information
Martin1982 authored May 14, 2018
2 parents 257678a + 8b6c06b commit 7aab872
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions Broadcaster/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function __construct(BroadcastStarter $starter, BroadcastManager $broadca
*/
public function applySchedule(): void
{
$this->broadcastManager->keepConnectionAlive();
$this->stopExpiredBroadcasts();
$this->startPlannedBroadcasts();
$this->sendEndSignals();
Expand Down
12 changes: 12 additions & 0 deletions Service/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
27 changes: 27 additions & 0 deletions Tests/Service/BroadcastManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Martin1982\LiveBroadcastBundle\Tests\Service;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\OptimisticLockException;
Expand Down Expand Up @@ -39,6 +40,32 @@ class BroadcastManagerTest extends TestCase
*/
protected $stack;

/**
* Test connection keepalive method
*/
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(EntityManager::class);
$entityManager->expects(self::atLeastOnce())
->method('getConnection')
->willReturn($connection);

$manager = new BroadcastManager($entityManager, $this->stack);
$manager->keepConnectionAlive();
}

/**
* Test getting a broadcast entity by id
*/
Expand Down

0 comments on commit 7aab872

Please sign in to comment.