def test_stop_timeout_exit_during_wait(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): task_set = MyTaskSet wait_time = between(1, 1) options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) # sleep to make sure locust has had time to start waiting timeout = gevent.Timeout(short_time) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail("Got Timeout exception. Waiting locusts should stop immediately, even when using stop_timeout.") finally: timeout.cancel()
def test_stop_timeout_with_interrupt(self): short_time = 0.05 class MySubTaskSet(TaskSet): @task def a_task(self): gevent.sleep(0) self.interrupt(reschedule=True) class MyTaskSet(TaskSet): tasks = [MySubTaskSet] class MyTestLocust(Locust): task_set = MyTaskSet options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(0) timeout = gevent.Timeout(short_time) timeout.start() try: runner.quit() runner.greenlet.join() except gevent.Timeout: self.fail("Got Timeout exception. Interrupted locusts should exit immediately during stop_timeout.") finally: timeout.cancel()
def test_spawn_zero_locusts(self): class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): task_set = MyTaskSet min_wait = 100 max_wait = 100 runner = LocalLocustRunner([MyTestLocust], self.options) timeout = gevent.Timeout(2.0) timeout.start() try: runner.start_hatching(0, 1, wait=True) runner.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_stop_timeout_during_on_start(self): short_time = 0.05 class MyTaskSet(TaskSet): finished_on_start = False my_task_run = False def on_start(self): gevent.sleep(short_time) MyTaskSet.finished_on_start = True @task def my_task(self): MyTaskSet.my_task_run = True class MyTestLocust(Locust): task_set = MyTaskSet min_wait = 0 max_wait = 0 options = mocked_options() options.stop_timeout = short_time runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertTrue(MyTaskSet.finished_on_start) self.assertFalse(MyTaskSet.my_task_run)
def test_taskset_setup_method_exception(self): class User(Locust): setup_run_count = 0 task_run_count = 0 locust_error_count = 0 wait_time = constant(1) class task_set(TaskSet): def setup(self): User.setup_run_count += 1 raise Exception("some exception") @task def my_task(self): User.task_run_count += 1 def on_locust_error(*args, **kwargs): User.locust_error_count += 1 events.locust_error += on_locust_error runner = LocalLocustRunner([User], mocked_options()) runner.start_hatching(locust_count=3, hatch_rate=3, wait=False) runner.hatching_greenlet.get(timeout=3) self.assertEqual(1, User.setup_run_count) self.assertEqual(1, User.locust_error_count) self.assertEqual(3, User.task_run_count)
def test_change_user_count_during_hatching(self): class User(Locust): wait_time = constant(1) class task_set(TaskSet): @task def my_task(self): pass runner = LocalLocustRunner([User], mocked_options()) runner.start_hatching(locust_count=10, hatch_rate=5, wait=False) sleep(0.6) runner.start_hatching(locust_count=5, hatch_rate=5, wait=False) runner.hatching_greenlet.join() self.assertEqual(5, len(runner.locusts)) runner.quit()
def test_stop_timeout(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): MyTaskSet.state = "first" gevent.sleep(short_time) MyTaskSet.state = "second" # should only run when run time + stop_timeout is > short_time gevent.sleep(short_time) MyTaskSet.state = "third" # should only run when run time + stop_timeout is > short_time * 2 class MyTestLocust(Locust): task_set = MyTaskSet options = mocked_options() runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.quit() self.assertEqual("first", MyTaskSet.state) options.stop_timeout = short_time / 2 # exit with timeout runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.quit() self.assertEqual("second", MyTaskSet.state) options.stop_timeout = short_time * 2 # allow task iteration to complete, with some margin runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.quit() self.assertEqual("third", MyTaskSet.state)
def test_spawn_zero_locusts(self): class MyTaskSet(TaskSet): @task def my_task(self): pass class MyTestLocust(Locust): task_set = MyTaskSet min_wait = 100 max_wait = 100 runner = LocalLocustRunner([MyTestLocust], self.options) timeout = gevent.Timeout(2.0) timeout.start() try: runner.start_hatching(0, 1, wait=True) runner.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_kill_locusts_with_stop_timeout(self): short_time = 0.05 class MyTaskSet(TaskSet): @task def my_task(self): MyTaskSet.state = "first" gevent.sleep(short_time) MyTaskSet.state = "second" # should only run when run time + stop_timeout is > short_time gevent.sleep(short_time) MyTaskSet.state = "third" # should only run when run time + stop_timeout is > short_time * 2 class MyTestLocust(Locust): task_set = MyTaskSet wait_time = constant(0) options = mocked_options() runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time / 2) runner.kill_locusts(1) self.assertEqual("first", MyTaskSet.state) runner.quit() options.stop_timeout = short_time / 2 # exit with timeout runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) runner.kill_locusts(1) self.assertEqual("second", MyTaskSet.state) runner.quit() options.stop_timeout = short_time * 3 # allow task iteration to complete, with some margin runner = LocalLocustRunner([MyTestLocust], options) runner.start_hatching(1, 1) gevent.sleep(short_time) timeout = gevent.Timeout(short_time * 2) timeout.start() try: runner.kill_locusts(1) runner.locusts.join() except gevent.Timeout: self.fail( "Got Timeout exception. Some locusts must have kept runnining after iteration finish" ) finally: timeout.cancel() self.assertEqual("third", MyTaskSet.state)