Files
XC_VM/tests/Unit/SignalsLoopQueryRateTest.php
T
Claude b41f7b72b7 fix(daemons): reload the servers list on a 5-second timer
Once the signals daemon had run its first settings check, it called
ServerRepository::getAll(true) - a SELECT * FROM servers against MAIN - on
every pass of a loop that sleeps 250 ms: up to 240 reads a minute from every
node. The watchdog and the cache handler tied the reload to the settings
interval instead (10-60 s), so their view of the cluster could be a minute
stale.

DaemonTrait::serversRefreshDue()/refreshServers() reload at most every five
seconds, and the three daemons use it: 12 reads a minute, never more than
five seconds stale (SignalsLoopQueryRateTest). Phase 0 of the MAIN <-> LB
API plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Es8UhyPS8MNjuYTagqfGWi
2026-09-25 09:28:03 +00:00

62 lines
1.9 KiB
PHP

<?php
use PHPUnit\Framework\TestCase;
use XcVm\Cli\DaemonTrait;
/**
* The signals daemon re-read `servers` (a SELECT * against MAIN) on every
* pass of its loop once it had run a settings check — four times a second on
* every node. Persistent loops now reload the servers list on a five-second
* timer shared through DaemonTrait.
*/
final class SignalsLoopQueryRateTest extends TestCase {
private function daemon(): object {
return new class {
use DaemonTrait;
public function due(?int $rNow): bool {
return $this->serversRefreshDue($rNow);
}
public function markRefreshed(int $rAt): void {
$this->rServersRefreshedAt = $rAt;
}
};
}
public function testReloadAtMostEveryFiveSeconds(): void {
$rDaemon = $this->daemon();
$this->assertTrue($rDaemon->due(1000), 'first pass loads');
$rDaemon->markRefreshed(1000);
$this->assertFalse($rDaemon->due(1000));
$this->assertFalse($rDaemon->due(1004));
$this->assertTrue($rDaemon->due(1005));
}
public function testAQuarterSecondLoopReadsServersTwelveTimesAMinuteNotTwoHundredForty(): void {
$rDaemon = $this->daemon();
$rReads = 0;
for ($rTick = 0; $rTick < 240; $rTick++) { // one minute at usleep(250000)
$rNow = 1000 + intdiv($rTick, 4);
if ($rDaemon->due($rNow)) {
$rReads++;
$rDaemon->markRefreshed($rNow);
}
}
$this->assertSame(12, $rReads);
}
/** @dataProvider daemons */
public function testDaemonsUseTheTimer(string $rFile): void {
$rSource = (string) file_get_contents(dirname(__DIR__, 2) . '/src/Cli/Commands/' . $rFile);
$this->assertStringContainsString('$this->serversRefreshDue()', $rSource);
$this->assertStringNotContainsString('ServerRepository::getAll(true)', $rSource);
}
/** @return array<string, array{string}> */
public static function daemons(): array {
return ['signals' => ['SignalsCommand.php'], 'watchdog' => ['WatchdogCommand.php'], 'cache handler' => ['CacheHandlerCommand.php']];
}
}