|
| 1 | +import time |
| 2 | +import pytest |
| 3 | +from src.thread import Thread, exceptions |
| 4 | + |
| 5 | + |
| 6 | +# >>>>>>>>>> Dummy Functions <<<<<<<<<< # |
| 7 | +def _dummy_target_raiseToPower(x: float, power: float, delay: float = 0): |
| 8 | + time.sleep(delay) |
| 9 | + return x**power |
| 10 | + |
| 11 | +def _dummy_raiseException(x: Exception, delay: float = 0): |
| 12 | + time.sleep(delay) |
| 13 | + raise x |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +# >>>>>>>>>> General Use <<<<<<<<<< # |
| 19 | +def test_threadCreation(): |
| 20 | + """This test is for testing parsing of args and kwargs and `.join()` method""" |
| 21 | + new = Thread( |
| 22 | + target = _dummy_target_raiseToPower, |
| 23 | + args = [4], |
| 24 | + kwargs = { 'power': 2 } |
| 25 | + ) |
| 26 | + new.start() |
| 27 | + s = new.join() |
| 28 | + assert s.status == 'Thread terminated' |
| 29 | + assert new.result == 16 |
| 30 | + |
| 31 | +def test_threadingThreadParsing(): |
| 32 | + """This test is for testing parsing arguments to `threading.Thead`""" |
| 33 | + new = Thread( |
| 34 | + target = _dummy_target_raiseToPower, |
| 35 | + args = [4, 2, 5], |
| 36 | + name = 'testingThread', |
| 37 | + daemon = True |
| 38 | + ) |
| 39 | + new.start() |
| 40 | + assert new._thread and new._thread.name == 'testingThread' |
| 41 | + |
| 42 | +def test_suppressAll(): |
| 43 | + """This test is for testing that errors are suppressed properly""" |
| 44 | + new = Thread( |
| 45 | + target = _dummy_raiseException, |
| 46 | + args = [ValueError()], |
| 47 | + suppress_errors = True, |
| 48 | + daemon = True |
| 49 | + ) |
| 50 | + new.start() |
| 51 | + new.join() |
| 52 | + assert len(new.errors) == 1 |
| 53 | + assert isinstance(new.errors[0], ValueError) |
| 54 | + |
| 55 | +def test_ignoreSpecificError(): |
| 56 | + """This test is for testing that specific errors are ignored properly""" |
| 57 | + new = Thread( |
| 58 | + target = _dummy_raiseException, |
| 59 | + args = [ValueError()], |
| 60 | + ignore_errors = [ValueError], |
| 61 | + ) |
| 62 | + new.start() |
| 63 | + new.join() |
| 64 | + assert len(new.errors) == 0 |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +# >>>>>>>>>> Raising Exceptions <<<<<<<<<< # |
| 70 | +def test_raises_notInitializedError(): |
| 71 | + """This test should raise ThreadNotInitializedError""" |
| 72 | + new = Thread( |
| 73 | + target = _dummy_target_raiseToPower, |
| 74 | + args = [4, 2] |
| 75 | + ) |
| 76 | + |
| 77 | + with pytest.raises(exceptions.ThreadNotInitializedError): |
| 78 | + new.result |
| 79 | + |
| 80 | +def test_raises_stillRunningError(): |
| 81 | + """This test should raise ThreadStillRunningError""" |
| 82 | + new = Thread( |
| 83 | + target = _dummy_target_raiseToPower, |
| 84 | + args = [4, 2, 5] |
| 85 | + ) |
| 86 | + new.start() |
| 87 | + |
| 88 | + with pytest.raises(exceptions.ThreadStillRunningError): |
| 89 | + new.result |
| 90 | + |
| 91 | +def test_raises_ignoreSpecificError(): |
| 92 | + """This test is for testing that non-specified errors are not ignored""" |
| 93 | + new = Thread( |
| 94 | + target = _dummy_raiseException, |
| 95 | + args = [FileExistsError()], |
| 96 | + ignore_errors = [ValueError], |
| 97 | + suppress_errors = False |
| 98 | + ) |
| 99 | + with pytest.raises(FileExistsError): |
| 100 | + new.start() |
| 101 | + new.join() |
| 102 | + |
| 103 | +def test_raises_HookError(): |
| 104 | + """This test should raise """ |
| 105 | + new = Thread( |
| 106 | + target = _dummy_target_raiseToPower, |
| 107 | + args = [4, 2] |
| 108 | + ) |
| 109 | + |
| 110 | + def newhook(x: int): |
| 111 | + raise RuntimeError() |
| 112 | + new.add_hook(newhook) |
| 113 | + |
| 114 | + with pytest.raises(exceptions.HookRuntimeError): |
| 115 | + new.start() |
| 116 | + new.join() |
0 commit comments