Locust stop test event hook not able to fetch flask session

  • 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?

New contributor

akshit kaushik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật