Files
XC_VM/tests/Unit/CommandBusDedupeTest.php
T
Divarion_D 9e118da975 test: run the unit suite on MariaDB only, and on the panel's own stack
The suite ran on in-memory SQLite, an engine production never uses: it hid
column lengths, MySQL-only syntax and type coercion (the identity bug fixed in
the previous commit passed CI that way). SQLite is gone:

- TestDb connects only to MariaDB/MySQL (XCVM_TEST_DB_DSN/_USER/_PASS; root over
  the local socket, else 127.0.0.1:3306). Each instance gets a throwaway
  schema, dropped with it, in the installer's sql_mode, with an explicit
  connect timeout (a boot under test can zero default_socket_timeout).
- InstallSchema and the tests take the production DDL as is: the SQLite
  reducers, the keys they re-added, the faked information_schema and the
  sqliteCreateFunction() stand-ins are removed; test DDL is MySQL DDL.
- ReplicaBootTest, DbConnectRefusalTest and LazyDatabaseHandlerTest reach the
  test server instead of SQLite files; RecordingFinalizer no longer swallows
  an information_schema failure "for tests on SQLite".
- `make test-db` starts a throwaway mariadb:11.4 (tmpfs) for a machine without
  a server; the CI PHPUnit job gets the same as a service.

The suite also runs on a panel host with the bundled PHP and its own php.ini:
tests reach the code through MAIN_HOME, not `<repo>/src`; repository checks
and process-isolated tests (ionCube Loader + OPcache CLI segfault on a script
read from stdin) are in the skip-on-panel group; xcvm_test_child_php() starts
a child that fakes \XC_VM without the real extension; Redis-down tests use
RedisManager::useConnector() instead of an eval'd \XC_VM.

Stale opt-in tests fixed on the way: the real-agent provisioning test answers
the agent-started check, the extension integration test sends the typed starts
and reads the binary signature raw, a fake producer starts as PHP_BINARY, and
the certbot test waits for the line its background command writes.
2026-10-04 16:12:23 +03:00

68 lines
3.2 KiB
PHP

<?php
use PHPUnit\Framework\TestCase;
use XcVm\Domain\Cluster\ClusterBus;
use XcVm\Domain\Cluster\ClusterClock;
use XcVm\Domain\Cluster\CommandBus;
use XcVm\Domain\Cluster\NodeRegistry;
use XcVm\Infrastructure\Database\DatabaseFactory;
use XcVm\Tests\Support\FakeClusterCrypto;
/**
* A command's `dedupe_key` names one desired state per node
* (`UNIQUE(server_id, dedupe_key)`, migration 030): a newer command with
* the key replaces one not yet acked. An acked command keeps its outcome
* for a day (CommandBus::result), and must not keep its key: the next
* command with that key (the next `config.changed`, a drop of the same
* viewer) is queued, not refused by the unique key.
*/
final class CommandBusDedupeTest extends TestCase {
private const SID = 5;
private TestDb $rDb;
private FakeClusterCrypto $rCrypto;
protected function setUp(): void {
$this->rDb = new TestDb();
foreach (['029_create_cluster_nodes', '030_create_cluster_commands'] as $rName) {
$rSql = (string) file_get_contents(MAIN_HOME . 'migrations/database/up/' . $rName . '.sql');
$this->rDb->exec((string) $rSql);
}
DatabaseFactory::set($this->rDb);
ClusterClock::fix(1800000000000);
ClusterBus::useSocket(sys_get_temp_dir() . '/no-such-bus-' . bin2hex(random_bytes(4)) . '/cluster.sock');
$this->rCrypto = new FakeClusterCrypto();
NodeRegistry::startEnrolment(self::SID, '0f8fad5b-d9cb-469f-a165-70867728950e', random_bytes(32), random_bytes(32), 1);
NodeRegistry::update(self::SID, ['state' => 'active', 'mode' => 1, 'flows' => NodeRegistry::FLOW_COMMANDS]);
}
protected function tearDown(): void {
ClusterBus::useSocket(null);
ClusterClock::fix(null);
DatabaseFactory::reset();
}
/** @return list<array{0: string, 1: string, 2: ?string}> [cmd_id, state, dedupe_key] by seq */
private function rows(): array {
$this->rDb->query('SELECT `cmd_id`, `state`, `dedupe_key` FROM `cluster_commands` ORDER BY `seq`');
return array_map(static fn(array $rRow): array => [(string) $rRow['cmd_id'], (string) $rRow['state'], $rRow['dedupe_key']], $this->rDb->get_rows());
}
public function testAnAckedCommandGivesUpItsKey(): void {
$rFirst = CommandBus::enqueue($this->rCrypto, self::SID, 'config.changed', ['sections' => ['servers']], 'config.changed');
$this->assertTrue(CommandBus::ack(self::SID, $rFirst, true, 'done'));
$rSecond = CommandBus::enqueue($this->rCrypto, self::SID, 'config.changed', ['sections' => ['servers']], 'config.changed');
$this->assertSame([[$rFirst, 'acked', null], [$rSecond, 'queued', 'config.changed']], $this->rows());
$this->assertSame([true, 'done'], CommandBus::result($rFirst), 'the acked one keeps its outcome');
// A newer desired state still replaces one not acked yet.
$rThird = CommandBus::enqueue($this->rCrypto, self::SID, 'config.changed', ['sections' => ['servers']], 'config.changed');
$this->assertSame([[$rFirst, 'acked', null], [$rThird, 'queued', 'config.changed']], $this->rows());
// And a failed one goes, as before.
$this->assertTrue(CommandBus::ack(self::SID, $rThird, false, 'no'));
$rFourth = CommandBus::enqueue($this->rCrypto, self::SID, 'conn.drop', ['uuid' => 'v1'], 'config.changed');
$this->assertSame([[$rFirst, 'acked', null], [$rFourth, 'queued', 'config.changed']], $this->rows());
}
}