[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 16/42] tests/functional: Allow asset downloading with concurrent t
From: |
Thomas Huth |
Subject: |
[PULL 16/42] tests/functional: Allow asset downloading with concurrent threads |
Date: |
Wed, 4 Sep 2024 12:38:51 +0200 |
When running "make -j$(nproc) check-functional", tests that use the
same asset might be running in parallel. Improve the downloading to
detect this situation and wait for the other thread to finish the
download.
Message-ID: <20240830133841.142644-17-thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
tests/functional/qemu_test/asset.py | 62 ++++++++++++++++++++++++-----
1 file changed, 51 insertions(+), 11 deletions(-)
diff --git a/tests/functional/qemu_test/asset.py
b/tests/functional/qemu_test/asset.py
index b329ab7dbe..d3be2aff82 100644
--- a/tests/functional/qemu_test/asset.py
+++ b/tests/functional/qemu_test/asset.py
@@ -12,6 +12,7 @@
import sys
import unittest
import urllib.request
+from time import sleep
from pathlib import Path
from shutil import copyfileobj
@@ -55,6 +56,35 @@ def _check(self, cache_file):
def valid(self):
return self.cache_file.exists() and self._check(self.cache_file)
+ def _wait_for_other_download(self, tmp_cache_file):
+ # Another thread already seems to download the asset, so wait until
+ # it is done, while also checking the size to see whether it is stuck
+ try:
+ current_size = tmp_cache_file.stat().st_size
+ new_size = current_size
+ except:
+ if os.path.exists(self.cache_file):
+ return True
+ raise
+ waittime = lastchange = 600
+ while waittime > 0:
+ sleep(1)
+ waittime -= 1
+ try:
+ new_size = tmp_cache_file.stat().st_size
+ except:
+ if os.path.exists(self.cache_file):
+ return True
+ raise
+ if new_size != current_size:
+ lastchange = waittime
+ current_size = new_size
+ elif lastchange - waittime > 90:
+ return False
+
+ self.log.debug("Time out while waiting for %s!", tmp_cache_file)
+ raise
+
def fetch(self):
if not self.cache_dir.exists():
self.cache_dir.mkdir(parents=True, exist_ok=True)
@@ -70,18 +100,28 @@ def fetch(self):
self.log.info("Downloading %s to %s...", self.url, self.cache_file)
tmp_cache_file = self.cache_file.with_suffix(".download")
- try:
- resp = urllib.request.urlopen(self.url)
- except Exception as e:
- self.log.error("Unable to download %s: %s", self.url, e)
- raise
+ for retries in range(3):
+ try:
+ with tmp_cache_file.open("xb") as dst:
+ with urllib.request.urlopen(self.url) as resp:
+ copyfileobj(resp, dst)
+ break
+ except FileExistsError:
+ self.log.debug("%s already exists, "
+ "waiting for other thread to finish...",
+ tmp_cache_file)
+ if self._wait_for_other_download(tmp_cache_file):
+ return str(self.cache_file)
+ self.log.debug("%s seems to be stale, "
+ "deleting and retrying download...",
+ tmp_cache_file)
+ tmp_cache_file.unlink()
+ continue
+ except Exception as e:
+ self.log.error("Unable to download %s: %s", self.url, e)
+ tmp_cache_file.unlink()
+ raise
- try:
- with tmp_cache_file.open("wb+") as dst:
- copyfileobj(resp, dst)
- except:
- tmp_cache_file.unlink()
- raise
try:
# Set these just for informational purposes
os.setxattr(str(tmp_cache_file), "user.qemu-asset-url",
--
2.46.0
- [PULL 11/42] tests/functional: Prepare the meson build system for the functional tests, (continued)
- [PULL 11/42] tests/functional: Prepare the meson build system for the functional tests, Thomas Huth, 2024/09/04
- [PULL 12/42] tests/functional: Convert simple avocado tests into standalone python tests, Thomas Huth, 2024/09/04
- [PULL 13/42] tests/functional: Convert avocado tests that just need a small adjustment, Thomas Huth, 2024/09/04
- [PULL 14/42] tests/functional: add a module for handling asset download & caching, Thomas Huth, 2024/09/04
- [PULL 19/42] tests/functional: Convert some avocado tests that needed avocado.utils.archive, Thomas Huth, 2024/09/04
- [PULL 17/42] tests/functional: Convert some tests that download files via fetch_asset(), Thomas Huth, 2024/09/04
- [PULL 18/42] tests/functional: Add a function for extracting files from an archive, Thomas Huth, 2024/09/04
- [PULL 15/42] tests/functional: enable pre-emptive caching of assets, Thomas Huth, 2024/09/04
- [PULL 21/42] tests/functional: Convert the x86_cpu_model_versions test, Thomas Huth, 2024/09/04
- [PULL 24/42] tests/functional: Convert most ppc avocado tests into standalone tests, Thomas Huth, 2024/09/04
- [PULL 16/42] tests/functional: Allow asset downloading with concurrent threads,
Thomas Huth <=
- [PULL 25/42] tests/functional: Convert the ppc_amiga avocado test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 20/42] tests/functional: Convert the s390x avocado tests into standalone tests, Thomas Huth, 2024/09/04
- [PULL 23/42] tests/functional: Convert the virtio_gpu avocado test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 26/42] tests/functional: Convert the ppc_hv avocado test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 22/42] tests/functional: Convert the microblaze avocado tests into standalone tests, Thomas Huth, 2024/09/04
- [PULL 27/42] tests/functional: Convert the m68k nextcube test with tesseract, Thomas Huth, 2024/09/04
- [PULL 28/42] tests/functional: Convert the acpi-bits test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 29/42] tests/functional: Convert the rx_gdbsim avocado test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 30/42] tests/functional: Convert the linux_initrd avocado test into a standalone test, Thomas Huth, 2024/09/04
- [PULL 31/42] tests/functional: Convert ARM Integrator/CP avocado tests, Thomas Huth, 2024/09/04