Add spawner progress message

This commit is contained in:
Benoît Seignovert 2024-02-22 17:41:58 +01:00
parent 8574a75e37
commit 77b4c64f88
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B
6 changed files with 186 additions and 4 deletions

View file

@ -1,17 +1,20 @@
"""Test GLiCID spawner module."""
from collections import namedtuple
from itertools import repeat
import glicid_spawner.spawner
import pytest
from batchspawner import SlurmSpawner
from glicid_spawner import GlicidSpawner
from glicid_spawner.spawner import asyncio
User = namedtuple('User', 'name')
def test_spawner_config():
"""Test spawner configuration."""
spawner = GlicidSpawner()
spawner = GlicidSpawner(progress_rate=10)
assert isinstance(spawner, GlicidSpawner)
assert isinstance(spawner, SlurmSpawner)
@ -19,6 +22,7 @@ def test_spawner_config():
assert spawner.batchspawner_singleuser_cmd == 'glicid-spawner-singleuser'
assert spawner.req_qos == 'short'
assert spawner.progress_rate == 10
def test_spawner_parse_job_id():
@ -47,3 +51,48 @@ def test_spawner_options_form(monkeypatch):
assert spawner.user.name == 'john-doe'
assert spawner.options_form == "options_form('john-doe')"
assert spawner.options_from_form({'foo': 123}) == "options_from_form({'foo': 123})"
@pytest.mark.asyncio
async def test_spawner_progress(monkeypatch):
"""Test spawner progress messages."""
def isrunning():
"""Running generator values."""
yield False # NOTFOUND
yield False # PENDING
yield from repeat(True) # RUNNING
def ispending():
"""Pending generator values."""
yield False # NOTFOUND
yield True # PENDING
yield from repeat(False) # RUNNING (never used)
# Mock Job status generator
iter_ispending = iter(ispending())
iter_isrunning = iter(isrunning())
monkeypatch.setattr(
glicid_spawner.GlicidSpawner, 'state_isrunning', lambda _: next(iter_isrunning)
)
monkeypatch.setattr(
glicid_spawner.GlicidSpawner, 'state_ispending', lambda _: next(iter_ispending)
)
async def mock_sleep(_):
"""Mock asyncio sleep."""
monkeypatch.setattr(asyncio, 'sleep', mock_sleep)
spawner = GlicidSpawner(progress_rate=20)
progress = [msg['progress'] async for msg in spawner.progress()]
assert progress == [
10, # submit
20, # pending
60, # running | elapse time = 20 -> setup
80, # running | elapse time = 40 -> connect
95, # running | elapse time = 60 -> too long
]