Skip to content

Commit

Permalink
#16 added tests; moved worker call to separate file for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
almostengr committed Jun 1, 2024
1 parent 2d0d277 commit f7be805
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 23 deletions.
1 change: 1 addition & 0 deletions ShowPulseBase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App;
use Exception;

$testing = false;
$commonFile = $testing ? "/opt/fpp/www/common.php" : __DIR__ . "/tests/OptFppWwwCommonMock.php";
Expand Down
25 changes: 25 additions & 0 deletions ShowPulseDaemon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App;

use Exception;

require_once "ShowPulseWorker.php";

$worker = new ShowPulseWorker();
while (true) {
try {
$worker->getWebsiteApiKey();
$worker->getFppStatus();
$worker->postShowStatus();
$worker->getNextRequest();
$sleepTime = $worker->calculateSleepTime();
sleep($sleepTime);
$worker->resetAttemptCount();
} catch (Exception $e) {
$worker->logError($e->getMessage());
$sleepTime = $worker->exponentialBackoffSleep();
$worker->increaseAttemptCount();
sleep($sleepTime);
}
}
38 changes: 16 additions & 22 deletions ShowPulseWorker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace App;

use Exception;

require_once "ShowPulseBase.php";
Expand Down Expand Up @@ -48,6 +49,11 @@ public function __construct()
$this->nextJukeboxRequest = null;
}

public function getAttemptCount()
{
return $this->attemptCount;
}

public function getFppStatus()
{
$url = $this->fppUrl("fppd/status");
Expand Down Expand Up @@ -82,8 +88,7 @@ public function exponentialBackoffSleep()
{
$defaultDelay = 2;
$maxDelay = 15;
$delay = min(pow(2, $this->attemptCount) * $defaultDelay, $maxDelay);
sleep($delay);
return min(pow(2, $this->attemptCount) * $defaultDelay, $maxDelay);
}

public function resetAttemptCount()
Expand All @@ -93,7 +98,9 @@ public function resetAttemptCount()

public function increaseAttemptCount()
{
$this->attemptCount = $this->attemptCount < 5 ? $this->attemptCount++ : $this->attemptCount;
if ($this->attemptCount < 5) {
$this->attemptCount++;
}
}

public function sleepShortValue()
Expand Down Expand Up @@ -180,25 +187,12 @@ public function postShowStatus()
$this->lastSequence = $this->fppStatus->current_sequence;
}

public function sleepDelay()
public function calculateSleepTime()
{
$seconds = $this->fppStatus->status_name === "idle" ? $this->sleepLongValue() : $this->sleepShortValue();
sleep($seconds);
}
}
if (is_null($this->fppStatus)) {
return $this->sleepShortValue();
}

$worker = new ShowPulseWorker();
while ($testing) {
try {
$worker->getWebsiteApiKey();
$worker->getFppStatus();
$worker->postShowStatus();
$worker->getNextRequest();
$worker->sleepDelay();
$worker->resetAttemptCount();
} catch (Exception $e) {
$worker->logError($e->getMessage());
$worker->exponentialBackoffSleep();
$worker->increaseAttemptCount();
return $this->fppStatus->status_name === "idle" ? $this->sleepLongValue() : $this->sleepShortValue();
}
}
}
2 changes: 1 addition & 1 deletion scripts/postStart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

echo "Running Show Pulse PostStart Script"

/usr/bin/php /home/fpp/media/plugins/showpulse/ShowPulseWorker.php &
/usr/bin/php /home/fpp/media/plugins/showpulse/ShowPulseService.php &
65 changes: 65 additions & 0 deletions tests/ShowPulseWorkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Test;

use App\ShowPulseWorker;
use PHPUnit\Framework\TestCase;

/**
* @covers ShowPulseWorker
*/
final class ShowPulseWorkerTest extends TestCase
{
public function testResetAttemptCount()
{
$worker = new ShowPulseWorker();
$worker->resetAttemptCount();

$this->assertEquals(0, $worker->getAttemptCount());
}

public function testIncreaseAttemptCountTwoTimes()
{
$worker = new ShowPulseWorker();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();

$this->assertEquals(2, $worker->getAttemptCount());
}

public function testIncreaseAttemptCountSixTimes()
{
$worker = new ShowPulseWorker();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();
$worker->increaseAttemptCount();

$this->assertEquals(5, $worker->getAttemptCount());
}

public function testSleepShortValue()
{
$worker = new ShowPulseWorker();

$this->assertEquals(5, $worker->sleepShortValue());
}

public function testSleepLongValue()
{
$worker = new ShowPulseWorker();

$this->assertEquals(30, $worker->sleepLongValue());
}

public function testCalculateSleepTime()
{
$worker = new ShowPulseWorker();

$sleepTime = $worker->calculateSleepTime();

$this->assertEquals(5, $sleepTime);
}
}

0 comments on commit f7be805

Please sign in to comment.