Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warning if tag filtering gets rid of all tasks #2186

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1170,10 +1202,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)
Expand All @@ -1187,7 +1217,6 @@ def task1(self):
def task2(self):
print("task2")
"""
)
with mock_locustfile(content=content) as mocked:
proc = subprocess.Popen(
[
Expand Down Expand Up @@ -1228,6 +1257,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)
Expand Down
2 changes: 2 additions & 0 deletions locust/user/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down