Files
XC_VM/tests/Unit/ConnectionTrackerReadConnectionsTest.php
T
Divarion_D e77698580c fix(reseller): guard the live-connections Redis batch read
Last of the three items Rosmi720/XC_VM@8fa1e67f left over, deferred in
78d7b232 because the CRAP ratchet refuses hardening an uncovered cc=53
method. Extracting the guard instead of inlining it keeps the sniff happy
and gives the batch read a home the other six call sites can move to.

ResellerTableRenderer::handleLiveConnections iterated
RedisManager::instance()->mGet($rKeys) directly. RedisManager::instance()
returns null when Redis is unreachable, which makes that a fatal; mGet
answers false on a dropped connection and the client itself while
pipelining, which makes the foreach a warning; and a reseller with no
lines sends an empty key batch. Admin\TableController already carries the
guard — this moves it onto ConnectionTracker::readConnections(), which
already owns the LINE# reads two lines above.

Also swap the `array_multisort` order-column test to !empty(): the client
picks the column by index, so it can name the Responsive control column
(index 0, `false`) or one past the end of the map, and array_multisort on
an undefined key is a TypeError.

Verified: 869 tests, make gates, CRAP gate, PHPStan.

Refs: #148
2026-09-22 22:28:46 +03:00

45 lines
1.6 KiB
PHP

<?php
use PHPUnit\Framework\TestCase;
use XcVm\Domain\Stream\ConnectionTracker;
/**
* ConnectionTracker::readConnections — the guard every mGet batch read goes
* through. A dropped connection or an unreachable Redis must look like "no
* connections", not like a warning or a fatal in the caller's foreach.
*/
final class ConnectionTrackerReadConnectionsTest extends TestCase {
public function testUnreachableRedisReadsAsNoConnections(): void {
$this->assertSame([], ConnectionTracker::readConnections(null, ['LINE#1', 'LINE#2']));
}
public function testEmptyBatchNeverReachesRedis(): void {
// A reseller with no lines produces no keys; phpredis is not asked at all,
// so a null connection is fine here too.
$this->assertSame([], ConnectionTracker::readConnections(null, []));
}
public function testDroppedConnectionReadsAsNoConnections(): void {
if (!extension_loaded('redis')) {
$this->markTestSkipped('phpredis is not loaded; the mGet branch needs a \Redis double.');
}
$rRedis = $this->createMock(\Redis::class);
$rRedis->method('mGet')->willReturn(false);
$this->assertSame([], ConnectionTracker::readConnections($rRedis, ['LINE#1']));
}
public function testPayloadsArePassedThroughUntouched(): void {
if (!extension_loaded('redis')) {
$this->markTestSkipped('phpredis is not loaded; the mGet branch needs a \Redis double.');
}
$rRedis = $this->createMock(\Redis::class);
$rRedis->method('mGet')->willReturn(['payload-a', 'payload-b']);
$this->assertSame(['payload-a', 'payload-b'], ConnectionTracker::readConnections($rRedis, ['LINE#1', 'LINE#2']));
}
}