Skip to content

Commit

Permalink
Updates test points to include SERVICE_UNAVAILABLE status errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishan Sharma authored and Prabhakar Kumar committed Oct 31, 2023
1 parent e8316cd commit 5de4355
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
4 changes: 1 addition & 3 deletions matlab_proxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ async def matlab_view(req):
logger.debug(
"MATLAB hasn't fully started, please retry after embedded connector has started"
)
raise web.HTTPServiceUnavailable(
"MATLAB hasn't fully started yet, please retry after some time."
)
raise web.HTTPServiceUnavailable()

# WebSocket
# According to according to RFC6455 (https://www.rfc-editor.org/rfc/rfc6455.html)
Expand Down
57 changes: 39 additions & 18 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Copyright (c) 2020-2023 The MathWorks, Inc.
# Copyright 2020-2023 The MathWorks, Inc.

import asyncio
import datetime
import json
import platform
import random
import time
import datetime
from datetime import timedelta, timezone
from http import HTTPStatus

import aiohttp
import pytest
import random
from http import HTTPStatus
import test_constants

from matlab_proxy import app, util
from matlab_proxy.util.mwi import environment_variables as mwi_env
from matlab_proxy.util.mwi.exceptions import MatlabInstallError
from datetime import timedelta, timezone
from matlab_proxy.util.mwi.exceptions import EntitlementError
import test_constants
from matlab_proxy.util.mwi.exceptions import EntitlementError, MatlabInstallError


def test_create_app():
Expand Down Expand Up @@ -314,8 +314,18 @@ async def test_root_redirect(test_server):
test_server (aiohttp_client): A aiohttp_client server to send HTTP GET request.
"""
resp = await test_server.get("/")
assert resp.status == 404
count = 0
while True:
resp = await test_server.get("/")
if resp.status == HTTPStatus.SERVICE_UNAVAILABLE:
time.sleep(test_constants.ONE_SECOND_DELAY)
count += 1
else:
assert resp.status == HTTPStatus.NOT_FOUND
break

if count > test_constants.FIVE_MAX_TRIES:
raise ConnectionError


@pytest.fixture(name="proxy_payload")
Expand Down Expand Up @@ -343,10 +353,21 @@ async def test_matlab_proxy_404(proxy_payload, test_server):

# Request a non-existing html file.
# Request gets proxied to app.matlab_view() which should raise HTTPNotFound() exception ie. return HTTP status code 404
resp = await test_server.post(
"./1234.html", data=json.dumps(proxy_payload), headers=headers
)
assert resp.status == 404

count = 0
while True:
resp = await test_server.post(
"./1234.html", data=json.dumps(proxy_payload), headers=headers
)
if resp.status == HTTPStatus.SERVICE_UNAVAILABLE:
time.sleep(test_constants.ONE_SECOND_DELAY)
count += 1
else:
assert resp.status == HTTPStatus.NOT_FOUND
break

if count > test_constants.FIVE_MAX_TRIES:
raise ConnectionError


async def test_matlab_proxy_http_get_request(proxy_payload, test_server):
Expand All @@ -369,7 +390,7 @@ async def test_matlab_proxy_http_get_request(proxy_payload, test_server):
"/http_get_request.html", data=json.dumps(proxy_payload)
)

if resp.status == 404:
if resp.status in (HTTPStatus.NOT_FOUND, HTTPStatus.SERVICE_UNAVAILABLE):
time.sleep(1)
count += 1

Expand Down Expand Up @@ -402,7 +423,7 @@ async def test_matlab_proxy_http_put_request(proxy_payload, test_server):
"/http_put_request.html", data=json.dumps(proxy_payload)
)

if resp.status == 404:
if resp.status in (HTTPStatus.NOT_FOUND, HTTPStatus.SERVICE_UNAVAILABLE):
time.sleep(1)
count += 1

Expand Down Expand Up @@ -435,7 +456,7 @@ async def test_matlab_proxy_http_delete_request(proxy_payload, test_server):
"/http_delete_request.html", data=json.dumps(proxy_payload)
)

if resp.status == 404:
if resp.status in (HTTPStatus.NOT_FOUND, HTTPStatus.SERVICE_UNAVAILABLE):
time.sleep(1)
count += 1

Expand Down Expand Up @@ -467,7 +488,7 @@ async def test_matlab_proxy_http_post_request(proxy_payload, test_server):
data=json.dumps(proxy_payload),
)

if resp.status == 404:
if resp.status in (HTTPStatus.NOT_FOUND, HTTPStatus.SERVICE_UNAVAILABLE):
time.sleep(1)
count += 1

Expand Down

0 comments on commit 5de4355

Please sign in to comment.