Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix memory leak while downloading large files #1707 #1736

Closed
wants to merge 9 commits into from
Closed

fix: fix memory leak while downloading large files #1707 #1736

wants to merge 9 commits into from

Conversation

hexadivine
Copy link

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

Fixes #1706 🦕

Added "MediaGenBaseDownload" class in order to fetch chunks using generator and to avoid memory leak issue.
@hexadivine hexadivine requested a review from a team as a code owner March 28, 2022 06:08
@busunkim96 busunkim96 requested a review from parthea March 31, 2022 17:18
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Apr 3, 2022
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Apr 3, 2022
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Apr 3, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Apr 3, 2022
@yoshi-kokoro yoshi-kokoro removed kokoro:run Add this label to force Kokoro to re-run the tests. kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Apr 3, 2022
@parthea
Copy link
Contributor

parthea commented Apr 3, 2022

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

@parthea parthea assigned hexadivine and unassigned parthea Apr 3, 2022
@hexadivine
Copy link
Author

hexadivine commented Apr 3, 2022

Sure @parthea

@aria1991
Copy link

aria1991 commented Jan 16, 2023

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.

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Jan 19, 2023
@hexadivine
Copy link
Author

hexadivine commented Jan 19, 2023

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

@hexadivine
Copy link
Author

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

@hexadivine hexadivine closed this by deleting the head repository Jul 29, 2024
@hexadivine hexadivine changed the title fix: Memory leak - issue while downloading large files NA Jul 29, 2024
@hexadivine hexadivine changed the title NA fix: fix memory leak while downloading large files #1707 Jul 29, 2024
@hexadivine
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak - issue while downloading large files
4 participants