|
| 1 | +from labthings.core.tasks import thread |
| 2 | +import pytest |
| 3 | + |
| 4 | +import gevent |
| 5 | +from gevent.thread import get_ident |
| 6 | + |
| 7 | + |
| 8 | +def test_task_with_args(): |
| 9 | + def task_func(arg, kwarg=False): |
| 10 | + pass |
| 11 | + |
| 12 | + task_obj = thread.TaskThread( |
| 13 | + target=task_func, args=("String arg",), kwargs={"kwarg": True} |
| 14 | + ) |
| 15 | + assert isinstance(task_obj, gevent.Greenlet) |
| 16 | + assert task_obj._target == task_func |
| 17 | + assert task_obj._args == ("String arg",) |
| 18 | + assert task_obj._kwargs == {"kwarg": True} |
| 19 | + |
| 20 | + |
| 21 | +def test_task_without_args(): |
| 22 | + def task_func(): |
| 23 | + pass |
| 24 | + |
| 25 | + task_obj = thread.TaskThread(target=task_func) |
| 26 | + |
| 27 | + assert isinstance(task_obj, gevent.Greenlet) |
| 28 | + assert task_obj._target == task_func |
| 29 | + assert task_obj._args == () |
| 30 | + assert task_obj._kwargs == {} |
| 31 | + |
| 32 | + |
| 33 | +def test_task_identity(): |
| 34 | + def task_func(): |
| 35 | + pass |
| 36 | + |
| 37 | + task_obj = thread.TaskThread(target=task_func) |
| 38 | + |
| 39 | + assert task_obj.ident == get_ident(task_obj) |
| 40 | + |
| 41 | + |
| 42 | +def test_task_start(): |
| 43 | + def task_func(): |
| 44 | + return "Return value" |
| 45 | + |
| 46 | + task_obj = thread.TaskThread(target=task_func) |
| 47 | + |
| 48 | + assert task_obj._status == "idle" |
| 49 | + assert task_obj._return_value is None |
| 50 | + |
| 51 | + task_obj.start() |
| 52 | + task_obj.join() |
| 53 | + assert task_obj._return_value == "Return value" |
| 54 | + assert task_obj._status == "success" |
| 55 | + |
| 56 | + |
| 57 | +def test_task_exception(): |
| 58 | + exc_to_raise = Exception("Exception message") |
| 59 | + |
| 60 | + def task_func(): |
| 61 | + raise exc_to_raise |
| 62 | + |
| 63 | + task_obj = thread.TaskThread(target=task_func) |
| 64 | + task_obj.start() |
| 65 | + task_obj.join() |
| 66 | + |
| 67 | + assert task_obj._status == "error" |
| 68 | + assert task_obj._return_value == str(exc_to_raise) |
| 69 | + |
| 70 | + |
| 71 | +def test_task_terminate(): |
| 72 | + def task_func(): |
| 73 | + while True: |
| 74 | + gevent.sleep(0.5) |
| 75 | + |
| 76 | + task_obj = thread.TaskThread(target=task_func) |
| 77 | + task_obj.start() |
| 78 | + task_obj.started_event.wait() |
| 79 | + assert task_obj._status == "running" |
| 80 | + task_obj.terminate() |
| 81 | + task_obj.join() |
| 82 | + assert task_obj._status == "terminated" |
| 83 | + assert task_obj._return_value is None |
| 84 | + |
| 85 | + |
| 86 | +def test_task_log_list(): |
| 87 | + import logging |
| 88 | + import os |
| 89 | + |
| 90 | + def task_func(): |
| 91 | + logging.warning("Task warning") |
| 92 | + |
| 93 | + task_obj = thread.TaskThread(target=task_func) |
| 94 | + task_obj.start() |
| 95 | + task_obj.started_event.wait() |
| 96 | + |
| 97 | + assert len(task_obj.log) == 1 |
| 98 | + assert task_obj.log[0]["message"] == "Task warning" |
| 99 | + assert task_obj.log[0]["levelname"] == "WARNING" |
| 100 | + assert task_obj.log[0]["filename"] == os.path.basename(__file__) |
| 101 | + |
| 102 | + |
| 103 | +def test_task_log_without_thread(): |
| 104 | + |
| 105 | + task_log_handler = thread.ThreadLogHandler() |
| 106 | + |
| 107 | + # Should always return True if not attached to a thread |
| 108 | + assert task_log_handler.check_thread(record=None) |
| 109 | + |
| 110 | + |
| 111 | +def test_task_log_with_incorrect_thread(): |
| 112 | + |
| 113 | + task_obj = thread.TaskThread() |
| 114 | + task_log_handler = thread.ThreadLogHandler(thread=task_obj) |
| 115 | + |
| 116 | + # Should always return False if called from outside the log handlers thread |
| 117 | + assert task_log_handler.thread == task_obj |
| 118 | + assert not task_log_handler.check_thread(record=None) |
0 commit comments