miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.31k stars 888 forks source link

Socket.IO test client does not run background task #2012

Closed al-awsdev4 closed 9 months ago

al-awsdev4 commented 9 months ago

Describe the bug We use test case to check custom heartbeat function using background task:

  @socketio.on('connect', namespace='/namespace')
  def handle_connect():
      def background_task():
          app.logger.info('Background task started') # should show log message
          heartbeat_handler() #  should save timestamp to Redis for current sid

      socketio.start_background_task(background_task)

test_heartbeat_handler.py

...
socketio_test_client.connect(namespace='/namespace')
assert socketio_test_client.is_connected(namespace='/namespace') 
# check Redis for updated timestamp
...

Expected behavior Log message and updated timestamp are presented.

Additional context It does not work only for test client. Recently it worked on 5.3.4 version. Update to 5.3.6 does not help.

miguelgrinberg commented 9 months ago

I cannot reproduce. I have added a test based on your code to this repository to confirm that background tasks are working fine. See https://github.com/miguelgrinberg/Flask-SocketIO/commit/84562cf876343150bb1c0b2431caf75b280e82cc.

Maybe your background task is crashing for some reason and does not run to completion?

al-awsdev4 commented 9 months ago

@miguelgrinberg thank you for your quick response. It does not work even my background job contains just logger call:

@socketio.on('connect', namespace='/namespace')
  def handle_connect():
      def background_task():
          app.logger.info('Background task started')

      socketio.start_background_task(background_task)

Additional info: platform linux -- Python 3.8.11, pytest-6.2.5

miguelgrinberg commented 9 months ago

You can confirm yourself that a similar test just added to this repository passes. I'm really not sure how I can help, given that I cannot debug an issue that I cannot reproduce. You can compare your testing procedures against mine in this repository to help you determine what is causing this.

al-awsdev4 commented 9 months ago

@miguelgrinberg got it. I'll try to compare and debug more deeply. I'll send feedback as soon as I find the reason.

al-awsdev4 commented 9 months ago

@miguelgrinberg, our application uses eventlet async mode, so that's one difference when issue is reproducible.

app = Flask(__name__)

socketio = SocketIO(
    logger=True,
    engineio_logger=True,
    async_mode='eventlet'
)

socketio.init_app(app=app)

I tried different versions of eventlet but it does not help.

miguelgrinberg commented 9 months ago

@al-awsdev4 If you are using eventlet during your test run you need to ensure that all the packages that you use to support your tests are compatible with eventlet style concurrency. Not sure which test runner you are using, but I have no idea if unittest, pytest, nose, etc. can be used safely with eventlet. Even if you were able to make your tests work fine in the past, supporting eventlet for unit tests has never been a goal.

al-awsdev4 commented 9 months ago

@miguelgrinberg, thank you for clarifying. Issue can be closed since it's not related to Socket.IO test client.