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 4, 2019
1 parent 23f8355 commit 817885c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
15 changes: 7 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 1054,
"projectName": "org.eclipse.lsp4xml"
}
{
"type": "java",
"name": "Debug (Attach) - Remote",
"request": "attach",
"hostName": "localhost",
"port": "1054"
}
]
}
5 changes: 5 additions & 0 deletions org.eclipse.lsp4xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.5.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
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.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

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

Expand All @@ -38,6 +42,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 +84,27 @@ public String getResourceFromClasspath() {
}

public CacheResourcesManager() {
this(Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.maximumSize(100)
.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) {
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,22 @@

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

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

import com.github.benmanes.caffeine.cache.Caffeine;

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 +43,52 @@ 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();
cacheResourcesManager.setUseCache(true);
try {
thrown.expect(CacheResourceDownloadingException.class);
cacheResourcesManager.getResource("http://bad");
} catch (IOException e) {
try {
thrown.expect(CacheResourceDownloadingException.class);
assertNull(cacheResourcesManager.getResource("http://bad"));
} catch (IOException e1) {
try {
assertNull(cacheResourcesManager.getResource("http://bad"));
} catch (Exception e) {
//TODO: handle exception
}
}
}
}

class MockCacheResourcesManager extends CacheResourcesManager {
public MockCacheResourcesManager() {
super(Caffeine.newBuilder()
.expireAfterWrite(5, TimeUnit.SECONDS)
.maximumSize(100)
.build());
}
}

}

0 comments on commit 817885c

Please sign in to comment.