google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

fix: Memory leak - issue while downloading large files

Open hexadivine opened this issue 2 years ago • 5 comments

Added "MediaGenBaseDownload" class in order to fetch chunks using generator and to avoid memory leak issue.

Fixes #1706 🦕

hexadivine avatar Mar 28 '22 06:03 hexadivine

Thanks for the fix @h3x4d1v1n3! Please could you add a test?

parthea avatar Apr 03 '22 10:04 parthea

Sure @parthea

hexadivine avatar Apr 03 '22 11:04 hexadivine

Hi @h3x4d1v1n3 & @parthea we can use the test below:

import unittest
from unittest.mock import patch, MagicMock
import random
import time

class TestMediaGenBaseDownload(unittest.TestCase):

    @patch('random.random')
    @patch('time.sleep')
    def test_next_chunk(self, mock_sleep, mock_random):
        mock_random.return_value = 0
        mock_sleep.return_value = None

        # mock request object
        request = MagicMock()
        request.headers = {'accept': 'application/json'}
        request.uri = 'https://test.com'
        request.http = MagicMock()

        # mock http object
        request.http.request.return_value = (MagicMock(), b'content')

        downloader = MediaGenBaseDownload(request)

        # loop through the generator
        for chunk, status, done in downloader.next_chunk():
            self.assertEqual(chunk, b'content')
            self.assertEqual(done, True)
            self.assertIsInstance(status, MediaDownloadProgress)


In this test, I have mocked the random and time modules. I have also created a mock request object and http object to simulate the response of the request. The test will loop through the generator function next_chunk and check if the output of the generator is as expected.

aria1991 avatar Jan 16 '23 17:01 aria1991

Thanks for the input @aria1991. @parthea - I have added the tests as asked. Thanks.

hexadivine avatar Jan 19 '23 22:01 hexadivine

@parthea could you check the latest comits for tests. Let me know your assessment...

hexadivine avatar Feb 05 '23 17:02 hexadivine

One of my commit do not have verified tag, causing it to fail CLA check. I will hard reset the commit and update the code with new branch.

hexadivine avatar Jul 29 '24 11:07 hexadivine