pytest-sftpserver icon indicating copy to clipboard operation
pytest-sftpserver copied to clipboard

When using pytest-sftpserver in django test, threads never conclude

Open bthorben opened this issue 2 years ago • 1 comments

Atfinity is using your SFTP Server in our Django tests (without pytest) nicely like so:

mock_sftp_server = SFTPServer()

class TestSFTP(APITestCase):
    fixtures = ['simple.json']
    
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        mock_sftp_server.block_on_close = False
        mock_sftp_server.start()
        print('Started Mock SFTP Server ...')  # noqa
    
    @classmethod
    def tearDownClass(cls) -> None:
        super().tearDownClass()
        mock_sftp_server.server_close()
        mock_sftp_server.shutdown()
        mock_sftp_server.join(0)
        print('Shut down Mock SFTP Server ...')  # noqa

...

However, threads never close. For this, we had to set the threads created by ThreadingMixin to be demon threads and not block like so:

class TestSFTPServer(SFTPServer):
    daemon_threads = True
    block_on_close = False

As this seems to have no negative effect on the functionality, we suggest to do this for SFTPServer directly to make this package useful for django tests. Happy to provide more info how to use this within django tests.

bthorben avatar Feb 08 '23 07:02 bthorben

@bthorben thanks for this hint. I am in the process of upgrading from python38 to python310 in a project that uses this library. I found that setting the above in conftest.py, where I specify the sftpserver in a fixture, resolves the hanging threads.

@pytest.fixture(scope="function")
def sftp_fixture(sftpserver):
    sftpserver.daemon_threads = True
    sftpserver.block_on_close = False
    yield

Best of luck with your PR.

628567 avatar May 02 '23 05:05 628567