Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test server: support simulating server error and timeout #15

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions dumps/test_server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import asyncio
from random import randrange

from aiohttp import web
import argparse
import json
from os import listdir
from os.path import isfile, join

appliances = {}
failure_rate = 0
timeout_rate = 0

async def login(request):
return web.Response(
Expand All @@ -29,12 +36,15 @@ async def token(request):
)

async def get_appliances(request):
if failure_rate > randrange(100):
return web.Response(status=500)
if timeout_rate > randrange(100):
await asyncio.sleep(10.1)
return web.Response(
content_type="application/json",
text=json.dumps(list(appliances.values()))
)


async def update_appliance(request):
req = await request.json()
if req["puid"] in appliances:
Expand All @@ -57,23 +67,31 @@ async def update_appliance(request):
text=f'{"resultCode":-1,"kvMap":null,"errorCode":404,"errorDesc":"Unknown puid {req["puid"]}"}'
)

def main(args):
filenames = list(filter(lambda f: f[-5:] == ".json", [f for f in listdir(".") if isfile(join(".", f))]))
for filename in filenames:
with (open(filename) as f):
appliance = json.load(f)
appliance["deviceId"] = filename[0:-5]
appliance["puid"] = f"puid{appliance['deviceId']}"
appliance["deviceNickName"] = f'{appliance["deviceNickName"]} ({appliance["deviceTypeCode"]}-{appliance["deviceFeatureCode"]})'
appliances[appliance["puid"]] = appliance

app = web.Application()
app.add_routes([web.post('/accounts.login', login)])
app.add_routes([web.post('/accounts.getJWT', get_jwt)])
app.add_routes([web.post('/oauth/authorize', authorize)])
app.add_routes([web.post('/oauth/token', token)])
app.add_routes([web.get('/appliances', get_appliances)])
app.add_routes([web.post('/appliances', update_appliance)])
web.run_app(app, port=args.port)

filenames = list(filter(lambda f: f[-5:] == ".json", [f for f in listdir(".") if isfile(join(".", f))]))
appliances = {}
for filename in filenames:
with (open(filename) as f):
appliance = json.load(f)
appliance["deviceId"] = filename[0:-5]
appliance["puid"] = f"puid{appliance['deviceId']}"
appliance["deviceNickName"] = f'{appliance["deviceNickName"]} ({appliance["deviceTypeCode"]}-{appliance["deviceFeatureCode"]})'
appliances[appliance["puid"]] = appliance

app = web.Application()
app.add_routes([web.post('/accounts.login', login)])
app.add_routes([web.post('/accounts.getJWT', get_jwt)])
app.add_routes([web.post('/oauth/authorize', authorize)])
app.add_routes([web.post('/oauth/token', token)])
app.add_routes([web.get('/appliances', get_appliances)])
app.add_routes([web.post('/appliances', update_appliance)])
web.run_app(app)
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='ConnectLife API test server')
parser.add_argument('-p', '--port', type=int, default=8080, help='Port on which to serve the web app')
parser.add_argument('-f', '--failure_rate', type=int, default=0, help='Failure rate in % for get appliances')
parser.add_argument('-t', '--timeout_rate', type=int, default=0, help='Timeout rate in % for get appliances')
args = parser.parse_args()
failure_rate = args.failure_rate
timeout_rate = args.timeout_rate
main(args)