Skip to content

Commit

Permalink
Merge pull request ronmamo#206 from andreabertagnolli/issue81
Browse files Browse the repository at this point in the history
Fix issue ronmamo#81 by disabling cache's use on url connection
  • Loading branch information
ronmamo authored Nov 11, 2017
2 parents 19b2fb2 + e56cd65 commit 402ad37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/java/org/reflections/vfs/Vfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import org.reflections.util.Utils;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.jar.JarFile;

/**
Expand Down Expand Up @@ -226,7 +228,8 @@ public Dir createDir(URL url) throws Exception {
try {
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
return new ZipDir(((JarURLConnection) urlConnection).getJarFile());
urlConnection.setUseCaches(false);
return new ZipDir(((JarURLConnection) urlConnection).getJarFile());
}
} catch (Throwable e) { /*fallback*/ }
java.io.File file = getFile(url);
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/reflections/ReflectionsThreadSafenessTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.reflections;

import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static java.util.Collections.singletonList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;

public class ReflectionsThreadSafenessTest {

/**
* https://github.com/ronmamo/reflections/issues/81
*/
@Test
public void reflections_scan_is_thread_safe() throws Exception {

Callable<Set<Class<? extends ImmutableMap>>> callable = new Callable<Set<Class<? extends ImmutableMap>>>() {
@Override
public Set<Class<? extends ImmutableMap>> call() throws Exception {
final Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(singletonList(ClasspathHelper.forClass(ImmutableMap.class)))
.setScanners(new SubTypesScanner(false)));

return reflections.getSubTypesOf(ImmutableMap.class);
}
};

final ExecutorService pool = Executors.newFixedThreadPool(2);

final Future<?> first = pool.submit(callable);
final Future<?> second = pool.submit(callable);

assertEquals(first.get(5, SECONDS), second.get(5, SECONDS));
}
}

0 comments on commit 402ad37

Please sign in to comment.