From a39389f9deb1c59321ddd9ff5ef67c71176abfab Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Tue, 6 Sep 2022 23:37:26 +0200 Subject: [PATCH 1/3] test_main.py/test_distributed_tags: actually detect an error that was happening --- locust/test/test_main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index 680af8a607..7742f76656 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1228,6 +1228,7 @@ def task2(self): ) stdout, stderr = proc.communicate() stdout_worker, stderr_worker = proc_worker.communicate() + self.assertNotIn("ERROR", stderr_worker) self.assertIn("task1", stdout_worker) self.assertNotIn("task2", stdout_worker) self.assertNotIn("Traceback", stderr) From eb2018c9584cfa70f60cce2d13b8f4f410d91d34 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Tue, 6 Sep 2022 23:38:33 +0200 Subject: [PATCH 2/3] test_main.py/test_distributed_tags: fix error in test --- locust/test/test_main.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index 7742f76656..cab3b2c416 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1170,10 +1170,8 @@ def on_test_stop(environment, **kwargs): self.assertEqual(0, proc_worker.returncode) def test_distributed_tags(self): - content = ( - MOCK_LOCUSTFILE_CONTENT - + """ -from locust import tag + content = """ +from locust import HttpUser, TaskSet, task, between, LoadTestShape, tag class SecondUser(HttpUser): host = "http://127.0.0.1:8089" wait_time = between(0, 0.1) @@ -1187,7 +1185,6 @@ def task1(self): def task2(self): print("task2") """ - ) with mock_locustfile(content=content) as mocked: proc = subprocess.Popen( [ From 3a7a478961449969c6f25b42f68cdf8702dcdb52 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Tue, 6 Sep 2022 23:39:47 +0200 Subject: [PATCH 3/3] --tags: Log a warning if no tasks remain on a User/TaskSet after filtering --- locust/test/test_main.py | 32 ++++++++++++++++++++++++++++++++ locust/user/task.py | 2 ++ 2 files changed, 34 insertions(+) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index cab3b2c416..7310262004 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1077,6 +1077,38 @@ def test_error_when_locustfiles_directory_is_empty(self): self.assertIn(f"Could not find any locustfiles in directory '{temp_dir}'", stderr) self.assertEqual(1, proc.returncode) + def test_error_when_no_tasks_match_tags(self): + content = """ +from locust import HttpUser, TaskSet, task, constant, LoadTestShape, tag +class MyUser(HttpUser): + host = "http://127.0.0.1:8089" + wait_time = constant(1) + @tag("tag1") + @task + def task1(self): + print("task1") + """ + with mock_locustfile(content=content) as mocked: + proc = subprocess.Popen( + [ + "locust", + "-f", + mocked.file_path, + "--headless", + "-t", + "1", + "--tags", + "tag2", + ], + stdout=PIPE, + stderr=PIPE, + text=True, + ) + stdout, stderr = proc.communicate() + self.assertIn("MyUser had no tasks left after filtering", stderr) + self.assertIn("No tasks defined on MyUser", stderr) + self.assertEqual(1, proc.returncode) + class DistributedIntegrationTests(ProcessIntegrationTest): def test_expect_workers(self): diff --git a/locust/user/task.py b/locust/user/task.py index 4ce76ba977..40906f96ba 100644 --- a/locust/user/task.py +++ b/locust/user/task.py @@ -208,6 +208,8 @@ def filter_tasks_by_tags( checked[task] = passing task_holder.tasks = new_tasks + if not new_tasks: + logging.warning(f"{task_holder.__name__} had no tasks left after filtering, instantiating it will fail!") class TaskSetMeta(type):