Skip to content

Commit

Permalink
GH-4592 Make sure tests do not share state as they run concurrently.
Browse files Browse the repository at this point in the history
Signed-off-by: Jerven Bolleman <jerven.bolleman@sib.swiss>
  • Loading branch information
JervenBolleman committed Nov 10, 2023
1 parent b7fc16b commit 940e316
Show file tree
Hide file tree
Showing 20 changed files with 2,400 additions and 1,855 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.function.Supplier;

import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.eclipse.rdf4j.common.transaction.IsolationLevel;
Expand Down Expand Up @@ -60,36 +61,36 @@ public abstract class AbstractComplianceTest {

protected DynamicTest makeTest(String name, Executable x) {
return DynamicTest.dynamicTest(name, () -> {
setUp();
x.execute();
tearDown();
});
}

protected final Logger logger = LoggerFactory.getLogger(this.getClass());

protected final Repository repo;
protected RepositoryConnection conn;

public AbstractComplianceTest(Repository repo) {
this.repo = repo;
protected Repository openRepository() {
Repository r = repo.get();
r.init();
return r;
}

public void setUp() {
repo.init();
conn = new RepositoryConnectionWrapper(repo.getConnection());
protected RepositoryConnection openConnection(Repository r) {
return new RepositoryConnectionWrapper(r.getConnection());
}

public void tearDown() {
try {
protected void closeRepository(Repository r) {
try (RepositoryConnection conn = r.getConnection()) {
conn.clear();
conn.close();
} finally {
repo.shutDown();
}
r.shutDown();
}

protected final Logger logger = LoggerFactory.getLogger(this.getClass());

protected final Supplier<Repository> repo;

public AbstractComplianceTest(Supplier<Repository> repo) {
this.repo = repo;
}

protected void loadTestData(String dataFile, Resource... contexts)
protected void loadTestData(String dataFile, RepositoryConnection conn, Resource... contexts)
throws RDFParseException, RepositoryException, IOException {
logger.debug("loading dataset {}", dataFile);
try (InputStream dataset = this.getClass().getResourceAsStream(dataFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
*******************************************************************************/
package org.eclipse.rdf4j.testsuite.sparql;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.eclipse.rdf4j.common.annotation.Experimental;
Expand Down Expand Up @@ -56,92 +59,92 @@ public abstract class RepositorySPARQLComplianceTestSuite {

@TestFactory
Stream<DynamicTest> aggregate() throws RDF4JException, IOException {
return new AggregateTest(getEmptyInitializedRepository()).tests();
return new AggregateTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> arbitraryLengthPath() throws RDF4JException, IOException {
return new ArbitraryLengthPathTest(getEmptyInitializedRepository()).tests();
return new ArbitraryLengthPathTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> basic() throws RDF4JException, IOException {
return new BasicTest(getEmptyInitializedRepository()).tests();
return new BasicTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> bind() throws RDF4JException, IOException {
return new BindTest(getEmptyInitializedRepository()).tests();
return new BindTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> builtinFunction() throws RDF4JException, IOException {
return new BuiltinFunctionTest(getEmptyInitializedRepository()).tests();
return new BuiltinFunctionTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> construct() throws RDF4JException, IOException {
return new ConstructTest(getEmptyInitializedRepository()).tests();
return new ConstructTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> defaultGraph() throws RDF4JException, IOException {
return new DefaultGraphTest(getEmptyInitializedRepository()).tests();
return new DefaultGraphTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> describe() throws RDF4JException, IOException {
return new DescribeTest(getEmptyInitializedRepository()).tests();
return new DescribeTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> groupBy() throws RDF4JException, IOException {
return new GroupByTest(getEmptyInitializedRepository()).tests();
return new GroupByTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> in() throws RDF4JException, IOException {
return new InTest(getEmptyInitializedRepository()).tests();
return new InTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> optional() throws RDF4JException, IOException {
return new OptionalTest(getEmptyInitializedRepository()).tests();
return new OptionalTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> propertyPath() throws RDF4JException, IOException {
return new PropertyPathTest(getEmptyInitializedRepository()).tests();
return new PropertyPathTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> subselect() throws RDF4JException, IOException {
return new SubselectTest(getEmptyInitializedRepository()).tests();
return new SubselectTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> union() throws RDF4JException, IOException {
return new UnionTest(getEmptyInitializedRepository()).tests();
return new UnionTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> values() throws RDF4JException, IOException {
return new ValuesTest(getEmptyInitializedRepository()).tests();
return new ValuesTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> orderBy() throws RDF4JException, IOException {
return new OrderByTest(getEmptyInitializedRepository()).tests();
return new OrderByTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> exists() throws RDF4JException, IOException {
return new ExistsTest(getEmptyInitializedRepository()).tests();
return new ExistsTest(this::getEmptyInitializedRepository).tests();
}

@TestFactory
Stream<DynamicTest> minus() throws RDF4JException, IOException {
return new MinusTest(getEmptyInitializedRepository()).tests();
return new MinusTest(this::getEmptyInitializedRepository).tests();
}

@BeforeAll
Expand All @@ -157,20 +160,33 @@ public static void tearDownClass() {
@TempDir
private File dataDir;

private static final AtomicInteger tempDirNameForRepoCounter = new AtomicInteger();

protected final RepositoryFactory factory;

public RepositorySPARQLComplianceTestSuite(RepositoryFactory factory) {
super();
this.factory = factory;
}

public Repository getEmptyInitializedRepository() throws RDF4JException, IOException {
Repository repository = factory.getRepository(factory.getConfig());
repository.setDataDir(dataDir);
try (RepositoryConnection con = repository.getConnection()) {
con.clear();
con.clearNamespaces();
public Repository getEmptyInitializedRepository() {
try {
Repository repository = factory.getRepository(factory.getConfig());
dataDir.mkdir();
File tmpDirPerRepo = new File(dataDir, "tmpDirPerRepo" + tempDirNameForRepoCounter.getAndIncrement());
if (!tmpDirPerRepo.mkdir()) {
fail("Could not create temporary directory for test");
}
repository.setDataDir(tmpDirPerRepo);
try (RepositoryConnection con = repository.getConnection()) {
con.clear();
con.clearNamespaces();
}
return repository;

} catch (RDF4JException e) {
fail(e);
return null;
}
return repository;
}
}
Loading

0 comments on commit 940e316

Please sign in to comment.