Skip to content

Commit

Permalink
fix: fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Lanman committed Jan 24, 2023
1 parent e47c83b commit e7d522f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
7 changes: 5 additions & 2 deletions coco/test/coco_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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

Expand Down
25 changes: 19 additions & 6 deletions tests/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,20 @@ 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
assert counters[p]["scheduled-check-val"] == num_sched
assert "scheduled-fail-type" not in counters[p]
assert "scheduled-fail-val" not in counters[p]

runner.stop_coco()

0 comments on commit e7d522f

Please sign in to comment.