4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-26 19:07:05 +00:00

Remove "look-ahead" timing on Next Song; add per-x-minutes unit test.

This commit is contained in:
Buster Neece 2019-07-20 16:04:42 -05:00
parent 62069a16b6
commit 3812f68069
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
3 changed files with 47 additions and 24 deletions

View File

@ -667,6 +667,14 @@ class StationPlaylist
return $this->played_at;
}
/**
* @param int $played_at
*/
public function setPlayedAt(int $played_at): void
{
$this->played_at = $played_at;
}
public function played(): void
{
$this->played_at = time();
@ -746,7 +754,7 @@ class StationPlaylist
* @param Chronos $now
* @return bool
*/
public function shouldPlayNowScheduled(Chronos $now): bool
protected function shouldPlayNowScheduled(Chronos $now): bool
{
$day_to_check = (int)$now->format('N');
$current_timecode = (int)$now->format('Hi');
@ -800,7 +808,7 @@ class StationPlaylist
* @param Chronos $now
* @return bool
*/
public function shouldPlayNowPerMinute(Chronos $now): bool
protected function shouldPlayNowPerMinute(Chronos $now): bool
{
return !$this->wasPlayedInLastXMinutes($now, $this->getPlayPerMinutes());
}
@ -809,7 +817,7 @@ class StationPlaylist
* @param Chronos $now
* @return bool
*/
public function shouldPlayNowPerHour(Chronos $now): bool
protected function shouldPlayNowPerHour(Chronos $now): bool
{
$current_minute = (int)$now->minute;
$target_minute = $this->getPlayPerHourMinute();
@ -835,7 +843,7 @@ class StationPlaylist
* @param Chronos $now
* @return bool
*/
public function shouldPlayNowOnce(Chronos $now): bool
protected function shouldPlayNowOnce(Chronos $now): bool
{
if (!$this->isScheduledToPlayToday((int)$now->format('N'))) {
return false;
@ -857,7 +865,7 @@ class StationPlaylist
* @param int $length
* @return bool
*/
public function wasPlayedRecently(array $songHistoryEntries = [], $length = 15): bool
protected function wasPlayedRecently(array $songHistoryEntries = [], $length = 15): bool
{
if (empty($songHistoryEntries)) {
return true;
@ -878,7 +886,7 @@ class StationPlaylist
return $was_played;
}
public function wasPlayedInLastXMinutes(Chronos $now, int $minutes): bool
protected function wasPlayedInLastXMinutes(Chronos $now, int $minutes): bool
{
if (0 === $this->played_at) {
return false;

View File

@ -249,16 +249,6 @@ class AutoDJ implements EventSubscriberInterface
->setMaxResults($song_history_count)
->getArrayResult();
// If the currently playing song is known, calculate the "next song" based on what the time will be
// at the *end* of the currently playing song, instead of right now.
if (count($cued_song_history) > 0) {
$most_recent_sh = $cued_song_history[0];
if (0 !== $most_recent_sh['duration']) {
$now = $now->addSeconds($most_recent_sh['duration']);
}
}
// Types of playlists that should play, sorted by priority.
$typesToPlay = [
Entity\StationPlaylist::TYPE_ONCE_PER_HOUR,

View File

@ -31,19 +31,44 @@ class StationPlaylistTest extends \Codeception\Test\Unit
// Playlist SHOULD play Monday evening at 10:30PM.
$test_time = $test_monday->setTime(22, 30);
$this->assertTrue($playlist->shouldPlayNowScheduled($test_time));
$this->assertTrue($playlist->shouldPlayNow($test_time));
// Playlist SHOULD play Thursday morning at 3:00AM.
$test_time = $test_thursday->setTime(3, 0);
$this->assertTrue($playlist->shouldPlayNowScheduled($test_time));
$this->assertTrue($playlist->shouldPlayNow($test_time));
// Playlist SHOULD NOT play Monday morning at 3:00AM.
$test_time = $test_monday->setTime(3, 0);
$this->assertFalse($playlist->shouldPlayNowScheduled($test_time));
$this->assertFalse($playlist->shouldPlayNow($test_time));
// Playlist SHOULD NOT play Thursday evening at 10:30PM.
$test_time = $test_thursday->setTime(22, 30);
$this->assertFalse($playlist->shouldPlayNowScheduled($test_time));
$this->assertFalse($playlist->shouldPlayNow($test_time));
}
public function testOncePerXMinutesPlaylist()
{
/** @var Entity\Station $station */
$station = Mockery::mock(Entity\Station::class);
$playlist = new Entity\StationPlaylist($station);
$playlist->setType(Entity\StationPlaylist::TYPE_ONCE_PER_X_MINUTES);
$playlist->setPlayPerMinutes(30);
$utc = new \DateTimeZone('UTC');
$test_day = \Cake\Chronos\Chronos::create(2018, 1, 15, 0, 0, 0, $utc);
// Last played 20 minutes ago, SHOULD NOT play again.
$last_played = $test_day->addMinutes(0-20);
$playlist->setPlayedAt($last_played->getTimestamp());
$this->assertFalse($playlist->shouldPlayNow($test_day));
// Last played 40 minutes ago, SHOULD play again.
$last_played = $test_day->addMinutes(0-40);
$playlist->setPlayedAt($last_played->getTimestamp());
$this->assertTrue($playlist->shouldPlayNow($test_day));
}
public function testOncePerHourPlaylist()
@ -60,18 +85,18 @@ class StationPlaylistTest extends \Codeception\Test\Unit
// Playlist SHOULD try to play at 11:59 PM.
$test_time = $test_day->setTime(23, 59);
$this->assertTrue($playlist->shouldPlayNowPerHour($test_time));
$this->assertTrue($playlist->shouldPlayNow($test_time));
// Playlist SHOULD try to play at 12:04 PM.
$test_time = $test_day->setTime(12, 4);
$this->assertTrue($playlist->shouldPlayNowPerHour($test_time));
$this->assertTrue($playlist->shouldPlayNow($test_time));
// Playlist SHOULD NOT try to play at 11:49 PM.
$test_time = $test_day->setTime(23, 49);
$this->assertFalse($playlist->shouldPlayNowPerHour($test_time));
$this->assertFalse($playlist->shouldPlayNow($test_time));
// Playlist SHOULD NOT try to play at 12:06 PM.
$test_time = $test_day->setTime(12, 6);
$this->assertFalse($playlist->shouldPlayNowPerHour($test_time));
$this->assertFalse($playlist->shouldPlayNow($test_time));
}
}