34 lines
1.0 KiB
PHP
34 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../backend/php/NiroMqttKeepalive.php';
|
|
|
|
function assertMqttKeepalive(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
fwrite(STDERR, "NiroMqttKeepaliveTest failed: {$message}\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$keepalive = new NiroMqttKeepalive(100, 15);
|
|
for ($now = 101; $now < 115; $now++) {
|
|
assertMqttKeepalive(!$keepalive->isDue($now), 'PINGREQ became due too early');
|
|
}
|
|
assertMqttKeepalive($keepalive->isDue(115),
|
|
'continuous inbound packets postponed the client PINGREQ');
|
|
|
|
$keepalive->markSent(115);
|
|
assertMqttKeepalive(!$keepalive->isDue(129), 'PINGREQ repeated before its interval');
|
|
assertMqttKeepalive($keepalive->isDue(130), 'next PINGREQ was not scheduled');
|
|
|
|
$invalidIntervalRejected = false;
|
|
try {
|
|
new NiroMqttKeepalive(100, 0);
|
|
} catch (InvalidArgumentException) {
|
|
$invalidIntervalRejected = true;
|
|
}
|
|
assertMqttKeepalive($invalidIntervalRejected, 'invalid keepalive interval was accepted');
|
|
|
|
echo "NiroMqttKeepaliveTest passed\n";
|