@@ -52,6 +52,38 @@ def get_connection_string():
52
52
53
53
54
54
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
+
55
87
@pytest .fixture (
56
88
name = "licensing_data" ,
57
89
params = [
@@ -225,20 +257,8 @@ async def test_start_matlab_route(test_server):
225
257
test_server (aiohttp_client): A aiohttp_client server to send GET request to.
226
258
"""
227
259
# 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 )
242
262
243
263
# Send get request to end point
244
264
await test_server .put ("/start_matlab" )
@@ -464,29 +484,6 @@ async def test_matlab_proxy_http_post_request(proxy_payload, test_server):
464
484
# "connection": "upgrade",
465
485
# "upgrade": "websocket",
466
486
# }
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
490
487
491
488
492
489
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):
525
522
assert resp .status == HTTPStatus .BAD_REQUEST
526
523
527
524
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
+
528
557
async def test_set_licensing_info_put_mhlm (test_server ):
529
558
"""Test to check endpoint : "/set_licensing_info"
530
559
0 commit comments