You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/AdministratorTelegramRateGa...

109 lines
3.7 KiB
PHTML

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\Configurations;
use App\Services\Telegram\AdministratorTelegramDestination;
use App\Services\Telegram\AdministratorTelegramLocalUnavailable;
use App\Services\Telegram\AdministratorTelegramRateBacklog;
use App\Services\Telegram\DatabaseAdministratorTelegramRateGate;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
Configurations::setConnectionResolver($capsule->getDatabaseManager());
$db = $capsule->getConnection('default');
$db->getSchemaBuilder()->create(
'administrator_telegram_delivery_gate',
static function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('next_send_at');
$table->integer('updated_at');
}
);
$db->table('administrator_telegram_delivery_gate')->insert([
'id' => 1, 'next_send_at' => 0, 'updated_at' => 0,
]);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$destination = new AdministratorTelegramDestination(
1, '123456', str_repeat('a', 32)
);
$now = 2000;
$sleeps = [];
$gate = new DatabaseAdministratorTelegramRateGate(
static function () use (&$now): int { return $now; },
static function (int $microseconds) use (&$sleeps, $db): void {
if ($db->transactionLevel() !== 0) {
throw new RuntimeException('Rate wait occurred inside transaction');
}
$sleeps[] = $microseconds;
}
);
$gate->acquire($destination);
$assert($sleeps === []
&& (int) $db->table('administrator_telegram_delivery_gate')
->value('next_send_at') === 2002,
'First Telegram permit was not reserved immediately.');
$gate->acquire($destination);
$assert($sleeps === [2000000]
&& (int) $db->table('administrator_telegram_delivery_gate')
->value('next_send_at') === 2004,
'Second Telegram permit did not reserve and wait for the adjacent slot.');
$backlog = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$backlog = $error;
}
$assert($backlog instanceof AdministratorTelegramRateBacklog
&& count($sleeps) === 1,
'Third concurrent permit was not classified as known-no-remote backlog.');
// A stale future wall-clock value is quarantined and rewritten. The current
// request receives no permit, while later callers can recover automatically.
$now = 3000;
$db->table('administrator_telegram_delivery_gate')->where('id', 1)->update([
'next_send_at' => 9002, 'updated_at' => 9000,
]);
$clockFailure = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$clockFailure = $error;
}
$recovered = $db->table('administrator_telegram_delivery_gate')
->where('id', 1)->first();
$assert($clockFailure instanceof AdministratorTelegramLocalUnavailable
&& (int) $recovered->updated_at === 3000
&& (int) $recovered->next_send_at === 3004,
'Future Telegram gate timestamp was not safely quarantined.');
$now = 3002;
$gate->acquire($destination);
$assert(end($sleeps) === 2000000,
'Recovered Telegram gate did not resume through a bounded slot.');
$db->getSchemaBuilder()->drop('administrator_telegram_delivery_gate');
$missing = null;
try {
$gate->acquire($destination);
} catch (Throwable $error) {
$missing = $error;
}
$assert($missing instanceof AdministratorTelegramLocalUnavailable,
'Missing shared Telegram gate was not a known local failure.');
echo "Administrator Telegram rate gate tests passed.\n";