Skip to content

Commit

Permalink
Merge pull request #31 from MarketSquare/18-make-test-cases-asynchronous
Browse files Browse the repository at this point in the history
WIP: basic multiprocessing
  • Loading branch information
Noordsestern authored Nov 1, 2023
2 parents 9318b14 + d7fc52d commit 70fd5c5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 38 deletions.
102 changes: 65 additions & 37 deletions RobotFrameworkService/routers/robotframework.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@

import robot

import multiprocessing as mp

router = APIRouter(
prefix="/robotframework",
responses={404: {"description": "Not found: Webservice is either busy or requested endpoint is not supported."}},
)

async def run_robot_in_brackground(func, args=[], kwargs={}):
p = mp.Process(target=func, args=args, kwargs=kwargs)
p.start()
return p

@router.get('/run/all', tags=["execution"])
async def run(request: Request):
"""
Run all task available.
"""
id = request.headers["request-id"]
result: int = _start_all_robot_tasks(id)
async def run_robot_and_wait(func, args=[], kwargs={}):
# this is still blocking
result: int = func(*args, **kwargs)
if result == 0:
result_page = 'PASS'
result_page += f'<p><a href="/logs/{id}/log.html">Go to log</a></p>'
Expand All @@ -37,26 +39,47 @@ async def run(request: Request):
return Response(content=result_page, media_type="text/html", status_code=status_code)


@router.get('/run/all', tags=["execution"])
async def run_all(request: Request):
"""
Run all task available.
"""
id = request.headers["request-id"]
response = await run_robot_and_wait(func=_start_all_robot_tasks, args=[id])

return response


@router.get('/run/all/async', tags=["execution"])
async def run_all_async(request: Request):
"""
Starts all Robot tasks. Returns execution id and continures to run Robot tasks in background.
"""
id = request.headers["request-id"]
await run_robot_in_brackground(func=_start_all_robot_tasks, args=[id])
return id


@router.get('/run/{task}', tags=["execution"])
async def run_task(task, request: Request):
"""
Run a given task.
"""
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
result: int = _start_specific_robot_task(id, task, variables=variables)
if result == 0:
result_page = 'PASS'
result_page += f'<p><a href="/logs/{id}/log.html">Go to log</a></p>'

elif 250 >= result >= 1:
result_page = f'FAIL: {result} tasks failed'
result_page += f'<p><a href="/logs/{id}/log.html">Go to log</a></p>'
variables = RequestHelper.parse_variables_from_query(request)
response = await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id': id, 'task':task, 'variables':variables})
return response

else:
result_page = f'FAIL: Errorcode {result}'

return Response(content=result_page, media_type="text/html")

@router.get('/run/{task}/async', tags=["execution"])
async def run_task_async(task, request: Request):
"""
Start a given task. Returns execution id and continues to run Robot task in background.
"""
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
await run_robot_in_brackground(func=_start_specific_robot_task, kwargs={'task':task, 'variables':variables})
return id

@router.get('/run/suite/{suite}', tags=["execution"])
async def run_suite(suite, request: Request):
Expand All @@ -65,35 +88,40 @@ async def run_suite(suite, request: Request):
"""
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
result: int = _start_specific_robot_suite(id, suite, variables=variables)
if result == 0:
result_page = 'PASS'
elif 250 >= result >= 1:
result_page = f'FAIL: {result} tasks for suite {suite} failed'
else:
result_page = f'FAIL: Errorcode {result}'
result_page += f'<p><a href="/logs/{id}/log.html">Go to log</a></p>'
return Response(content=result_page, media_type="text/html")
response = await run_robot_and_wait(func=_start_specific_robot_suite, kwargs={'id': id, 'suite':suite, 'variables':variables})
return response

@router.get('/run/suite/{suite}/async', tags=["execution"])
async def run_suite_async(suite, request: Request):
"""
Start a given suite. Returns execution id and continues to run Robot suite in background.
"""
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
await run_robot_in_brackground(func=_start_specific_robot_suite, kwargs={'suite':suite, 'variables':variables})
return id


@router.get('/run_and_show/{task}', tags=["execution"], response_class=HTMLResponse)
async def start_robot_task_and_show_log(task: str, arguments: Request):
async def start_robot_task_and_show_log(task: str, request: Request):
"""
Run a given task with variables and return log.html
"""
variables = RequestHelper.parse_variables_from_query(arguments)
_start_specific_robot_task(task, variables)
return RedirectResponse(f"/logs/{task}/log.html")
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id':id, 'task':task, 'variables':variables})
return RedirectResponse(f"/logs/{id}/log.html")


@router.get('/run_and_show_report/{task}', tags=["execution"], response_class=HTMLResponse)
async def start_robot_task_and_show_report(task: str, arguments: Request):
async def start_robot_task_and_show_report(task: str, request: Request):
"""
Run a given task with variables and return report.html
"""
variables = RequestHelper.parse_variables_from_query(arguments)
_start_specific_robot_task(task, variables)
return RedirectResponse(f"/logs/{task}/report.html")
id = request.headers["request-id"]
variables = RequestHelper.parse_variables_from_query(request)
await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id':id, 'task':task, 'variables':variables})
return RedirectResponse(f"/logs/{id}/report.html")


@router.get('/show_log/{executionid}', tags=["reporting"], response_class=HTMLResponse)
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
robotframework-tidy
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
httpx
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_is_robotreport_available(self):
def __get_robot_webservice(self,endpoint, expected_response_code=200):
with TestClient(app) as client:
response = client.get(endpoint)
self.assertEqual(expected_response_code, response.status_code)
self.assertEqual(expected_response_code, response.status_code, response.text)
return response

def __is_robot_passed(self, response, msg=None):
Expand Down

0 comments on commit 70fd5c5

Please sign in to comment.