Skip to content

Commit

Permalink
Cache test uses polling to adapt to system speed
Browse files Browse the repository at this point in the history
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 eclipse-lemminx#753

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Sep 22, 2020
1 parent 3ea8abb commit 4b65fc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand Down

0 comments on commit 4b65fc8

Please sign in to comment.