From 4b65fc8a76718309565525015445e4f2e6504418 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 22 Sep 2020 10:15:35 -0400 Subject: [PATCH] Cache test uses polling to adapt to system speed Instead of waiting a fixed amount of time for a download, the cache tests use repeated polling to check if the download has finished. This means that on fast systems, the tests will run faster, and on slow systems there should be less random failure. Fixes #753 Signed-off-by: David Thompson --- .../contentmodel/DTDHoverExtensionsTest.java | 12 ++++--- .../CacheResourcesManagerTest.java | 33 +++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/DTDHoverExtensionsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/DTDHoverExtensionsTest.java index 01cc4dd115..175232aed3 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/DTDHoverExtensionsTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/DTDHoverExtensionsTest.java @@ -77,11 +77,15 @@ public void webXML() throws Exception { // Download the DTD by waiting 1 sec CacheResourcesManager cacheResourcesManager = new CacheResourcesManager(); - try { - cacheResourcesManager.getResource(httpDTDUri); - } catch (CacheResourceDownloadingException ignored) { + boolean downloaded = false; + while (!downloaded) { + try { + cacheResourcesManager.getResource(httpDTDUri); + downloaded = true; + } catch (CacheResourceDownloadingException ignored) { + TimeUnit.MILLISECONDS.sleep(100); + } } - TimeUnit.MILLISECONDS.sleep(1000); assertTrue(Files.exists(cachedFilePath), "'" + cachedFilePath + "' file should be downloaded in the cache."); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/uriresolver/CacheResourcesManagerTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/uriresolver/CacheResourcesManagerTest.java index f780d845d6..59fb2b79ba 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/uriresolver/CacheResourcesManagerTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/uriresolver/CacheResourcesManagerTest.java @@ -71,11 +71,20 @@ public void testUnavailableCache() throws Exception { fail("cacheResourcesManager should be busy downloading the url"); } catch (CacheResourceDownloadingException ignored) { } - TimeUnit.MILLISECONDS.sleep(200); - // failed to download so returns null - assertNull(cacheResourcesManager.getResource(uri)); - TimeUnit.SECONDS.sleep(1);// wait past the cache expiration date + boolean downloaded = false; + int i = 0; + while (!downloaded && i < 9) { + try { + TimeUnit.MILLISECONDS.sleep(100); + assertNull(cacheResourcesManager.getResource(uri)); + downloaded = true; + } catch (CacheResourceDownloadingException e) { + } + i++; + } + + TimeUnit.SECONDS.sleep(2);// wait past the cache expiration date // Manager should retry downloading try { @@ -95,11 +104,21 @@ public void testAvailableCache() throws Exception { fail("cacheResourcesManager should be busy downloading the url"); } catch (CacheResourceDownloadingException ignored) { } - TimeUnit.MILLISECONDS.sleep(200); - assertNotNull(cacheResourcesManager.getResource(uri)); + + boolean downloaded = false; + int i = 0; + while (!downloaded && i < 9) { + try { + TimeUnit.MILLISECONDS.sleep(100); + assertNotNull(cacheResourcesManager.getResource(uri)); + downloaded = true; + } catch (CacheResourceDownloadingException e) { + } + i++; + } server.stop(); - TimeUnit.SECONDS.sleep(1);// wait past the cache expiration date + TimeUnit.SECONDS.sleep(2);// wait past the cache expiration date // Manager should return cached content, even if server is offline assertNotNull(cacheResourcesManager.getResource(uri));