def test_spawn_zero_locusts(self): class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestUser(User): tasks = [MyTaskSet] wait_time = constant(0.1) environment = Environment(user_classes=[MyTestUser]) runner = LocalRunner(environment) timeout = gevent.Timeout(2.0) timeout.start() try: runner.start(0, 1, wait=True) runner.hatching_greenlet.join() except gevent.Timeout: self.fail( "Got Timeout exception. A locust seems to have been spawned, even though 0 was specified." ) finally: timeout.cancel()
def test_start_event(self): class MyUser(User): wait_time = constant(1) task_run_count = 0 @task def my_task(self): MyUser.task_run_count += 1 test_start_run = [0] environment = Environment(user_classes=[MyUser]) def on_test_start(*args, **kwargs): test_start_run[0] += 1 environment.events.test_start.add_listener(on_test_start) runner = LocalRunner(environment) runner.start(user_count=3, hatch_rate=3, wait=False) runner.hatching_greenlet.get(timeout=3) self.assertEqual(1, test_start_run[0]) self.assertEqual(3, MyUser.task_run_count)
def test_no_reset_stats(self): class MyUser(User): wait_time = constant(0) @task class task_set(TaskSet): @task def my_task(self): self.user.environment.events.request_success.fire( request_type="GET", name="/test", response_time=666, response_length=1337, ) sleep(2) environment = Environment(reset_stats=False, user_classes=[MyUser]) runner = LocalRunner(environment) runner.start(user_count=6, hatch_rate=12, wait=False) sleep(0.25) self.assertGreaterEqual( runner.stats.get("/test", "GET").num_requests, 3) sleep(0.3) self.assertEqual(6, runner.stats.get("/test", "GET").num_requests) runner.quit()
def test_change_user_count_during_hatching(self): class MyUser(User): wait_time = constant(1) @task def my_task(self): pass environment = Environment(user_classes=[MyUser]) runner = LocalRunner(environment) runner.start(user_count=10, hatch_rate=5, wait=False) sleep(0.6) runner.start(user_count=5, hatch_rate=5, wait=False) runner.hatching_greenlet.join() self.assertEqual(5, len(runner.user_greenlets)) runner.quit()
def test_exception_is_catched(self): """ Test that exceptions are stored, and execution continues """ class MyTaskSet(TaskSet): def __init__(self, *a, **kw): super(MyTaskSet, self).__init__(*a, **kw) self._task_queue = [ { "callable": self.will_error, "args": [], "kwargs": {} }, { "callable": self.will_stop, "args": [], "kwargs": {} }, ] @task(1) def will_error(self): raise HeyAnException(":(") @task(1) def will_stop(self): raise StopUser() class MyUser(User): wait_time = constant(0.01) tasks = [MyTaskSet] # set config to catch exceptions in locust users self.environment.catch_exceptions = True self.environment.user_classes = [MyUser] runner = LocalRunner(self.environment) l = MyUser(self.environment) # make sure HeyAnException isn't raised l.run() l.run() # make sure we got two entries in the error log self.assertEqual(2, len(self.mocked_log.error)) # make sure exception was stored self.assertEqual(1, len(runner.exceptions)) hash_key, exception = runner.exceptions.popitem() self.assertTrue("traceback" in exception) self.assertTrue("HeyAnException" in exception["traceback"]) self.assertEqual(2, exception["count"])
def test_stop_event_stop_and_quit(self): class MyUser(User): wait_time = constant(1) @task def my_task(self): pass test_stop_run = [0] environment = Environment(user_classes=[MyUser]) def on_test_stop(*args, **kwargs): test_stop_run[0] += 1 environment.events.test_stop.add_listener(on_test_stop) runner = LocalRunner(environment) runner.start(user_count=3, hatch_rate=3, wait=False) self.assertEqual(0, test_stop_run[0]) runner.stop() runner.quit() self.assertEqual(1, test_stop_run[0])
def test_cpu_warning(self): _monitor_interval = runners.CPU_MONITOR_INTERVAL runners.CPU_MONITOR_INTERVAL = 2.0 try: class CpuUser(User): wait_time = constant(0.001) @task def cpu_task(self): for i in range(1000000): _ = 3 / 2 environment = Environment(user_classes=[CpuUser]) runner = LocalRunner(environment) self.assertFalse(runner.cpu_warning_emitted) runner.spawn_users(1, 1, wait=False) sleep(2.5) runner.quit() self.assertTrue(runner.cpu_warning_emitted) finally: runners.CPU_MONITOR_INTERVAL = _monitor_interval
def test_gracefully_handle_exceptions_in_listener(self): class MyUser(User): wait_time = constant(1) @task def my_task(self): pass test_stop_run = [0] environment = Environment(user_classes=[User]) def on_test_stop_ok(*args, **kwargs): test_stop_run[0] += 1 def on_test_stop_fail(*args, **kwargs): assert 0 environment.events.test_stop.add_listener(on_test_stop_ok) environment.events.test_stop.add_listener(on_test_stop_fail) environment.events.test_stop.add_listener(on_test_stop_ok) runner = LocalRunner(environment) runner.start(user_count=3, hatch_rate=3, wait=False) self.assertEqual(0, test_stop_run[0]) runner.stop() self.assertEqual(2, test_stop_run[0])