From b764eca51272ed2d39600c166b83e538140cc6d8 Mon Sep 17 00:00:00 2001 From: sbgap Date: Fri, 13 Nov 2026 13:32:19 +0100 Subject: [PATCH] test: fix flaky delayed notification timing and add timeout Start the timer before creating the alert, since the delay is scheduled during alert creation. Bound the polling loop to 5 seconds so a broken delay fails instead of hanging, and use timezone-aware UTC timestamps. Co-Authored-By: Claude Opus 5.5 --- tests/test_notification_rule_plugin.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_notification_rule_plugin.py b/tests/test_notification_rule_plugin.py index c24f6143d..d0ab73303 100644 --- a/tests/test_notification_rule_plugin.py +++ b/tests/test_notification_rule_plugin.py @@ -1,7 +1,7 @@ import json import os import unittest -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from alerta.app import create_app, db, plugins from alerta.models.key import ApiKey @@ -102,9 +102,9 @@ def test_delayed_notifications(self): self.assertEqual(delayed_notification_rule['notificationRule']['delayTime'], 1) # new alert should activate notification_rule + start = datetime.now(UTC) data = self.create_api_obj('/alert', self.prod_alert, self.headers) alert_id = data['id'] - start = datetime.now() active_notification_rules = self.create_api_obj('/notificationrules/active', data['alert'], self.headers, 200)['notificationRules'] self.assertIn( notification_rule_id, @@ -115,9 +115,12 @@ def test_delayed_notifications(self): self.assertIn({'rule_id': delayed_notification_rule_id, 'alert_id': alert_id}, map(get_delay_id, delayed_rules)) delayed_data = self.get_api_obj('/notificationdelay/fire', self.headers) self.assertEqual(delayed_data['notifications'], []) + deadline = datetime.now(UTC) + timedelta(seconds=5) while len(delayed_data['notifications']) == 0: + self.assertLess(datetime.now(UTC), deadline, 'Delayed notification did not fire within 5 seconds') delayed_data = self.get_api_obj('/notificationdelay/fire', self.headers) - self.assertGreaterEqual(datetime.now() - start, timedelta(seconds=1)) + + self.assertGreaterEqual(datetime.now(UTC) - start, timedelta(seconds=1)) self.assertIn({'rule_id': delayed_notification_rule_id, 'alert_id': alert_id}, map(get_delay_id, delayed_data['notifications'])) def test_twilio_sms_channel(self):