Projet

Général

Profil

0001-jump-log-timeout-compute-errors-43384.patch

Thomas Noël, 30 mai 2020 03:52

Télécharger (4,94 ko)

Voir les différences:

Subject: [PATCH] jump: log timeout compute errors (#43384)

 tests/test_workflows.py | 98 +++++++++++++++++++++++++++++++++++++++++
 wcs/wf/jump.py          | 13 +++++-
 2 files changed, 110 insertions(+), 1 deletion(-)
tests/test_workflows.py
2147 2147
    _apply_timeouts(two_pubs)
2148 2148

  
2149 2149

  
2150
def test_compute_timeout(two_pubs):
2151
    workflow = Workflow(name='timeout')
2152
    st1 = workflow.add_status('Status1', 'st1')
2153
    st2 = workflow.add_status('Status2', 'st2')
2154

  
2155
    jump = JumpWorkflowStatusItem()
2156
    jump.id = '_jump'
2157
    jump.by = ['_submitter', '_receiver']
2158
    jump.status = 'st2'
2159
    st1.items.append(jump)
2160
    jump.parent = st1
2161

  
2162
    workflow.store()
2163

  
2164
    formdef = FormDef()
2165
    formdef.name = 'baz'
2166
    formdef.fields = [StringField(id='1', label='Test', type='string', varname='foo')]
2167
    formdef.workflow_id = workflow.id
2168
    assert formdef.get_workflow().id == workflow.id
2169
    formdef.store()
2170

  
2171
    formdata = formdef.data_class()()
2172
    formdata.data = {'1': '10'}
2173
    formdata.just_created()
2174
    formdata.store()
2175
    formdata_id = formdata.id
2176

  
2177
    two_pubs.substitutions.feed(formdata)
2178

  
2179
    # Django
2180
    jump.timeout = '{{ form_var_foo|subtract:form_var_foo }}'  # 10-10 = 0
2181
    workflow.store()
2182
    formdata.status = 'wf-st1'
2183
    formdata.store()
2184
    LoggedError.wipe()
2185
    time.sleep(0.3)
2186
    _apply_timeouts(two_pubs)
2187
    assert formdef.data_class().get(formdata_id).status == 'wf-st2'
2188
    assert LoggedError.count() == 0
2189

  
2190
    jump.timeout = '{{ bad + syntax }}'
2191
    workflow.store()
2192
    formdata.status = 'wf-st1'
2193
    formdata.store()
2194
    LoggedError.wipe()
2195
    _apply_timeouts(two_pubs)
2196
    assert formdef.data_class().get(formdata_id).status == 'wf-st1'
2197
    assert LoggedError.count() == 1
2198
    logged_error = LoggedError.select()[0]
2199
    assert logged_error.summary == 'Failed to compute template'
2200

  
2201
    jump.timeout = '{{ form_var_foo|divide:3 }}'  # 10/3 = 3.333... not an integer
2202
    workflow.store()
2203
    formdata.status = 'wf-st1'
2204
    formdata.store()
2205
    LoggedError.wipe()
2206
    _apply_timeouts(two_pubs)
2207
    assert formdef.data_class().get(formdata_id).status == 'wf-st1'
2208
    assert LoggedError.count() == 1
2209
    logged_error = LoggedError.select()[0]
2210
    assert "Invalid timeout '3.333" in logged_error.summary
2211

  
2212
    # Python
2213
    jump.timeout = '=1/int(form_var_foo)'  # 1/10 = 0.1s
2214
    workflow.store()
2215
    formdata.status = 'wf-st1'
2216
    formdata.store()
2217
    time.sleep(0.3)
2218
    LoggedError.wipe()
2219
    _apply_timeouts(two_pubs)
2220
    assert formdef.data_class().get(formdata_id).status == 'wf-st2'
2221
    assert LoggedError.count() == 0
2222

  
2223
    jump.timeout = '=int(form_var_foo)/0'
2224
    workflow.store()
2225
    formdata.status = 'wf-st1'
2226
    formdata.store()
2227
    LoggedError.wipe()
2228
    _apply_timeouts(two_pubs)
2229
    assert formdef.data_class().get(formdata_id).status == 'wf-st1'
2230
    assert LoggedError.count() == 1
2231
    logged_error = LoggedError.select()[0]
2232
    assert logged_error.summary == 'Failed to compute Python expression'
2233
    assert logged_error.exception_class == 'ZeroDivisionError'
2234

  
2235
    jump.timeout = '="string"'
2236
    workflow.store()
2237
    formdata.status = 'wf-st1'
2238
    formdata.store()
2239
    LoggedError.wipe()
2240
    _apply_timeouts(two_pubs)
2241
    assert formdef.data_class().get(formdata_id).status == 'wf-st1'
2242
    assert LoggedError.count() == 1
2243
    logged_error = LoggedError.select()[0]
2244
    assert logged_error.summary == "Invalid timeout 'string' (not an integer)"
2245
    assert logged_error.exception_class == 'ValueError'
2246

  
2247

  
2150 2248
def test_legacy_timeout(pub):
2151 2249
    workflow = Workflow(name='timeout')
2152 2250
    st1 = workflow.add_status('Status1', 'st1')
wcs/wf/jump.py
243 243
            must_jump = must_jump and triggered
244 244

  
245 245
        if self.timeout:
246
            timeout = int(self.compute(self.timeout))
246
            try:
247
                timeout = self.compute(self.timeout, raises=True, formdata=formdata, status_item=self)
248
            except Exception:
249
                # already logged by self.compute
250
                return False
251
            try:
252
                timeout = int(timeout)
253
            except (ValueError, TypeError) as ex:
254
                from wcs.logged_errors import LoggedError
255
                LoggedError.record(_('Invalid timeout %r (not an integer)') % timeout,
256
                                   formdata=formdata, status_item=self, exception=ex)
257
                return False
247 258
            last = formdata.last_update_time
248 259
            if last:
249 260
                diff = time.time() - time.mktime(last)
250
-