Skip to content

Commit

Permalink
Change mock server (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
yumiguan authored Dec 2, 2022
1 parent 847f4ad commit d66fe51
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
21 changes: 21 additions & 0 deletions lyrebird/application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import webbrowser

from flask import jsonify
from functools import reduce

Expand Down Expand Up @@ -50,6 +52,17 @@ def make_fail_response(msg, **kwargs):
encoders_decoders = None


def start_server_without_mock():
for name in server:
if name == 'mock':
continue
server[name].start()


def start_mock_server():
server['mock'].start()


def start_server():
for name in server:
server[name].start()
Expand Down Expand Up @@ -86,6 +99,8 @@ def root_dir():
return _cm.ROOT


NO_BROSWER = False

# --------- Lyrebird status ---------
'''
[INITING] --> /run lyrebird main method/ --> [READY]
Expand Down Expand Up @@ -135,9 +150,15 @@ def status_listener(event):

is_all_status_checkpoints_ok = reduce(lambda x, y: x and y, status_checkpoints.values())
if is_all_status_checkpoints_ok:
# Set global status
global status
status = 'READY'

server['event'].publish('system', {'action': 'init_module', 'status': 'READY', 'module': 'all'})

# auto open web browser
if not NO_BROSWER:
webbrowser.open(f'http://localhost:{config["mock.port"]}')

def process_status_listener():
server['event'].subscribe('system', status_listener)
Expand Down
13 changes: 9 additions & 4 deletions lyrebird/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import socket
import threading
import traceback
import webbrowser
from pathlib import Path

from packaging.version import parse as vparse
Expand Down Expand Up @@ -188,16 +187,19 @@ def run(args: argparse.Namespace):
application.status_checkpoints['mitm_proxy'] = False
application.server['proxy'] = LyrebirdProxyServer()

application.server['mock'] = LyrebirdMockServer()
application.server['extra.mock'] = ExtraMockServer()
application.server['db'] = LyrebirdDatabaseServer(path=args.database)
application.server['plugin'] = PluginManager()
application.server['checker'] = LyrebirdCheckerServer()

# Mock mush init after other servers
application.server['mock'] = LyrebirdMockServer()

# handle progress message
application.process_status_listener()

application.start_server()
# Start server without mock server, mock server must start after all blueprint is done
application.start_server_without_mock()

# int statistics reporter
application.reporter = reporter.Reporter()
Expand All @@ -222,9 +224,12 @@ def run(args: argparse.Namespace):
if args.script:
application.server['checker'].load_scripts(args.script)

# Start server without mock server, mock server must start after all blueprint is done
application.start_mock_server()

# auto open web browser
if not args.no_browser:
webbrowser.open(f'http://localhost:{application.config["mock.port"]}')
application.NO_BROSWER = args.no_browser

# main process is ready, publish system event
application.status_ready()
Expand Down
5 changes: 4 additions & 1 deletion lyrebird/mock/extra_mock_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ async def proxy(context: LyrebirdProxyContext):

async def forward(context: LyrebirdProxyContext):
logger.info(f'forward {context.forward_url}')
return await send_request(context, context.forward_url)
try:
return await send_request(context, context.forward_url)
except Exception:
logger.debug(f'Forward request error, it may caused by mock server not ready. URL: {context.forward_url}')


async def req_handler(request: web.Request):
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
IVERSION = (2, 10, 2)
IVERSION = (2, 10, 3)
VERSION = ".".join(str(i) for i in IVERSION)
LYREBIRD = "Lyrebird " + VERSION

0 comments on commit d66fe51

Please sign in to comment.