Skip to content

Commit

Permalink
Fix running the web UI with class defined hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed Dec 9, 2021
1 parent 581e1de commit d170631
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
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

0 comments on commit d170631

Please sign in to comment.