diff --git a/coco/test/coco_runner.py b/coco/test/coco_runner.py index 0f782e4..058dbf3 100644 --- a/coco/test/coco_runner.py +++ b/coco/test/coco_runner.py @@ -47,7 +47,9 @@ class Runner: """Coco Runner for unit tests.""" - def __init__(self, config, endpoints, reset_on_start=False, reset_on_shutdown=True): + def __init__( + self, config, endpoints, reset_on_start=False, reset_on_shutdown=False + ): self.reset_on_shutdown = reset_on_shutdown self.start_coco(config, endpoints, reset_on_start) time.sleep(1) @@ -73,7 +75,7 @@ def client(self, command, data=None, silent=False): try: result = subprocess.check_output(cmd, encoding="utf-8") except subprocess.CalledProcessError as e: - print(f"coco client errored: {e}") + print(f"coco client errored: {e.returncode}, {e.output}") return None if not silent: @@ -84,6 +86,7 @@ def client(self, command, data=None, silent=False): result = json.loads(result) except json.JSONDecodeError as err: print(f"Failure parsing json returned by client: {err}.\n{result}") + print(err) return None return result diff --git a/tests/test_queue.py b/tests/test_queue.py index b084d66..ea523de 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -3,6 +3,7 @@ from aiohttp import request import requests import asyncio +import time from prometheus_client.parser import text_string_to_metric_families from coco.test import coco_runner @@ -33,7 +34,7 @@ def callback(data): N_HOSTS = 2 -CALLBACKS = {edpt: callback for edpt in ENDPOINTS} +CALLBACKS = {edpt: callback for edpt in ENDPOINTS.keys()} @pytest.fixture @@ -46,9 +47,21 @@ def farm(): def runner(farm): """Create a coco runner.""" CONFIG["groups"] = {"test": farm.hosts} - with coco_runner.Runner(CONFIG, ENDPOINTS) as runner: + with coco_runner.Runner(CONFIG, ENDPOINTS, reset_on_shutdown=False) as runner: yield runner +@pytest.fixture +def loop(): + """Use a separate event loop.""" + orig_loop = asyncio.get_event_loop() + try: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + yield loop + loop.close() + finally: + asyncio.set_event_loop(orig_loop) + async def _client(config, endpoint, sleep=None): if sleep: @@ -59,19 +72,19 @@ async def _client(config, endpoint, sleep=None): return await r.json() -def test_queue(farm, runner): +def test_queue(farm, runner, loop): """Test queue limit.""" # Wait for coco to start up - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) + time.sleep(3) + # Set up client tasks wait = _client(runner.configfile.name, "do_wait") clients = [] for i in range(QUEUE_LEN + 1): clients.append(_client(runner.configfile.name, "test", sleep=0.1)) + # Send requests replies = loop.run_until_complete(asyncio.gather(wait, *clients)) - loop.close() # Check responses failed = 0 diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py index 8cf46bb..4adbdc1 100644 --- a/tests/test_scheduler.py +++ b/tests/test_scheduler.py @@ -80,19 +80,16 @@ def runner(farm): print(STATEFILE.name) with coco_runner.Runner(CONFIG, ENDPOINTS, reset_on_start=True) as runner: yield runner - + runner.stop_coco() def test_sched(farm, runner): """Test if scheduled endpoints are called when they should be.""" start_t = time.time() # Let three periods pass - time.sleep(3 * PERIOD + 0.5) - + time.sleep(3 * PERIOD + 1.4) counters = farm.counters() end_t = time.time() - num_sched = (end_t - start_t) // PERIOD - for p in farm.ports: assert counters[p]["scheduled"] == num_sched assert counters[p]["scheduled-check-type"] == num_sched @@ -100,4 +97,3 @@ def test_sched(farm, runner): assert "scheduled-fail-type" not in counters[p] assert "scheduled-fail-val" not in counters[p] - runner.stop_coco()