-
Notifications
You must be signed in to change notification settings - Fork 86
Run health alert checks concurrently #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
silentgeckoaudit3801
wants to merge
7
commits into
Quantarq:main
Choose a base branch
from
silentgeckoaudit3801:perf/concurrent-health-alerts-206
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e695ea
perf: check health alerts concurrently
silentgeckoaudit3801 f7dd1ca
test: cover concurrent alert sweep failures
silentgeckoaudit3801 f844fa8
style: wrap alert sweep lines
silentgeckoaudit3801 6156745
style: wrap alert mixin tests
silentgeckoaudit3801 0098d05
test: rewrite alert mixin tests cleanly
silentgeckoaudit3801 9b8325f
style: keep alert tests within line length
silentgeckoaudit3801 eaa736c
Use unittest assertion in alert concurrency test
silentgeckoaudit3801 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import asyncio | ||
| from contextlib import ExitStack | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from web_app.contract_tools.mixins.alert import AlertMixin | ||
|
|
||
| USER_CONNECTOR = "web_app.contract_tools.mixins.alert.UserDBConnector" | ||
| STELLAR_CLIENT = "web_app.contract_tools.mixins.alert.get_stellar_client" | ||
| HEALTH_RATIO = ( | ||
| "web_app.contract_tools.mixins.alert." | ||
| "HealthRatioMixin.get_health_ratio_and_tvl" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_alert_sweep_continues_after_user_failure(): | ||
| users = [("contract-ok", 1001), ("contract-fail", 1002), ("contract-low", 1003)] | ||
|
|
||
| async def fake_health_ratio(contract_address, client): | ||
| if contract_address == "contract-fail": | ||
| raise RuntimeError("rpc failed") | ||
| if contract_address == "contract-low": | ||
| return "1.0", "100" | ||
| return "1.5", "100" | ||
|
|
||
| with ExitStack() as stack: | ||
| connector = stack.enter_context(patch(USER_CONNECTOR)) | ||
| stack.enter_context(patch(STELLAR_CLIENT, return_value=MagicMock())) | ||
| stack.enter_context(patch(HEALTH_RATIO, side_effect=fake_health_ratio)) | ||
| send_notification = stack.enter_context( | ||
| patch.object(AlertMixin, "send_notification", new_callable=AsyncMock) | ||
| ) | ||
| connector.return_value.get_users_for_notifications.return_value = users | ||
|
|
||
| await AlertMixin.check_users_health_ratio_level() | ||
|
|
||
| send_notification.assert_awaited_once_with(1003, "1.0") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_alert_sweep_runs_health_checks_concurrently(): | ||
| users = [(f"contract-{idx}", idx) for idx in range(4)] | ||
| started = 0 | ||
| peak_started = 0 | ||
| release = asyncio.Event() | ||
|
|
||
| async def fake_health_ratio(contract_address, client): | ||
| nonlocal started, peak_started | ||
| started += 1 | ||
| peak_started = max(peak_started, started) | ||
| if started == len(users): | ||
| release.set() | ||
| await release.wait() | ||
| return "1.5", "100" | ||
|
|
||
| with ExitStack() as stack: | ||
| stack.enter_context( | ||
| patch( | ||
| "web_app.contract_tools.mixins.alert.ALERT_BATCH_CONCURRENCY", | ||
| len(users), | ||
| ) | ||
| ) | ||
| connector = stack.enter_context(patch(USER_CONNECTOR)) | ||
| stack.enter_context(patch(STELLAR_CLIENT, return_value=MagicMock())) | ||
| stack.enter_context(patch(HEALTH_RATIO, side_effect=fake_health_ratio)) | ||
| stack.enter_context( | ||
| patch.object(AlertMixin, "send_notification", new_callable=AsyncMock) | ||
| ) | ||
| connector.return_value.get_users_for_notifications.return_value = users | ||
|
|
||
| await asyncio.wait_for(AlertMixin.check_users_health_ratio_level(), timeout=1) | ||
|
|
||
| assert peak_started == len(users) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_single_user_timeout_does_not_stop_successes(): | ||
| users = [("contract-timeout", 1001), ("contract-ok", 1002)] | ||
|
|
||
| async def fake_health_ratio(contract_address, client): | ||
| if contract_address == "contract-timeout": | ||
| await asyncio.sleep(1) | ||
| return "1.5", "100" | ||
|
|
||
| with ExitStack() as stack: | ||
| stack.enter_context( | ||
| patch( | ||
| "web_app.contract_tools.mixins.alert.ALERT_USER_TIMEOUT_SECONDS", | ||
| 0.01, | ||
| ) | ||
| ) | ||
| connector = stack.enter_context(patch(USER_CONNECTOR)) | ||
| stack.enter_context(patch(STELLAR_CLIENT, return_value=MagicMock())) | ||
| stack.enter_context(patch(HEALTH_RATIO, side_effect=fake_health_ratio)) | ||
| stack.enter_context( | ||
| patch.object(AlertMixin, "send_notification", new_callable=AsyncMock) | ||
| ) | ||
| connector.return_value.get_users_for_notifications.return_value = users | ||
|
|
||
| await AlertMixin.check_users_health_ratio_level() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.