Skip to content

Commit

Permalink
fixed #175: Mandate TestResourceProviders to be thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Jan 26, 2018
1 parent 0664fea commit 2a9e3d4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* A test resource provider is used by the test runner to enumerate and retrieve the necessary resources, which are then
* parsed and linked into the test model. The classloader to use for resolving any Java references is also provided by
* the resource provider.
* <p>
* Test resource providers must be thread-safe with regard to all methods defined in {@link TestResourceProvider}, as
* they are used in multi-threaded script loading, in which resources are opened and closed in parallel!
*
*
* @author Rene Schneider - initial API and implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public ClassLoader getClassLoader() {

/**
* The resources in this provider. This abstract class supports an "early-evaluation" model: resources are
* discovered and added early, before they're actually requested.
* discovered and added early, before they're actually requested. This thing does not have to be thread-safe, as the
* resource discovery phase is not expected to be run in parallel on multiple threads (and if an implementor would
* do such a thing, he is responsible to protect against thread interferences).
*/
protected Set<TestResource> resourceFiles = new HashSet<TestResource>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Stack;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -43,7 +43,7 @@ public class ArchiveTestResourceProvider extends AbstractTestResourceProvider {
/**
* A map to keep track of opened {@link ZipInputStream}s. Used to close the streams.
*/
Map<InputStream, Stack<ZipInputStream>> openedResourceToStreamsMap = new HashMap<InputStream, Stack<ZipInputStream>>();
Map<InputStream, Stack<ZipInputStream>> openedResourceToStreamsMap = new Hashtable<InputStream, Stack<ZipInputStream>>();

/**
* Adds an archive (all contained .integrity files) to the resource provider.
Expand Down Expand Up @@ -81,11 +81,10 @@ protected void addArchive(ZipInputStream anArchiveInputStream, String aPrefix, b
String tempEntryName = aPrefix + "/" + tempEntry.getName();
String tempLowerCaseName = tempEntry.getName().toLowerCase();
if (tempLowerCaseName.endsWith(INTEGRITY_TEST_FILES_SUFFIX)) {
addResource(new ArchivedTestResource(tempEntryName, this, anArchiveFileNameStack,
tempEntry.getName()));
} else if (aRecursiveFlag
&& (tempLowerCaseName.endsWith(ARCHIVE_ENDING_ZIP) || tempLowerCaseName
.endsWith(ARCHIVE_ENDING_JAR))) {
addResource(
new ArchivedTestResource(tempEntryName, this, anArchiveFileNameStack, tempEntry.getName()));
} else if (aRecursiveFlag && (tempLowerCaseName.endsWith(ARCHIVE_ENDING_ZIP)
|| tempLowerCaseName.endsWith(ARCHIVE_ENDING_JAR))) {
Stack<String> tempSubArchiveStack = (Stack<String>) anArchiveFileNameStack.clone();
tempSubArchiveStack.push(tempEntry.getName());
addArchive(new ZipInputStream(anArchiveInputStream), tempEntryName, aRecursiveFlag,
Expand Down Expand Up @@ -132,8 +131,10 @@ public InputStream openResource(TestResource aResourceName) throws IOException {
public void closeResource(TestResource aResourceName, InputStream aResourceStream) throws IOException {
Stack<ZipInputStream> tempOpenStreams = openedResourceToStreamsMap.remove(aResourceStream);

for (ZipInputStream tempOpenStream : tempOpenStreams) {
tempOpenStream.close();
if (tempOpenStreams != null) {
for (ZipInputStream tempOpenStream : tempOpenStreams) {
tempOpenStream.close();
}
}
}

Expand Down

0 comments on commit 2a9e3d4

Please sign in to comment.