mirror of
https://github.com/Vateron-Media/XC_VM.git
synced 2026-10-25 11:03:14 +01:00
A node records the runtime state of the streams it runs (pids, status, probe results, progress, current source) by updating its streams_servers row; ~30 places did that with their own UPDATE. They now call StreamStateWriter::update(stream, server, fields) or updateRow(id, fields), which accept only the runtime-state columns (STATE_FIELDS) and write through a sink. The SQL sink issues the same UPDATE as before (StreamStateWriterTest); the cluster API plan (Phase 5) adds one that sends a stream.state event for MAIN to merge instead. Desired state (what runs where, parents, on-demand, the panel's edits) stays MAIN's and is untouched. Phase 0 seam. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Es8UhyPS8MNjuYTagqfGWi
73 lines
2.7 KiB
PHP
73 lines
2.7 KiB
PHP
<?php
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use XcVm\Domain\Stream\StreamStateWriter;
|
|
|
|
/**
|
|
* A node's runtime state for a stream (pids, status, probe results, progress)
|
|
* is written through StreamStateWriter; desired state stays MAIN's. The SQL
|
|
* sink writes the same UPDATE the ~30 call sites wrote, and any column
|
|
* outside the runtime-state list is refused.
|
|
*/
|
|
final class StreamStateWriterTest extends TestCase {
|
|
|
|
private function recorder(): object {
|
|
return new class {
|
|
/** @var list<array{string, list<mixed>}> */
|
|
public array $rQueries = [];
|
|
public function query(string $rSql, mixed ...$rParams): bool {
|
|
$this->rQueries[] = [$rSql, $rParams];
|
|
return true;
|
|
}
|
|
};
|
|
}
|
|
|
|
protected function tearDown(): void {
|
|
StreamStateWriter::useSink(null);
|
|
}
|
|
|
|
public function testUpdateByStreamAndServer(): void {
|
|
$rDb = $this->recorder();
|
|
StreamStateWriter::update(12, 3, ['pid' => 4242, 'stream_status' => 0, 'stream_info' => null], $rDb);
|
|
$this->assertSame([[
|
|
'UPDATE `streams_servers` SET `pid` = ?, `stream_status` = ?, `stream_info` = ? WHERE `stream_id` = ? AND `server_id` = ?',
|
|
[4242, 0, null, 12, 3],
|
|
]], $rDb->rQueries);
|
|
}
|
|
|
|
public function testUpdateByRowId(): void {
|
|
$rDb = $this->recorder();
|
|
StreamStateWriter::updateRow(77, ['progress_info' => ''], $rDb);
|
|
$this->assertSame([['UPDATE `streams_servers` SET `progress_info` = ? WHERE `server_stream_id` = ?', ['', 77]]], $rDb->rQueries);
|
|
StreamStateWriter::updateRow(77, [], $rDb);
|
|
$this->assertCount(1, $rDb->rQueries, 'nothing to write, no query');
|
|
}
|
|
|
|
public function testDesiredStateColumnsAreRefused(): void {
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('parent_id');
|
|
StreamStateWriter::update(1, 1, ['pid' => 1, 'parent_id' => 5], $this->recorder());
|
|
}
|
|
|
|
public function testSinkReceivesTheIntent(): void {
|
|
$rSeen = [];
|
|
StreamStateWriter::useSink(function (string $rWhere, array $rFields, array $rKeys) use (&$rSeen): bool {
|
|
$rSeen[] = [$rWhere, $rFields, $rKeys];
|
|
return true;
|
|
});
|
|
StreamStateWriter::update(5, 2, ['monitor_pid' => 9]);
|
|
$this->assertSame([['`stream_id` = ? AND `server_id` = ?', ['monitor_pid' => 9], [5, 2]]], $rSeen);
|
|
}
|
|
|
|
public function testRuntimeWritersUseTheSeam(): void {
|
|
$rRoot = dirname(__DIR__, 2) . '/src/';
|
|
foreach ([
|
|
'Domain/Stream/StreamProcess.php', 'Cli/Commands/MonitorCommand.php', 'Cli/CronJobs/StreamsCronJob.php',
|
|
'Cli/CronJobs/VodCronJob.php', 'Cli/CronJobs/CleanupCronJob.php', 'Cli/Commands/CreatedCommand.php',
|
|
'Cli/Commands/DelayCommand.php', 'Cli/Commands/ScannerCommand.php',
|
|
] as $rFile) {
|
|
$this->assertStringNotContainsString('UPDATE `streams_servers` SET', (string) file_get_contents($rRoot . $rFile), $rFile);
|
|
}
|
|
}
|
|
}
|