Skip to content

Commit

Permalink
Merge pull request #2186 from locustio/log-warning-if-tag-filtering-g…
Browse files Browse the repository at this point in the history
…ets-rid-of-all-tasks

Log warning if tag filtering gets rid of all tasks
  • Loading branch information
cyberw committed Sep 7, 2022
2 parents 96986de + 3a7a478 commit 1329075
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 35 additions & 5 deletions locust/test/test_main.py
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
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

0 comments on commit 1329075

Please sign in to comment.