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 bug causing test_stop event to be fired twice in master node #1641

Merged
merged 2 commits into from
Dec 1, 2020
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
7 changes: 2 additions & 5 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,8 @@ def stop(self):
self.environment.events.test_stop.fire(environment=self.environment)

def quit(self):
if self.state not in [STATE_INIT, STATE_STOPPED, STATE_STOPPING]:
logger.debug("Quitting...")
# fire test_stop event if state isn't already stopped
self.environment.events.test_stop.fire(environment=self.environment)

self.stop()
logger.debug("Quitting...")
for client in self.clients.all:
logger.debug("Sending quit message to client %s" % (client.id))
self.server.send_to_client(Message("quit", None, client.id))
Expand Down
55 changes: 55 additions & 0 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,61 @@ def incr_stats(l):
"For some reason the master node's stats has not come in",
)

def test_test_stop_event(self):
class TestUser(User):
wait_time = constant(0.1)

@task
def my_task(l):
pass

with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3):
# start a Master runner
master_env = Environment(user_classes=[TestUser])
test_stop_count = {"master": 0, "worker": 0}

@master_env.events.test_stop.add_listener
def _(*args, **kwargs):
test_stop_count["master"] += 1

master = master_env.create_master_runner("*", 0)
sleep(0)
# start a Worker runner
worker_env = Environment(user_classes=[TestUser])

@worker_env.events.test_stop.add_listener
def _(*args, **kwargs):
test_stop_count["worker"] += 1

worker = worker_env.create_worker_runner("127.0.0.1", master.server.port)

# give worker time to connect
sleep(0.1)
# issue start command that should trigger TestUsers to be spawned in the Workers
master.start(2, spawn_rate=1000)
sleep(0.1)
# check that worker nodes have started locusts
self.assertEqual(2, worker.user_count)
# give time for users to generate stats, and stats to be sent to master
sleep(0.1)
master_env.events.quitting.fire(environment=master_env, reverse=True)
master.quit()
sleep(0.1)
# make sure users are killed
self.assertEqual(0, worker.user_count)

# check the test_stop event was called one time in master and zero times in workder
self.assertEqual(
1,
test_stop_count["master"],
"The test_stop event was not called exactly one time in the master node",
)
self.assertEqual(
0,
test_stop_count["worker"],
"The test_stop event was called in the worker node",
)

def test_distributed_shape(self):
"""
Full integration test that starts both a MasterRunner and three WorkerRunner instances
Expand Down