Skip to content

Commit

Permalink
Merge pull request #1641 from locustio/fix-1638
Browse files Browse the repository at this point in the history
Fix bug causing test_stop event to be fired twice in master node
  • Loading branch information
cyberw authored Dec 1, 2020
2 parents 12ff1d8 + 15c5ddc commit ab72014
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
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

0 comments on commit ab72014

Please sign in to comment.