@patch('requests.Session.close', autospec=True)
    def test_caller_managed_connections(self, mock_close):
        connection = mail.get_connection()
        connection.open()
        mail.send_mail('Subject 1', 'body', 'from@example.com', ['to@example.com'], connection=connection)
        session1 = self.mock_post.call_args[0]
        self.assertEqual(mock_close.call_count, 0)  # shouldn't be closed yet

        mail.send_mail('Subject 2', 'body', 'from@example.com', ['to@example.com'], connection=connection)
        self.assertEqual(mock_close.call_count, 0)  # still shouldn't be closed
        session2 = self.mock_post.call_args[0]
        self.assertEqual(session1, session2)  # should have reused same session

        connection.close()
        self.assertEqual(mock_close.call_count, 1)
Calling code can created long-lived connection that it opens and closes
dataset/ETHPy150Open brack3t/Djrill/djrill/tests/test_mandrill_session_sharing.py/DjrillSessionSharingTests.test_caller_managed_connections