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/CloudNotificationDeliverySe...

106 lines
3.4 KiB
PHTML

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudNotificationDeliveryService;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$order = [];
$maintenanceCalls = 0;
$compensationCalls = 0;
$abuseCalls = 0;
$service = new CloudNotificationDeliveryService(
static function () use (&$order, &$maintenanceCalls): bool {
$order[] = 'maintenance';
$maintenanceCalls++;
return false;
},
static function () use (&$order, &$compensationCalls): bool {
$order[] = 'compensation';
$compensationCalls++;
return true;
},
static function () use (&$order, &$abuseCalls): bool {
$order[] = 'abuse';
$abuseCalls++;
return false;
}
);
$assert($service->tick() === true
&& $order === ['maintenance', 'compensation', 'abuse']
&& $maintenanceCalls === 1 && $compensationCalls === 1
&& $abuseCalls === 1
&& $service->observationErrors() === [],
'First notification pass was not bounded or did not report work.');
$order = [];
$assert($service->tick() === true
&& $order === ['compensation', 'abuse', 'maintenance']
&& $maintenanceCalls === 2 && $compensationCalls === 2
&& $abuseCalls === 2,
'Notification channel priority did not alternate fairly.');
$order = [];
$assert($service->tick() === true
&& $order === ['abuse', 'maintenance', 'compensation']
&& $maintenanceCalls === 3 && $compensationCalls === 3
&& $abuseCalls === 3,
'Third notification channel did not receive fair first priority.');
$order = [];
$isolated = new CloudNotificationDeliveryService(
static function () use (&$order): bool {
$order[] = 'maintenance';
throw new RuntimeException('expected maintenance failure');
},
static function () use (&$order): bool {
$order[] = 'compensation';
return true;
},
static function () use (&$order): bool {
$order[] = 'abuse';
return true;
}
);
$assert($isolated->tick() === true
&& $order === ['maintenance', 'compensation', 'abuse']
&& $isolated->observationErrors() === [
'maintenance_notification_failed',
], 'A maintenance failure blocked compensation delivery or was hidden.');
$abuseUnavailable = new CloudNotificationDeliveryService(
static fn (): bool => false,
static fn (): bool => false,
static function (): bool {
throw new RuntimeException('expected local Telegram failure');
}
);
$assert($abuseUnavailable->tick() === false
&& $abuseUnavailable->observationErrors() === [
'abuse_alert_notification_failed',
], 'A local Telegram failure was reported as a healthy idle pass.');
$bothFailed = new CloudNotificationDeliveryService(
static function (): bool {
throw new RuntimeException('expected maintenance failure');
},
static function (): bool {
throw new RuntimeException('expected compensation failure');
},
static function (): bool {
throw new RuntimeException('expected abuse alert failure');
}
);
$assert($bothFailed->tick() === false
&& $bothFailed->observationErrors() === [
'maintenance_notification_failed',
'notification_failed',
'abuse_alert_notification_failed',
], 'Multiple notification channel failures were not retained.');
echo "Cloud notification delivery service test passed.\n";