Skip to content

Commit 482789f

Browse files
pranaverma92Prabhakar Kumar
authored andcommitted
Fixes unit test to wait for server to be available before testing against it.
1 parent a8e2470 commit 482789f

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

matlab_proxy/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ async def wsforward(ws_from, ws_to):
500500

501501
await asyncio.wait(
502502
[
503-
wsforward(ws_server, ws_client),
504-
wsforward(ws_client, ws_server),
503+
asyncio.create_task(wsforward(ws_server, ws_client)),
504+
asyncio.create_task(wsforward(ws_client, ws_server)),
505505
],
506506
return_when=asyncio.FIRST_COMPLETED,
507507
)

tests/test_app.py

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ def get_connection_string():
5252
5353

5454

55+
async def wait_for_matlab_to_be_up(test_server, sleep_seconds):
56+
"""Checks at max five times for the MATLAB status to be up and throws ConnectionError
57+
if MATLAB status is not up.
58+
59+
This function mitigates the scenario where the tests may try to send the request
60+
to the test server and the MATLAB status is not up yet which may cause the test to fail
61+
unexpectedly.
62+
63+
Use this function if the test intends to wait for the matlab status to be up before
64+
sending any requests.
65+
66+
Args:
67+
test_server (aiohttp_client) : A aiohttp_client server to send HTTP GET request.
68+
sleep_seconds : Seconds to be sent to the asyncio.sleep method
69+
"""
70+
71+
count = 0
72+
while True:
73+
resp = await test_server.get("/get_status")
74+
assert resp.status == HTTPStatus.OK
75+
76+
resp_json = json.loads(await resp.text())
77+
78+
if resp_json["matlab"]["status"] == "up":
79+
break
80+
else:
81+
count += 1
82+
await asyncio.sleep(sleep_seconds)
83+
if count > test_constants.FIVE_MAX_TRIES:
84+
raise ConnectionError
85+
86+
5587
@pytest.fixture(
5688
name="licensing_data",
5789
params=[
@@ -225,20 +257,8 @@ async def test_start_matlab_route(test_server):
225257
test_server (aiohttp_client): A aiohttp_client server to send GET request to.
226258
"""
227259
# Waiting for the matlab process to start up.
228-
count = 0
229-
while True:
230-
resp = await test_server.get("/get_status")
231-
assert resp.status == HTTPStatus.OK
232-
233-
resp_json = json.loads(await resp.text())
234-
235-
if resp_json["matlab"]["status"] == "up":
236-
break
237-
else:
238-
count += 1
239-
await asyncio.sleep(1)
240-
if count > test_constants.FIVE_MAX_TRIES:
241-
raise ConnectionError
260+
sleep_interval = 1
261+
await wait_for_matlab_to_be_up(test_server, sleep_interval)
242262

243263
# Send get request to end point
244264
await test_server.put("/start_matlab")
@@ -464,29 +484,6 @@ async def test_matlab_proxy_http_post_request(proxy_payload, test_server):
464484
# "connection": "upgrade",
465485
# "upgrade": "websocket",
466486
# }
467-
@pytest.mark.parametrize(
468-
"headers",
469-
[
470-
{
471-
"connection": "Upgrade",
472-
"Upgrade": "websocket",
473-
},
474-
{
475-
"connection": "upgrade",
476-
"upgrade": "websocket",
477-
},
478-
],
479-
)
480-
async def test_matlab_proxy_web_socket(test_server, headers):
481-
"""Test to check if test_server proxies web socket request to fake matlab server
482-
483-
Args:
484-
test_server (aiohttp_client): Test Server to send HTTP Requests.
485-
"""
486-
487-
resp = await test_server.ws_connect("/http_ws_request.html", headers=headers)
488-
text = await resp.receive()
489-
assert text.type == aiohttp.WSMsgType.CLOSED
490487

491488

492489
async def test_set_licensing_info_put_nlm(test_server):
@@ -525,6 +522,38 @@ async def test_set_licensing_info_put_invalid_license(test_server):
525522
assert resp.status == HTTPStatus.BAD_REQUEST
526523

527524

525+
@pytest.mark.parametrize(
526+
"headers",
527+
[
528+
{
529+
"connection": "Upgrade",
530+
"Upgrade": "websocket",
531+
},
532+
{
533+
"connection": "upgrade",
534+
"upgrade": "websocket",
535+
},
536+
],
537+
ids=["Uppercase header", "Lowercase header"],
538+
)
539+
async def test_matlab_proxy_web_socket(test_server, headers):
540+
"""Test to check if test_server proxies web socket request to fake matlab server
541+
542+
Args:
543+
test_server (aiohttp_client): Test Server to send HTTP Requests.
544+
"""
545+
546+
sleep_interval = 2
547+
await wait_for_matlab_to_be_up(test_server, sleep_interval)
548+
resp = await test_server.ws_connect("/http_ws_request.html/", headers=headers)
549+
text = await resp.receive()
550+
websocket_response_string = (
551+
"Hello world" # This string is set by the web_socket_handler in devel.py
552+
)
553+
assert text.type == aiohttp.WSMsgType.TEXT
554+
assert text.data == websocket_response_string
555+
556+
528557
async def test_set_licensing_info_put_mhlm(test_server):
529558
"""Test to check endpoint : "/set_licensing_info"
530559

0 commit comments

Comments
 (0)