mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 16:40:50 +08:00
fix 0c55b83c1a: import locust may lead to RecursionError
This commit is contained in:
@@ -1,13 +1,10 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# monkey patch at beginning to avoid RecursionError when running locust.
|
# monkey patch ssl at beginning to avoid RecursionError when running locust.
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
if not monkey.is_module_patched('socket'):
|
if not monkey.is_module_patched('ssl'):
|
||||||
print("========== monkey patch all ==========")
|
monkey.patch_ssl()
|
||||||
monkey.patch_all()
|
|
||||||
else:
|
|
||||||
print("========== monkey patched ==========")
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
+15
-16
@@ -1,22 +1,16 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
import argparse
|
|
||||||
import multiprocessing
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from httprunner import logger
|
|
||||||
from httprunner.__about__ import __description__, __version__
|
|
||||||
from httprunner.api import HttpRunner
|
|
||||||
from httprunner.compat import is_py2
|
|
||||||
from httprunner.utils import (create_scaffold, get_python2_retire_msg,
|
|
||||||
prettify_json_file, validate_json_file)
|
|
||||||
|
|
||||||
|
|
||||||
def main_hrun():
|
def main_hrun():
|
||||||
""" API test: parse command line options and run commands.
|
""" API test: parse command line options and run commands.
|
||||||
"""
|
"""
|
||||||
|
import argparse
|
||||||
|
from httprunner import logger
|
||||||
|
from httprunner.__about__ import __description__, __version__
|
||||||
|
from httprunner.api import HttpRunner
|
||||||
|
from httprunner.compat import is_py2
|
||||||
|
from httprunner.utils import (create_scaffold, get_python2_retire_msg,
|
||||||
|
prettify_json_file, validate_json_file)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=__description__)
|
parser = argparse.ArgumentParser(description=__description__)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-V', '--version', dest='version', action='store_true',
|
'-V', '--version', dest='version', action='store_true',
|
||||||
@@ -92,9 +86,14 @@ def main_hrun():
|
|||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def main_locust():
|
def main_locust():
|
||||||
""" Performance test with locust: parse command line options and run commands.
|
""" Performance test with locust: parse command line options and run commands.
|
||||||
"""
|
"""
|
||||||
|
import multiprocessing
|
||||||
|
import sys
|
||||||
|
from httprunner import logger
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from httprunner import locusts
|
from httprunner import locusts
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -108,7 +107,7 @@ def main_locust():
|
|||||||
sys.argv.extend(["-h"])
|
sys.argv.extend(["-h"])
|
||||||
|
|
||||||
if sys.argv[1] in ["-h", "--help", "-V", "--version"]:
|
if sys.argv[1] in ["-h", "--help", "-V", "--version"]:
|
||||||
locusts.main()
|
locusts.start_locust_main()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# set logging level
|
# set logging level
|
||||||
@@ -174,4 +173,4 @@ def main_locust():
|
|||||||
sys.argv.pop(processes_index)
|
sys.argv.pop(processes_index)
|
||||||
locusts.run_locusts_with_processes(sys.argv, processes_count)
|
locusts.run_locusts_with_processes(sys.argv, processes_count)
|
||||||
else:
|
else:
|
||||||
locusts.main()
|
locusts.start_locust_main()
|
||||||
|
|||||||
+11
-3
@@ -7,7 +7,6 @@ import sys
|
|||||||
|
|
||||||
from httprunner.logger import color_print
|
from httprunner.logger import color_print
|
||||||
from httprunner import loader
|
from httprunner import loader
|
||||||
from locust.main import main
|
|
||||||
|
|
||||||
|
|
||||||
def parse_locustfile(file_path):
|
def parse_locustfile(file_path):
|
||||||
@@ -31,6 +30,7 @@ def parse_locustfile(file_path):
|
|||||||
|
|
||||||
return locustfile_path
|
return locustfile_path
|
||||||
|
|
||||||
|
|
||||||
def gen_locustfile(testcase_file_path):
|
def gen_locustfile(testcase_file_path):
|
||||||
""" generate locustfile from template.
|
""" generate locustfile from template.
|
||||||
"""
|
"""
|
||||||
@@ -49,17 +49,25 @@ def gen_locustfile(testcase_file_path):
|
|||||||
|
|
||||||
return locustfile_path
|
return locustfile_path
|
||||||
|
|
||||||
|
|
||||||
|
def start_locust_main():
|
||||||
|
from locust.main import main
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
def start_master(sys_argv):
|
def start_master(sys_argv):
|
||||||
sys_argv.append("--master")
|
sys_argv.append("--master")
|
||||||
sys.argv = sys_argv
|
sys.argv = sys_argv
|
||||||
main()
|
start_locust_main()
|
||||||
|
|
||||||
|
|
||||||
def start_slave(sys_argv):
|
def start_slave(sys_argv):
|
||||||
if "--slave" not in sys_argv:
|
if "--slave" not in sys_argv:
|
||||||
sys_argv.extend(["--slave"])
|
sys_argv.extend(["--slave"])
|
||||||
|
|
||||||
sys.argv = sys_argv
|
sys.argv = sys_argv
|
||||||
main()
|
start_locust_main()
|
||||||
|
|
||||||
|
|
||||||
def run_locusts_with_processes(sys_argv, processes_count):
|
def run_locusts_with_processes(sys_argv, processes_count):
|
||||||
processes = []
|
processes = []
|
||||||
|
|||||||
+5
-14
@@ -3,14 +3,9 @@
|
|||||||
from unittest.case import SkipTest
|
from unittest.case import SkipTest
|
||||||
|
|
||||||
from httprunner import exceptions, logger, response, utils
|
from httprunner import exceptions, logger, response, utils
|
||||||
from httprunner.client import HttpSession as HttpRunnerSession
|
from httprunner.client import HttpSession
|
||||||
from httprunner.context import SessionContext
|
from httprunner.context import SessionContext
|
||||||
|
|
||||||
try:
|
|
||||||
from locust.clients import HttpSession as LocustHttpSession
|
|
||||||
except ImportError:
|
|
||||||
LocustHttpSession = None
|
|
||||||
|
|
||||||
|
|
||||||
class Runner(object):
|
class Runner(object):
|
||||||
""" Running testcases.
|
""" Running testcases.
|
||||||
@@ -63,7 +58,7 @@ class Runner(object):
|
|||||||
# testcase teardown hooks
|
# testcase teardown hooks
|
||||||
self.testcase_teardown_hooks = config.get("teardown_hooks", [])
|
self.testcase_teardown_hooks = config.get("teardown_hooks", [])
|
||||||
|
|
||||||
self.http_client_session = http_client_session or HttpRunnerSession(base_url)
|
self.http_client_session = http_client_session or HttpSession(base_url)
|
||||||
self.session_context = SessionContext(self.functions)
|
self.session_context = SessionContext(self.functions)
|
||||||
|
|
||||||
if testcase_setup_hooks:
|
if testcase_setup_hooks:
|
||||||
@@ -76,7 +71,7 @@ class Runner(object):
|
|||||||
def __clear_test_data(self):
|
def __clear_test_data(self):
|
||||||
""" clear request and response data
|
""" clear request and response data
|
||||||
"""
|
"""
|
||||||
if not isinstance(self.http_client_session, HttpRunnerSession):
|
if not isinstance(self.http_client_session, HttpSession):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.validation_results = []
|
self.validation_results = []
|
||||||
@@ -85,7 +80,7 @@ class Runner(object):
|
|||||||
def __get_test_data(self):
|
def __get_test_data(self):
|
||||||
""" get request/response data and validate results
|
""" get request/response data and validate results
|
||||||
"""
|
"""
|
||||||
if not isinstance(self.http_client_session, HttpRunnerSession):
|
if not isinstance(self.http_client_session, HttpSession):
|
||||||
return
|
return
|
||||||
|
|
||||||
meta_data = self.http_client_session.meta_data
|
meta_data = self.http_client_session.meta_data
|
||||||
@@ -282,11 +277,7 @@ class Runner(object):
|
|||||||
base_url = config.get("base_url")
|
base_url = config.get("base_url")
|
||||||
|
|
||||||
# each testcase should have individual session.
|
# each testcase should have individual session.
|
||||||
if LocustHttpSession and isinstance(self.http_client_session, LocustHttpSession):
|
http_client_session = self.http_client_session.__class__(base_url)
|
||||||
http_client_session = LocustHttpSession(base_url)
|
|
||||||
else:
|
|
||||||
http_client_session = HttpRunnerSession(base_url)
|
|
||||||
|
|
||||||
test_runner = Runner(config, self.functions, http_client_session)
|
test_runner = Runner(config, self.functions, http_client_session)
|
||||||
|
|
||||||
tests = testcase_dict.get("tests", [])
|
tests = testcase_dict.get("tests", [])
|
||||||
|
|||||||
Reference in New Issue
Block a user