Skip to content

Commit 7a2d1aa

Browse files
authored
Fix running all tests from CLI (reuse same loop) (#1647)
1 parent f6f9243 commit 7a2d1aa

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

telescope/app.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,17 @@ def init_app(checks: Checks):
565565
return app
566566

567567

568-
def run_check(check):
568+
def run_check(loop, check, cache, events, force):
569569
cprint(check.description, "white")
570570

571-
_, success, data, _ = asyncio.run(check.run())
572-
573-
cprint(json.dumps(data, indent=2), "green" if success else "red")
571+
try:
572+
_, success, data, _ = loop.run_until_complete(
573+
check.run(cache=cache, events=events, force=force)
574+
)
575+
cprint(json.dumps(data, indent=2), "green" if success else "red")
576+
except Exception as e:
577+
cprint(f"Error running check '{check.project}/{check.name}': {e!r}", "red")
578+
success = False
574579
return success
575580

576581

@@ -580,6 +585,10 @@ def main(argv):
580585

581586
checks = Checks.from_conf(conf)
582587

588+
app = init_app(checks)
589+
cache = app["telescope.cache"]
590+
events = app["telescope.events"]
591+
583592
# If CLI arg is provided, run the check.
584593
if len(argv) >= 1 and argv[0] == "check":
585594
project = None
@@ -588,20 +597,21 @@ def main(argv):
588597
project = argv[1]
589598
if len(argv) > 2:
590599
name = argv[2]
600+
force = "--force" in argv
591601
try:
592602
selected = checks.lookup(project=project, name=name)
593603
except ValueError as e:
594604
cprint(f"{e} in '{config.CONFIG_FILE}'", "red")
595605
return 2
596606

607+
loop = asyncio.get_event_loop()
597608
successes = []
598609
for check in selected:
599-
success = run_check(check)
610+
success = run_check(loop, check, cache, events, force=force)
600611
successes.append(success)
601612

602613
return 0 if all(successes) else 1
603614

604615
# Otherwise, run the Web app.
605-
app = init_app(checks)
606616
logger.debug(f"Running at http://{config.HOST}:{config.PORT}")
607617
web.run_app(app, host=config.HOST, port=config.PORT, print=False)

tests/test_main.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import sys
23
from unittest import mock
34

@@ -37,11 +38,37 @@ def test_run_check(mock_aioresponses):
3738
mock_aioresponses.get(url, status=200, payload={"ok": True})
3839

3940
assert run_check(
40-
Check(
41+
loop=asyncio.get_event_loop(),
42+
check=Check(
4143
project="a-project",
4244
name="a-name",
4345
description="My heartbeat",
4446
module="checks.core.heartbeat",
4547
params={"url": url},
48+
),
49+
cache=None,
50+
events=None,
51+
force=False,
52+
)
53+
54+
55+
def test_run_failing_check(mock_aioresponses):
56+
url = "http://server.local/__heartbeat__"
57+
mock_aioresponses.get(url, exception=RuntimeError("Weird error"))
58+
59+
assert (
60+
run_check(
61+
loop=asyncio.get_event_loop(),
62+
check=Check(
63+
project="a-project",
64+
name="a-name",
65+
description="My heartbeat",
66+
module="checks.core.heartbeat",
67+
params={"url": url},
68+
),
69+
cache=None,
70+
events=None,
71+
force=False,
4672
)
73+
is False
4774
)

0 commit comments

Comments
 (0)