Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from veraPDF/merge/multi-threaded
FEAT - Multithreaded processing
- Loading branch information
Showing
10 changed files
with
524 additions
and
46 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
greenfield-apps/src/test/java/org/verapdf/apps/MultithreadingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.verapdf.apps; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.verapdf.pdfa.VeraGreenfieldFoundryProvider; | ||
import org.verapdf.pdfa.results.ValidationResult; | ||
import org.verapdf.pdfa.validation.validators.CallableValidatorForTest; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
|
||
public class MultithreadingTest { | ||
private static final String TEST_FILE = "src/test/resources/veraPDFtest-pass-a.pdf"; | ||
|
||
@Before | ||
public void init() { | ||
VeraGreenfieldFoundryProvider.initialise(); | ||
} | ||
|
||
@Test | ||
public void shouldCreateXmlReportsInGivenDirectory() throws Exception { | ||
File fileToValidate = new File(TEST_FILE); | ||
|
||
int numberOfThreads = 4; | ||
|
||
List<Future<ValidationResult>> futureResult = startValidation(fileToValidate, numberOfThreads); | ||
List<ValidationResult> validationResults = getValidationResult(futureResult); | ||
|
||
Boolean isExpectedResults = compareResultsFromDifferentThreads(validationResults); | ||
|
||
assertTrue(isExpectedResults); | ||
} | ||
|
||
private boolean compareResultsFromDifferentThreads(List<ValidationResult> validationResults) { | ||
boolean isExpectedResults = true; | ||
for (ValidationResult result : validationResults) { | ||
for (ValidationResult comparingResult : validationResults) { | ||
boolean isCompliantResultsEquals = result.isCompliant() == comparingResult.isCompliant(); | ||
boolean isTestAssertionsEquals = result.getTestAssertions().equals(comparingResult.getTestAssertions()); | ||
boolean isTotalAssertionsEquals = result.getTotalAssertions() == comparingResult.getTotalAssertions(); | ||
if (!isCompliantResultsEquals || !isTestAssertionsEquals || !isTotalAssertionsEquals) { | ||
isExpectedResults = false; | ||
} | ||
} | ||
} | ||
return isExpectedResults; | ||
} | ||
|
||
private List<ValidationResult> getValidationResult(List<Future<ValidationResult>> results) throws InterruptedException, java.util.concurrent.ExecutionException { | ||
List<ValidationResult> validationResults = new ArrayList<>(); | ||
for (Future<ValidationResult> result : results) { | ||
ValidationResult validationResult = result.get(); | ||
validationResults.add(validationResult); | ||
} | ||
return validationResults; | ||
} | ||
|
||
private List<Future<ValidationResult>> startValidation(File fileToValidate, int numberOfThreads) { | ||
List<Future<ValidationResult>> futureResult = new ArrayList<>(); | ||
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); | ||
for (int i = 0; i < numberOfThreads; i++) { | ||
CallableValidatorForTest validator = new CallableValidatorForTest(fileToValidate); | ||
Future<ValidationResult> submit = executor.submit(validator); | ||
futureResult.add(submit); | ||
} | ||
return futureResult; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...d-apps/src/test/java/org/verapdf/pdfa/validation/validators/CallableValidatorForTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.verapdf.pdfa.validation.validators; | ||
|
||
import org.verapdf.pdfa.Foundries; | ||
import org.verapdf.pdfa.PDFAParser; | ||
import org.verapdf.pdfa.PDFAValidator; | ||
import org.verapdf.pdfa.results.ValidationResult; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.util.concurrent.Callable; | ||
|
||
public class CallableValidatorForTest implements Callable<ValidationResult> { | ||
private File fileToValidate; | ||
|
||
public CallableValidatorForTest(File fileToValidate) { | ||
this.fileToValidate = fileToValidate; | ||
} | ||
|
||
@Override | ||
public ValidationResult call() throws Exception { | ||
ValidationResult result; | ||
try (FileInputStream fis = new FileInputStream(this.fileToValidate); | ||
PDFAParser parser = Foundries.defaultInstance().createParser(fis); | ||
PDFAValidator validator = Foundries.defaultInstance().createValidator(parser.getFlavour(), false)) { | ||
result = validator.validate(parser); | ||
} | ||
return result; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.