Skip to content

Commit

Permalink
Add a test for starting worker
Browse files Browse the repository at this point in the history
  • Loading branch information
soapy1 committed Oct 30, 2024
1 parent 5d78a79 commit 89bb975
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions conda-store-server/tests/_internal/worker/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,48 @@

import pytest
import logging
import sys

from traitlets.config import Config

from unittest.mock import patch

from conda_store_server._internal.worker.app import CondaStoreWorker


def test_worker_with_valid_config(conda_store_config, tmp_path):
class MockCeleryApp():
def worker_main(argv):
pass


@pytest.mark.skipif(
sys.platform == "win32", reason="celery beat is not supported on windows"
)
@patch('conda_store_server.app.CondaStore.celery_app')
def test_start_worker(mock_celery_app, conda_store_config):
"""Test that the celery worker is started with the right arguments"""
mock_celery_app.return_value = MockCeleryApp()
conda_store_config["CondaStoreWorker"] = Config(
dict(
log_level=logging.WARN,
watch_paths=["/opt/environments"],
concurrency=4,
)
)
worker = CondaStoreWorker(config=conda_store_config)
worker.initialize()
worker.start()
mock_celery_app.worker_main.assert_called_with([
"worker",
"--loglevel=WARNING",
"--max-tasks-per-child=10",
"--beat",
"--concurrency=4"
])


def test_initialize_worker_with_valid_config_file(conda_store_config, tmp_path):
"""Test that a worker is able to init when the provided config file exists"""
config_file_contents = """
import logging
c.CondaStoreWorker.log_level = logging.INFO
Expand All @@ -20,13 +55,14 @@ def test_worker_with_valid_config(conda_store_config, tmp_path):
test_config_file = str(tmp_path / "config1")
with open(test_config_file, "w") as file:
file.write(config_file_contents)

worker = CondaStoreWorker(config=conda_store_config)
worker.config_file = test_config_file
worker.initialize()


def test_worker_with_invalid_config(conda_store_config):
def test_initialize_worker_with_invalid_config(conda_store_config):
"""Test that a worker is not able to init if the provided config file does not exist"""
worker = CondaStoreWorker(config=conda_store_config)
with pytest.raises(SystemExit) as exc_info:
worker.config_file = "/i/dont/exist"
Expand All @@ -41,6 +77,7 @@ def test_worker_with_invalid_config(conda_store_config):
(logging.INFO, "INFO"),
(logging.CRITICAL, "CRITICAL"),
(logging.FATAL, "CRITICAL"),
(logging.WARN, "WARNING"),
]
)
def test_logger_to_celery_logging_level(logging_level, output):
Expand Down

0 comments on commit 89bb975

Please sign in to comment.