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

Fix running the web UI with class defined hosts #1956

Merged
merged 1 commit into from
Dec 9, 2021
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
6 changes: 3 additions & 3 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def start(self, user_count: int, spawn_rate: float, wait: bool = False):
raise ValueError("wait is True but the amount of users to add is greater than the spawn rate")

for user_class in self.user_classes:
if self.environment.host is not None:
if self.environment.host:
user_class.host = self.environment.host

if self.state != STATE_INIT and self.state != STATE_STOPPED:
Expand Down Expand Up @@ -674,7 +674,7 @@ def start(self, user_count: int, spawn_rate: float, **kwargs) -> None:
return

for user_class in self.user_classes:
if self.environment.host is not None:
if self.environment.host:
user_class.host = self.environment.host

self.spawn_rate = spawn_rate
Expand Down Expand Up @@ -1085,7 +1085,7 @@ def start_worker(self, user_classes_count: Dict[str, int], **kwargs):
self.worker_state = STATE_SPAWNING

for user_class in self.user_classes:
if self.environment.host is not None:
if self.environment.host:
user_class.host = self.environment.host

user_classes_spawn_count = {}
Expand Down
32 changes: 32 additions & 0 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,38 @@ def my_task(self):

runner.quit()

def test_host_class_attribute_from_web(self):
"""If host is left empty from the webUI, we should not use it"""

class MyUser1(User):
host = "https://host1.com"

@task
def my_task(self):
pass

class MyUser2(User):
host = "https://host2.com"

@task
def my_task(self):
pass

opts = mocked_options()
# If left empty on the web, we get an empty string as host
opts.host = ""
environment = create_environment([MyUser1, MyUser2], opts)
runner = LocalRunner(environment)
# Start the runner to trigger problematic code
runner.start(user_count=2, spawn_rate=1, wait=False)
runner.spawning_greenlet.join()

# Make sure we did not overwrite the host variable
self.assertEqual(MyUser1.host, "https://host1.com")
self.assertEqual(MyUser2.host, "https://host2.com")

runner.quit()

def test_custom_message(self):
class MyUser(User):
wait_time = constant(1)
Expand Down