Skip to content

Commit

Permalink
Cache unavailable URI's
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#201

Tests aren't complete, need to find a way to handle exceptions.

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Jan 8, 2019
1 parent 23f8355 commit 3821fef
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
10 changes: 4 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"configurations": [{
"type": "java",
"name": "Debug (Attach)",
"name": "Debug (Attach) - Remote",
"request": "attach",
"hostName": "localhost",
"port": 1054,
"projectName": "org.eclipse.lsp4xml"
}
]
}
}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;


import org.eclipse.lsp4xml.utils.FilesUtils;
import org.eclipse.lsp4xml.utils.URIUtils;

Expand All @@ -38,6 +43,7 @@
*
*/
public class CacheResourcesManager {
protected final Cache<String, Boolean> unavailableURICache;

private static final String CACHE_PATH = "cache";
private static final Logger LOGGER = Logger.getLogger(CacheResourcesManager.class.getName());
Expand Down Expand Up @@ -79,14 +85,26 @@ public String getResourceFromClasspath() {
}

public CacheResourcesManager() {
this(CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(30, TimeUnit.SECONDS).build());

}

public CacheResourcesManager(Cache<String, Boolean> cache) {
resourcesLoading = new HashMap<>();
unavailableURICache = cache;
}

public Path getResource(final String resourceURI) throws IOException {
Path resourceCachePath = getResourceCachePath(resourceURI);
if (Files.exists(resourceCachePath)) {
return resourceCachePath;
}

if(unavailableURICache.getIfPresent(resourceURI) != null) {
LOGGER.info("Ignored unavailable schema URI: " + resourceURI + "\n");
return null;
}

CompletableFuture<Path> f = null;
synchronized (resourcesLoading) {
if (resourcesLoading.containsKey(resourceURI)) {
Expand Down Expand Up @@ -139,11 +157,12 @@ private CompletableFuture<Path> downloadResource(final String resourceURI, Path
LOGGER.info("Downloaded " + resourceURI + " to " + resourceCachePath + " in " + elapsed + "ms");
} catch (Exception e) {
// Do nothing
unavailableURICache.put(resourceURI, true);
Throwable rootCause = getRootCause(e);
String error = "[" + rootCause.getClass().getTypeName() + "] " + rootCause.getMessage();
LOGGER.log(Level.SEVERE,
"Error while downloading " + resourceURI + " to " + resourceCachePath + " : " + error);
throw new CacheResourceDownloadedException("Error while downloading '" + resourceURI + "'.", e);
throw new CacheResourceDownloadedException("Error while downloading '" + resourceURI + "' to " + resourceCachePath + ".", e);
} finally {
synchronized (resourcesLoading) {
resourcesLoading.remove(resourceURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class CacheResourcesManagerTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testCanUseCache() {
testCanUseCache(true);
Expand All @@ -32,4 +45,48 @@ private void testCanUseCache(boolean useCacheEnabled) {
assertFalse(cacheResourcesManager.canUseCache("file:///foo"));
}

@Test
public void testUnavailableCache() {
CacheResourcesManager cacheResourcesManager = new CacheResourcesManager();
cacheResourcesManager.setUseCache(true);
try {
thrown.expect(CacheResourceDownloadingException.class);
cacheResourcesManager.getResource("http://bad");
} catch (IOException e) {

}

try {
thrown.expect(CacheResourceDownloadedException.class);
assertNull(cacheResourcesManager.getResource("http://bad"));
} catch (IOException e1) {

}
}

@Test

public void testAvailableCache() {
CacheResourcesManager cacheResourcesManager = new CacheResourcesManager(CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.maximumSize(100)
.build());
cacheResourcesManager.setUseCache(true);
try {
//thrown.expect(CacheResourceDownloadedException.class);
//cacheResourcesManager.getResource("http://bad");

//thrown.expect(CacheResourceDownloadedException.class);
assertNull(cacheResourcesManager.getResource("http://bad"));

TimeUnit.SECONDS.sleep(10);
assertNull(cacheResourcesManager.getResource("http://bad"));
} catch (Exception IOException) {
fail();
}



}

}

0 comments on commit 3821fef

Please sign in to comment.