Skip to content
Snippets Groups Projects
Commit d4b8ca52 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

introducing sync. option for analytics GET and making tests more robust

parent ee951b4a
No related branches found
No related tags found
No related merge requests found
......@@ -51,9 +51,11 @@ def get_job_details(task_id):
if task_id not in session['tasks']: # access control
return jsonify({'error': "No matching task found."}), 404
async_result = celery.AsyncResult(task_id)
if request.args.get('wait') and request.args.get('wait') == '1':
async_result.get(propagate=False) # wait for results
state = async_result.state
result = async_result.result
if isinstance(result, Exception):
if isinstance(result, Exception): # Exception -> str
result = "{}: {}".format(type(result).__name__, str(result))
return jsonify({'status': state,
'result': result}), 200
......@@ -64,6 +66,7 @@ def cancel_job(task_id):
task_id = str(task_id)
if task_id not in session['tasks']: # Access control
return jsonify({'error': "No matching task found."}), 404
celery.control.revoke(task_id, terminate=True)
# possibly dangerous: http://stackoverflow.com/a/29627549
celery.control.revoke(task_id, terminate=True, signal='SIGUSR1', wait=True)
session['tasks'].remove(task_id)
return jsonify({'task_id': task_id}), 200
"""This module tests the analytics controller."""
"""This module tests the analytics controller module."""
import uuid
import time
import flask
import pytest
from fractalis.celery import app as celery
class TestAnalytics(object):
......@@ -103,9 +106,8 @@ class TestAnalytics(object):
task='test.add',
args={'a': 1, 'b': 2}
)))
time.sleep(1)
body = flask.json.loads(rv.get_data())
new_url = '/analytics/{}'.format(body['task_id'])
new_url = '/analytics/{}?wait=1'.format(body['task_id'])
new_response = app.get(new_url)
new_body = flask.json.loads(new_response.get_data())
assert new_body['result'] == 3
......@@ -117,7 +119,7 @@ class TestAnalytics(object):
)))
time.sleep(1)
body = flask.json.loads(rv.get_data())
new_url = '/analytics/{}'.format(body['task_id'])
new_url = '/analytics/{}?wait=0'.format(body['task_id'])
new_response = app.get(new_url)
new_body = flask.json.loads(new_response.get_data())
assert not new_body['result']
......@@ -128,25 +130,24 @@ class TestAnalytics(object):
task='test.div',
args={'a': 2, 'b': 0}
)))
time.sleep(1)
body = flask.json.loads(rv.get_data())
new_url = '/analytics/{}'.format(body['task_id'])
new_url = '/analytics/{}?wait=1'.format(body['task_id'])
new_response = app.get(new_url)
new_body = flask.json.loads(new_response.get_data())
assert new_body['status'] == 'FAILURE'
assert 'ZeroDivisionError' in new_body['result']
def test_404_if_status_non_existing_resource(self, app):
assert app.get('/analytics/{}'.format(uuid.uuid4())).status_code == 404
assert app.get('/analytics/{}?wait=1'.format(uuid.uuid4())
).status_code == 404
def test_404_if_status_without_auth(self, app):
rv = app.post('/analytics', data=flask.json.dumps(dict(
task='test.do_nothing',
args={'seconds': 4}
)))
time.sleep(1)
body = flask.json.loads(rv.get_data())
new_url = '/analytics/{}'.format(body['task_id'])
new_url = '/analytics/{}?wait=0'.format(body['task_id'])
with app.session_transaction() as sess:
sess['tasks'] = []
assert app.get(new_url).status_code == 404
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment