- i have implemented single sign on from third party service and wants to do some custom logic inside locust stop test event hook with user details
- inside stop test event hook i am not able to fetch any object of flask session or current_user (https://flask-login.readthedocs.io/en/latest/)
Setting the session object in init listener:
@events.init.add_listener
def locust_init(environment, **kwargs):
print(environment)
if environment.web_ui:
# use the access token to get the user's email address
response = requests.get(USERINFO_URL, headers={
'Authorization': 'Bearer ' + oauth2_token,
'Accept': 'application/json',
})
if response.status_code != 200:
abort(401)
res = response.json()
email = res['email']
# find or create the user in the database
# user = db.session.scalar(db.select(User).where(User.email == email))
# if user is None:
# user = User(email=email, username=email.split('@')[0])
# db.session.add(user)
# db.session.commit()
# log the user in
username=email.split('@')[0]
session['username'] = username
print(gevent.getcurrent())
login_user(User(username))
return redirect(url_for('index'))
fetching session object or current_user in locust stop test even hook:
@events.test_stop.add_listener
def on_test_stop(environment, **kwargs):
print("Stopping the test")
print(current_user.username)
print(session.get('username'))
error stack:
Traceback (most recent call last):
File "C:UsersAdministratorDesktopAkshitlocustlocustevent.py", line 47, in fire
handler(**kwargs)
File "C:UsersAdministratorDesktopAkshitlocustlocustscriptslocustfile.py", line 172, in on_test_stop
print(session.get('username'))
^^^^^^^^.
File "C:UsersAdministratorAppDataLocalpypoetryCachevirtualenvslocust-a2fw8qL2-py3.12Libsite-packageswerkzeuglocal.py", line 3 18, in __get_
obj instance._get_current_object()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersAdministratorAppDataLocalpypoetryCachevirtualenvslocust-a2fw8qL2-py3.12Libsite-packageswerkzeuglocal.py", line 5 19, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
- i assume every event hook runs in a different thread and so it has it’s own application context and request context
- session object is binded to application context or request context and so every event hook has it’s own session object which is not sharable
when i add request context or application context, the variables are accessible but not what i set earlier, it is showing as none
adding request context :
@events.test_stop.add_listener
def on_test_stop(environment, **kwargs):
with environment.web_ui.app.app_context(),environment.web_ui.app.test_request_context():
print("Stopping the test")
print(current_user.username)
print(session.get('username'))
print(gevent.getcurrent())
fetching session and current_user object:
[2024-07-23 04:59:50,164] EC2AMAZ-868PT3A/INFO/locust.main: Starting web interface at http://localhost:8089 (accepting connections from all [2024-07-23 04:59:50,211] EC2AMAZ-868PT3A/INFO/locust.main: Starting Locust 2.29.1.dev39
[2024-07-23 04:59:50,211] EC2AMAZ-868PT3A/INFO/locust.main: Locust is running with the UserClass Picker Enabled
[2024-07-23 04:59:53,587] EC2AMAZ-868PT3A/INFO/locust.runners: Ramping to 1 users at a rate of 1.00 per second
[2024-07-23 04:59:53,587] EC2AMAZ-868PT3A/INFO/locust.runners: All users spawned: {"LocustHttpUser": 1, "QuickstartUser": 0} (1 total users > )
Stopping the test
<flask_login.mixins.AnonymousUserMixin object at 0x0000016A6CED30E0>
None
Is there a way to share flask session object across event hooks? or another approach to persist currently logged in user data form stop test event hook?
akshit kaushik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.