summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladyslav Drok <vdrok@mirantis.com>2020-06-08 18:39:36 +0200
committerHervé Beraud <hberaud@redhat.com>2020-06-10 18:33:09 +0200
commit4cf03f0016693d066bdd6855bcfe234347f00426 (patch)
treeb68ea62421d238188b4db0301e978a3b1e955a6f
parent127da507884133363028bbdd22b1aa9090de652e (diff)
downloadoslo-utils-stable/stein.tar.gz
Release greenthread when computing checksumstein-em3.40.7stable/stein
Having time.sleep(0) after reading each block of data for computing checksum will allow other greenthreads to run. Closes-Bug: 1882569 Change-Id: I6e547d206de9e3e333e29ccad52bf6b519a86ff9 (cherry picked from commit b9938230f992935e8332b6e288937be890724cd2) (cherry picked from commit eeb7b1010720c47511d8b1abb94260357b0bea3f) (cherry picked from commit f4deaad463efacaaaf429ac37bf5d37903cb4f55)
-rw-r--r--oslo_utils/fileutils.py3
-rw-r--r--oslo_utils/tests/test_fileutils.py19
2 files changed, 22 insertions, 0 deletions
diff --git a/oslo_utils/fileutils.py b/oslo_utils/fileutils.py
index e2b0917..909ed4b 100644
--- a/oslo_utils/fileutils.py
+++ b/oslo_utils/fileutils.py
@@ -25,6 +25,7 @@ import hashlib
import os
import stat
import tempfile
+import time
from oslo_utils import excutils
@@ -123,6 +124,8 @@ def compute_file_checksum(path, read_chunksize=65536, algorithm='sha256'):
with open(path, 'rb') as f:
for chunk in iter(lambda: f.read(read_chunksize), b''):
checksum.update(chunk)
+ # Release greenthread, if greenthreads are not used it is a noop.
+ time.sleep(0)
return checksum.hexdigest()
diff --git a/oslo_utils/tests/test_fileutils.py b/oslo_utils/tests/test_fileutils.py
index d34a38b..c576e11 100644
--- a/oslo_utils/tests/test_fileutils.py
+++ b/oslo_utils/tests/test_fileutils.py
@@ -19,8 +19,10 @@ import os
import shutil
import stat
import tempfile
+import time
import uuid
+import mock
from oslotest import base as test_base
import six
@@ -215,6 +217,23 @@ class TestComputeFileChecksum(test_base.BaseTestCase):
self.assertEqual(expected_checksum.hexdigest(), actual_checksum)
+ def test_compute_checksum_sleep_0_called(self):
+ path = fileutils.write_to_tempfile(self.content)
+ self.assertTrue(os.path.exists(path))
+ self.check_file_content(self.content, path)
+
+ expected_checksum = hashlib.sha256()
+ expected_checksum.update(self.content)
+
+ with mock.patch.object(time, "sleep") as sleep_mock:
+ actual_checksum = fileutils.compute_file_checksum(
+ path, read_chunksize=4)
+
+ sleep_mock.assert_has_calls([mock.call(0)] * 3)
+ # Just to make sure that there were exactly 3 calls
+ self.assertEqual(3, sleep_mock.call_count)
+ self.assertEqual(expected_checksum.hexdigest(), actual_checksum)
+
def test_compute_checksum_named_algorithm(self):
path = fileutils.write_to_tempfile(self.content)
self.assertTrue(os.path.exists(path))