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

Dont suppress StopUser or GreenletExit in on_stop #2486

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
48 changes: 48 additions & 0 deletions locust/test/test_interruptable_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from collections import defaultdict
from unittest import TestCase

from locust import SequentialTaskSet, User, constant, task
from locust.env import Environment
from locust.exception import StopUser


class InterruptableTaskSet(SequentialTaskSet):
counter: defaultdict[str, int] = defaultdict(int)

def on_start(self):
super().on_start()
self.counter["on_start"] += 1

@task
def t1(self):
self.counter["t1"] += 1
self.interrupt(reschedule=False)

@task
def t2(self):
self.counter["t2"] += 1

def on_stop(self):
super().on_stop()
self.counter["on_stop"] += 1
if self.counter["on_stop"] >= 2:
raise StopUser()


class TestInterruptableTask(TestCase):
def setUp(self):
super().setUp()

class InterruptableUser(User):
host = "127.0.0.1"
tasks = [InterruptableTaskSet]
wait_time = constant(0)

self.locust = InterruptableUser(Environment(catch_exceptions=True))

def test_interruptable_task(self):
self.locust.run()
self.assertEqual(InterruptableTaskSet.counter.get("on_start"), 2)
self.assertEqual(InterruptableTaskSet.counter.get("t1"), 2)
self.assertEqual(InterruptableTaskSet.counter.get("t2", 0), 0)
self.assertEqual(InterruptableTaskSet.counter.get("on_stop"), 2)
2 changes: 2 additions & 0 deletions locust/user/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ def run(self):
except InterruptTaskSet as e:
try:
self.on_stop()
except (StopUser, GreenletExit):
raise
except Exception:
logging.error("Uncaught exception in on_stop: \n%s", traceback.format_exc())
if e.reschedule:
Expand Down
Loading