diff --git a/test/unit/queue_worker/__init__.py b/test/unit/queue_worker/__init__.py new file mode 100644 index 00000000000..9009d803301 --- /dev/null +++ b/test/unit/queue_worker/__init__.py @@ -0,0 +1,3 @@ +""" +Tests for Galaxy Queue Worker +""" diff --git a/test/unit/queue_worker/conftest.py b/test/unit/queue_worker/conftest.py new file mode 100644 index 00000000000..0fa9c33bf08 --- /dev/null +++ b/test/unit/queue_worker/conftest.py @@ -0,0 +1,44 @@ +import contextlib +import os +import tempfile + +import pytest + +from galaxy.util import which +from ..unittest_utils import galaxy_mock + + +@contextlib.contextmanager +def create_base_test(connection): + app = galaxy_mock.MockApp(database_connection=connection) + app.config.database_connection = connection + yield app + + +@pytest.fixture(scope='session') +def sqlite_connection(): + fd, path = tempfile.mkstemp() + os.close(fd) + yield 'sqlite:////%s' % path + os.remove(path) + + +@pytest.fixture(scope='session') +def sqlite_app(sqlite_connection): + with create_base_test(sqlite_connection) as app: + yield app + + +@pytest.fixture(scope='session') +def postgres_app(postgresql_proc): + connection = "postgresql://{p.user}@{p.host}:{p.port}/".format(p=postgresql_proc) + with create_base_test(connection) as app: + yield app + + +@pytest.fixture(params=['postgres_app', 'sqlite_app'], scope='session') +def database_app(request): + if request.param == 'postgres_app': + if not which('initdb'): + pytest.skip("initdb must be on PATH for postgresql fixture") + yield request.getfixturevalue(request.param) diff --git a/test/unit/queue_worker/test_queue_worker.py b/test/unit/queue_worker/test_queue_worker.py new file mode 100644 index 00000000000..f108bb015b3 --- /dev/null +++ b/test/unit/queue_worker/test_queue_worker.py @@ -0,0 +1,54 @@ +import os +import tempfile +import time + +import pytest + +from galaxy.queue_worker import ( + GalaxyQueueWorker, + send_control_task, +) +from galaxy.queues import connection_from_config +from ..tools_support import UsesApp + + +def foo(app, **kwargs): + app.some_var = 'bar' + + +control_message_to_task = {'echo': foo} + + +@pytest.fixture +def sqlite_database_path(): + fd, path = tempfile.mkstemp() + os.close(fd) + yield path + os.remove(path) + + +@pytest.fixture +def simple_app(sqlite_database_path): + test = UsesApp() + test.setup_app() + test.app.config.server_name = 'test_queue_worker' + test.app.config.server_names = ['test_server_name', 'test_queue_worker'] + test.app.config.amqp_internal_connection = 'sqlalchemy+sqlite:////%s' % sqlite_database_path + test.app.amqp_internal_connection_obj = connection_from_config(test.app.config) + test.queue_worker = GalaxyQueueWorker(app=test.app, task_mapping=control_message_to_task) + test.queue_worker.bind_and_start() + yield test.app + test.queue_worker.shutdown() + + +def test_queue_worker_echo(simple_app): + simple_app.some_var = 'foo' + send_control_task(app=simple_app, task='echo') + wait_for_var(simple_app, 'some_var', 'bar') + + +def wait_for_var(obj, var, value, tries=10, sleep=0.25): + while getattr(obj, var) != value and tries >= 0: + tries -= 1 + time.sleep(sleep) + assert getattr(obj, var) == value