File: test_block_error_handler.py

package info (click to toggle)
python-parsl 2025.01.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,072 kB
  • sloc: python: 23,817; makefile: 349; sh: 276; ansic: 45
file content (174 lines) | stat: -rw-r--r-- 6,068 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from functools import partial
from unittest.mock import Mock

import pytest

from parsl.executors import HighThroughputExecutor
from parsl.jobs.error_handlers import (
    noop_error_handler,
    simple_error_handler,
    windowed_error_handler,
)
from parsl.jobs.states import JobState, JobStatus
from parsl.providers import LocalProvider


@pytest.mark.local
def test_block_error_handler_false():
    mock = Mock()
    htex = HighThroughputExecutor(block_error_handler=False, encrypted=True)
    assert htex.block_error_handler is noop_error_handler
    htex.set_bad_state_and_fail_all = mock

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}

    htex.handle_errors(bad_jobs)
    mock.assert_not_called()


@pytest.mark.local
def test_block_error_handler_mock():
    handler_mock = Mock()
    htex = HighThroughputExecutor(block_error_handler=handler_mock, encrypted=True)
    assert htex.block_error_handler is handler_mock

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}

    htex.handle_errors(bad_jobs)
    handler_mock.assert_called()
    handler_mock.assert_called_with(htex, bad_jobs)


@pytest.mark.local
def test_simple_error_handler():
    htex = HighThroughputExecutor(block_error_handler=simple_error_handler,
                                  encrypted=True,
                                  provider=LocalProvider(init_blocks=3))

    assert htex.block_error_handler is simple_error_handler

    bad_state_mock = Mock()
    htex.set_bad_state_and_fail_all = bad_state_mock

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    # Check the bad behavior where if any job is not failed
    # bad state won't be set
    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}

    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}

    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_called()


@pytest.mark.local
def test_windowed_error_handler():
    htex = HighThroughputExecutor(block_error_handler=windowed_error_handler, encrypted=True)
    assert htex.block_error_handler is windowed_error_handler

    bad_state_mock = Mock()
    htex.set_bad_state_and_fail_all = bad_state_mock

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.FAILED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.COMPLETED),
                '4': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_called()


@pytest.mark.local
def test_windowed_error_handler_sorting():
    htex = HighThroughputExecutor(block_error_handler=windowed_error_handler, encrypted=True)
    assert htex.block_error_handler is windowed_error_handler

    bad_state_mock = Mock()
    htex.set_bad_state_and_fail_all = bad_state_mock

    bad_jobs = {'8': JobStatus(JobState.FAILED),
                '9': JobStatus(JobState.FAILED),
                '10': JobStatus(JobState.FAILED),
                '11': JobStatus(JobState.COMPLETED),
                '12': JobStatus(JobState.COMPLETED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'8': JobStatus(JobState.COMPLETED),
                '9': JobStatus(JobState.FAILED),
                '21': JobStatus(JobState.FAILED),
                '22': JobStatus(JobState.FAILED),
                '10': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_called()


@pytest.mark.local
def test_windowed_error_handler_with_threshold():
    error_handler = partial(windowed_error_handler, threshold=2)
    htex = HighThroughputExecutor(block_error_handler=error_handler, encrypted=True)
    assert htex.block_error_handler is error_handler

    bad_state_mock = Mock()
    htex.set_bad_state_and_fail_all = bad_state_mock

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.FAILED),
                '3': JobStatus(JobState.COMPLETED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.COMPLETED),
                '3': JobStatus(JobState.COMPLETED),
                '4': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_not_called()

    bad_jobs = {'1': JobStatus(JobState.COMPLETED),
                '2': JobStatus(JobState.COMPLETED),
                '3': JobStatus(JobState.FAILED),
                '4': JobStatus(JobState.FAILED)}
    htex.handle_errors(bad_jobs)
    bad_state_mock.assert_called()