This is an automated email from the ASF dual-hosted git repository. jackietien pushed a commit to branch ty/TableModelGrammar in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 424c37f3e6af566d0ba3502b99abed6335fcabeb Author: Brian Demers <[email protected]> AuthorDate: Tue Jul 23 02:30:28 2024 -0400 Allows for unit tests to be run in parallel (#12980) NOTE: This does NOT set Surefire's `forkCount` property, that will be done on an isolated commit This is to ensure these changes don't negatively affect the existing build. Details on changes: - Change to test working directory The working directory was changed from `${project.build.directory}` to `${project.build.directory}/fork_${surefire.forkNumber}` (single fork builds would be `<module_name>/target/fork_1`), This isolates any test output written to relative paths. Long term tests may want to be more explicit where they write output to, (e.g. using JUnit 5's `@TempDir` annotation) - IOTDB_CONF property Becuse the working directory change, the IOTDB_CONF directory needs to resolve differently, however, even without parallel tests, this property _should_ use an absolute path - Test run `mkdirs` The tests assumed that the `./target` directory was always present (relative to the working directory), a change to the working directory invalidated this assumption, so any directories a test uses should be created. This is probably a good idea even without the parallel test concern. (but in the future this could be replaced with something like JUnit 5's `@TempDir` annotation) - Hardcoded Ports TestUtils, was using a hardcodes sequentially from `6001`, it now uses free JUnit 5's `@TempDir` annotation ports Fixes: #12979 (cherry picked from commit 77b41310f55ec602f8cba96cca9529f5f0792f24) --- .../test/java/org/apache/iotdb/tool/WriteDataFileTest.java | 14 ++++++++++++++ .../java/org/apache/iotdb/consensus/ratis/TestUtils.java | 11 ++++++++++- iotdb-core/datanode/pom.xml | 2 +- .../org/apache/iotdb/db/utils/constant/TestConstant.java | 8 +++++++- .../db/storageengine/dataregion/wal/io/WALFileTest.java | 2 ++ pom.xml | 2 ++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/WriteDataFileTest.java b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/WriteDataFileTest.java index c198b530c4e..eb9e01d2937 100644 --- a/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/WriteDataFileTest.java +++ b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/WriteDataFileTest.java @@ -19,8 +19,10 @@ package org.apache.iotdb.tool; +import org.junit.Before; import org.junit.Test; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -28,6 +30,18 @@ import java.util.List; import static org.junit.Assert.assertTrue; public class WriteDataFileTest { + + /** + * Create the 'target' directory before the tests run. When running tests with multiple threads, + * the working directory might be 'target/fork_#' which would changes the assumption that the + * 'target' directory exists already. When running tests via an IDE, the working directory might + * be the project directory, and the target directory likely already exists. + */ + @Before + public void createTestDirectory() { + new File("target").mkdirs(); + } + @Test public void writeCsvFileTest() { List<String> headerNames = diff --git a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java index 4aaa5f77d9e..f5997c261fd 100644 --- a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java +++ b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/ratis/TestUtils.java @@ -51,6 +51,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.net.ServerSocket; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; @@ -251,7 +252,7 @@ public class TestUtils { this.servers = new ArrayList<>(); for (int i = 0; i < replicas; i++) { - peers.add(new Peer(gid, i, new TEndPoint("127.0.0.1", 6001 + i))); + peers.add(new Peer(gid, i, new TEndPoint("127.0.0.1", randomFreePort()))); final File storage = storageProvider.apply(i); FileUtils.deleteFileQuietly(storage); @@ -493,4 +494,12 @@ public class TestUtils { return new MiniCluster(gid, replicas, peerStorageProvider, smProvider, ratisConfig); } } + + private static int randomFreePort() { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } catch (IOException e) { + throw new IllegalStateException("Failed to find free server socket port.", e); + } + } } diff --git a/iotdb-core/datanode/pom.xml b/iotdb-core/datanode/pom.xml index 007b146784c..7ddf561b3fd 100644 --- a/iotdb-core/datanode/pom.xml +++ b/iotdb-core/datanode/pom.xml @@ -380,7 +380,7 @@ <configuration> <skipTests>${iotdb.ut.skip}</skipTests> <systemProperties> - <IOTDB_CONF>src/test/resources</IOTDB_CONF> + <IOTDB_CONF>${project.basedir}/src/test/resources</IOTDB_CONF> </systemProperties> <reuseForks>false</reuseForks> <runOrder>random</runOrder> diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/TestConstant.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/TestConstant.java index 1647a19b0cb..a0724c9ae1c 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/TestConstant.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/TestConstant.java @@ -25,7 +25,7 @@ import java.io.File; public class TestConstant { - public static final String BASE_OUTPUT_PATH = "target".concat(File.separator); + public static final String BASE_OUTPUT_PATH = baseOutputDirectory(); public static final String OUTPUT_DATA_DIR = BASE_OUTPUT_PATH.concat("data").concat(File.separator); public static final String PARTIAL_PATH_STRING = @@ -109,4 +109,10 @@ public class TestConstant { virtualStorageGroupId, timePartitionId); } + + private static String baseOutputDirectory() { + File dir = new File("target"); + dir.mkdirs(); + return dir.getPath().concat(File.separator); + } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/WALFileTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/WALFileTest.java index 3e6ec4937ad..a15cc27fa26 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/WALFileTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/WALFileTest.java @@ -68,6 +68,8 @@ public class WALFileTest { public void setUp() throws Exception { if (walFile.exists()) { Files.delete(walFile.toPath()); + } else { + walFile.getParentFile().mkdirs(); } } diff --git a/pom.xml b/pom.xml index 04b2fbea5b0..58a9047b352 100644 --- a/pom.xml +++ b/pom.xml @@ -743,6 +743,8 @@ <version>3.1.2</version> <configuration> <argLine>${argLine} -Xmx1024m</argLine> + <!-- Force the working directory to be different for each fork, so forks don't trample each other --> + <workingDirectory>${project.build.directory}/fork_${surefire.forkNumber}</workingDirectory> </configuration> </plugin> <!--
