From 0d04df1c2a15003224578e4ba4e4e75e8af02b3f Mon Sep 17 00:00:00 2001 From: Harveer Singh Date: Mon, 13 May 2024 21:07:26 +0000 Subject: [PATCH] Bug 1799717: Adding a try/catch block in the test to exempt some known possible failures.r=jari Differential Revision: https://phabricator.services.mozilla.com/D210174 --- ...st_caches_delete_cleanup_after_shutdown.py | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/dom/cache/test/marionette/test_caches_delete_cleanup_after_shutdown.py b/dom/cache/test/marionette/test_caches_delete_cleanup_after_shutdown.py index 2923deeffe2f..7833ee7b3818 100644 --- a/dom/cache/test/marionette/test_caches_delete_cleanup_after_shutdown.py +++ b/dom/cache/test/marionette/test_caches_delete_cleanup_after_shutdown.py @@ -3,6 +3,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. import os +import time from marionette_driver import Wait from marionette_harness import MarionetteTestCase @@ -77,26 +78,43 @@ class CachesDeleteCleanupAtShutdownTestCase(MarionetteTestCase): script_args=(CACHE_ID,), ) + # asynchronously iterating over body files could be challenging as firefox + # might be doing some cleanup while we are iterating over it's morgue dir. + # It's expected for this helper to throw FileNotFoundError exception in + # such cases. + def fallibleCountBodies(self, morgueDir): + bodyCount = 0 + for elem in os.listdir(morgueDir): + absPathElem = os.path.join(morgueDir, elem) + + if os.path.isdir(absPathElem): + bodyCount += sum( + 1 + for e in os.listdir(absPathElem) + if os.path.isfile(os.path.join(absPathElem, e)) + ) + return bodyCount + def countBodies(self): profile = self.marionette.instance.profile.profile originDir = ( self.marionette.absolute_url("")[:-1].replace(":", "+").replace("/", "+") ) + morgueDir = f"{profile}/storage/default/{originDir}/cache/morgue" print("morgueDir path = ", morgueDir) - bodyCount = -1 - if os.path.exists(morgueDir): - bodyCount = 0 - for elem in os.listdir(morgueDir): - absPathElem = os.path.join(morgueDir, elem) - if os.path.isdir(absPathElem): - bodyCount += sum( - 1 - for e in os.listdir(absPathElem) - if os.path.isfile(os.path.join(absPathElem, e)) - ) - return bodyCount + if not os.path.exists(morgueDir): + print("morgue directory does not exists.") + return -1 + + while True: + try: + return self.fallibleCountBodies(morgueDir) + except FileNotFoundError: + # we probably just got in the period when firefox was cleaning up. + # retry... + time.sleep(0.5) def ensureCleanDirectory(self): orphanedBodiesCount = self.countBodies()