def setUp(self): # Prevent args passed to test runner from being passed to Locust del sys.argv[1:] locust.events = Events() self.environment = Environment(events=locust.events, catch_exceptions=False) self.runner = LocustRunner(self.environment, []) # When running the tests in Python 3 we get warnings about unclosed sockets. # This causes tests that depends on calls to sys.stderr to fail, so we'll # suppress those warnings. For more info see: # https://github.com/requests/requests/issues/1882 try: warnings.filterwarnings(action="ignore", message="unclosed <socket object", category=ResourceWarning) except NameError: # ResourceWarning doesn't exist in Python 2, but since the warning only appears # on Python 3 we don't need to mock it. Instead we can happily ignore the exception pass # set up mocked logging handler self._logger_class = MockedLoggingHandler() self._logger_class.setLevel(logging.INFO) self._root_log_handlers = [h for h in logging.root.handlers] [logging.root.removeHandler(h) for h in logging.root.handlers] logging.root.addHandler(self._logger_class) logging.root.setLevel(logging.INFO) self.mocked_log = MockedLoggingHandler
def test_weight_locusts(self): maxDiff = 2048 class BaseLocust(Locust): pass class L1(BaseLocust): weight = 101 class L2(BaseLocust): weight = 99 class L3(BaseLocust): weight = 100 runner = LocustRunner(Environment(options=mocked_options()), locust_classes=[L1, L2, L3]) self.assert_locust_class_distribution({ L1: 10, L2: 9, L3: 10 }, runner.weight_locusts(29)) self.assert_locust_class_distribution({ L1: 10, L2: 10, L3: 10 }, runner.weight_locusts(30)) self.assert_locust_class_distribution({ L1: 11, L2: 10, L3: 10 }, runner.weight_locusts(31))
def test_weight_locusts(self): maxDiff = 2048 class BaseLocust(Locust): class task_set(TaskSet): pass class L1(BaseLocust): weight = 101 class L2(BaseLocust): weight = 99 class L3(BaseLocust): weight = 100 runner = LocustRunner([L1, L2, L3], mocked_options()) self.assert_locust_class_distribution({ L1: 10, L2: 9, L3: 10 }, runner.weight_locusts(29)) self.assert_locust_class_distribution({ L1: 10, L2: 10, L3: 10 }, runner.weight_locusts(30)) self.assert_locust_class_distribution({ L1: 11, L2: 10, L3: 10 }, runner.weight_locusts(31))
def test_host_value_from_locust_class(self): class MyLocust(Locust): host = "http://example.com" runners.locust_runner = LocustRunner([MyLocust], options=self.options) response = requests.get("http://127.0.0.1:%i/" % self.web_port) self.assertEqual(200, response.status_code) self.assertIn("http://example.com", response.content.decode("utf-8")) self.assertNotIn("setting this will override the host on all Locust classes", response.content.decode("utf-8"))
def setUp(self): super(TestWebUI, self).setUp() stats.global_stats.clear_all() runners.locust_runner = LocustRunner([], None, None, None, None) self._web_ui_server = wsgi.WSGIServer(('127.0.0.1', 0), web.app, log=None) gevent.spawn(lambda: self._web_ui_server.serve_forever()) gevent.sleep(0.01) self.web_port = self._web_ui_server.server_port
def setUp(self): super(TestWebUIAuth, self).setUp() parser = get_parser(default_config_files=[]) options = parser.parse_args(["--web-auth", "john:doe"]) self.runner = LocustRunner(self.environment) self.stats = self.runner.stats self.web_ui = self.environment.create_web_ui( "127.0.0.1", 0, auth_credentials=options.web_auth) self.web_ui.app.view_functions["request_stats"].clear_cache() gevent.sleep(0.01) self.web_port = self.web_ui.server.server_port
def test_weight_locusts_fewer_amount_than_locust_classes(self): class BaseLocust(Locust): class task_set(TaskSet): pass class L1(BaseLocust): weight = 101 class L2(BaseLocust): weight = 99 class L3(BaseLocust): weight = 100 runner = LocustRunner([L1, L2, L3], mocked_options()) self.assertEqual(1, len(runner.weight_locusts(1))) self.assert_locust_class_distribution({L1:1}, runner.weight_locusts(1))
def setUp(self): super(TestWebUI, self).setUp() stats.global_stats.clear_all() parser = parse_options(default_config_files=[])[0] self.options = parser.parse_args([]) runners.locust_runner = LocustRunner([], self.options) web.request_stats.clear_cache() self._web_ui_server = pywsgi.WSGIServer(('127.0.0.1', 0), web.app, log=None) gevent.spawn(lambda: self._web_ui_server.serve_forever()) gevent.sleep(0.01) self.web_port = self._web_ui_server.server_port
def setUp(self): super(TestWebUI, self).setUp() parser = get_parser(default_config_files=[]) self.environment.options = parser.parse_args([]) self.runner = LocustRunner(self.environment, []) self.stats = self.runner.stats self.web_ui = WebUI(self.environment) self.web_ui.app.view_functions["request_stats"].clear_cache() gevent.spawn(lambda: self.web_ui.start("127.0.0.1", 0)) gevent.sleep(0.01) self.web_port = self.web_ui.server.server_port
def test_kill_locusts(self): triggered = [False] class BaseLocust(Locust): wait_time = constant(1) class task_set(TaskSet): @task def trigger(self): triggered[0] = True runner = LocustRunner([BaseLocust], mocked_options()) runner.spawn_locusts(2, wait=False) self.assertEqual(2, len(runner.locusts)) g1 = list(runner.locusts)[0] g2 = list(runner.locusts)[1] runner.kill_locusts(2) self.assertEqual(0, len(runner.locusts)) self.assertTrue(g1.dead) self.assertTrue(g2.dead) self.assertTrue(triggered[0])