Skip to content

Commit d73035e

Browse files
committed
Improve unit tests for the service quality oracle core module
1 parent f55d801 commit d73035e

File tree

3 files changed

+207
-168
lines changed

3 files changed

+207
-168
lines changed

src/models/service_quality_oracle.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,18 @@ def main(run_date_override: date = None):
117117
logger.info(f"Oracle run completed successfully in {execution_time:.2f} seconds")
118118

119119
if slack_notifier:
120-
batch_count = len(transaction_links) if transaction_links else 0
121-
total_processed = len(eligible_indexers)
122-
slack_notifier.send_success_notification(
123-
eligible_indexers=eligible_indexers,
124-
total_processed=total_processed,
125-
execution_time=execution_time,
126-
transaction_links=transaction_links,
127-
batch_count=batch_count,
128-
)
120+
try:
121+
batch_count = len(transaction_links) if transaction_links else 0
122+
total_processed = len(eligible_indexers)
123+
slack_notifier.send_success_notification(
124+
eligible_indexers=eligible_indexers,
125+
total_processed=total_processed,
126+
execution_time=execution_time,
127+
transaction_links=transaction_links,
128+
batch_count=batch_count,
129+
)
130+
except Exception as e:
131+
logger.error(f"Failed to send Slack success notification: {e}", exc_info=True)
129132

130133
except Exception as e:
131134
execution_time = time.time() - start_time

src/utils/slack_notifier.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ def __init__(self, webhook_url: str) -> None:
3030

3131

3232
@retry_with_backoff(
33-
max_attempts=8, min_wait=1, max_wait=128, exceptions=(requests.exceptions.RequestException,)
33+
max_attempts=8,
34+
min_wait=1,
35+
max_wait=128,
36+
exceptions=(requests.exceptions.RequestException,),
37+
reraise=True,
3438
)
3539
def _send_message(self, payload: Dict) -> bool:
3640
"""
@@ -40,37 +44,28 @@ def _send_message(self, payload: Dict) -> bool:
4044
payload: The message payload to send
4145
4246
Returns:
43-
bool: True if message was sent successfully, False otherwise
47+
bool: True if message was sent successfully.
48+
Raises:
49+
requests.exceptions.RequestException: If the request fails after all retries.
4450
"""
4551
# Get the message type from the payload
4652
message_type = payload.get("text", "Unknown")
4753

4854
# Log the message type
4955
logger.info(f"Sending Slack notification: {message_type}")
5056

51-
try:
52-
response = requests.post(
53-
self.webhook_url,
54-
json=payload,
55-
timeout=self.timeout,
56-
headers={"Content-Type": "application/json"},
57-
)
58-
59-
# If the message is sent successfully, return True
60-
if response.status_code == 200:
61-
logger.info("Slack notification sent successfully")
62-
return True
63-
else:
64-
# log message failure
65-
logger.warning(f"Slack notification failed: {response.status_code}")
66-
# Raise an exception to trigger retry
67-
response.raise_for_status()
68-
return False
69-
70-
# If there is an error when trying to send the message, log the error and re-raise
71-
except requests.exceptions.RequestException as e:
72-
logger.warning(f"Slack notification failed: {str(e)}")
73-
raise
57+
response = requests.post(
58+
self.webhook_url,
59+
json=payload,
60+
timeout=self.timeout,
61+
headers={"Content-Type": "application/json"},
62+
)
63+
# Raise an exception for 4xx/5xx responses
64+
response.raise_for_status()
65+
66+
# If the message is sent successfully, return True
67+
logger.info("Slack notification sent successfully")
68+
return True
7469

7570

7671
def _create_payload(self, text: str, fields: List[Dict], color: str = "good") -> Dict:

0 commit comments

Comments
 (0)