Skip to content

Commit 05e5d38

Browse files
authored
fix: vortex-cxx test race (#7189)
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
1 parent 66236f8 commit 05e5d38

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

vortex-cxx/cpp/tests/basic_test.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <fstream>
88
#include <thread>
99
#include <iostream>
10+
#include <random>
11+
#include <sstream>
1012

1113
#include "vortex/file.hpp"
1214
#include "vortex/scan.hpp"
@@ -19,28 +21,30 @@
1921
#include <nanoarrow/nanoarrow.h>
2022

2123
class VortexTest : public ::testing::Test {
22-
public:
23-
static void SetUpTestSuite() {
24-
std::string test_data_path = GetTestDataPath("test_data.vortex");
25-
auto stream = vortex::testing::CreateTestDataStream();
26-
auto write_options = vortex::ffi::write_options_new();
27-
vortex::ffi::write_array_stream(std::move(write_options),
28-
reinterpret_cast<uint8_t *>(&stream),
29-
test_data_path.c_str());
30-
}
31-
3224
protected:
33-
// Helper function to construct file paths in system temp directory
34-
static std::string GetTestDataPath(const std::string &filename) {
25+
// Helper function to create unique temporary files for each test
26+
static std::string GetUniqueTempFile(const std::string &suffix = "vortex") {
3527
std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
3628
std::filesystem::path vortex_test_dir = temp_dir / "vortex_test";
3729

3830
if (!std::filesystem::exists(vortex_test_dir)) {
3931
std::filesystem::create_directories(vortex_test_dir);
4032
}
4133

42-
std::filesystem::path target_path = vortex_test_dir / filename;
43-
return target_path.string();
34+
// Use a unique random filename to prevent races between parallel test runs
35+
std::string unique_name = "test_" + std::to_string(std::random_device {}()) + "_" + suffix;
36+
return (vortex_test_dir / unique_name).string();
37+
}
38+
39+
// Write test data to a unique temporary file and return the path
40+
static std::string WriteTestData(const std::string &suffix = "test_data.vortex") {
41+
std::string path = GetUniqueTempFile(suffix);
42+
auto stream = vortex::testing::CreateTestDataStream();
43+
auto write_options = vortex::ffi::write_options_new();
44+
vortex::ffi::write_array_stream(std::move(write_options),
45+
reinterpret_cast<uint8_t *>(&stream),
46+
path.c_str());
47+
return path;
4448
}
4549

4650
// Helper function to create and initialize array view
@@ -153,7 +157,8 @@ class VortexTest : public ::testing::Test {
153157
// Helper to execute scan builder and get array+schema
154158
std::pair<nanoarrow::UniqueArray, nanoarrow::UniqueSchema> ScanFirstArrayFromTestData(
155159
const std::function<ArrowArrayStream(vortex::ScanBuilder &)> &configureScanBuilder) {
156-
auto file = vortex::VortexFile::Open(GetTestDataPath("test_data.vortex"));
160+
auto test_data_path = WriteTestData();
161+
auto file = vortex::VortexFile::Open(test_data_path);
157162
auto scan_builder = file.CreateScanBuilder();
158163
auto stream = configureScanBuilder(scan_builder);
159164

@@ -260,15 +265,17 @@ TEST_F(VortexTest, ScanBuilderWithRowRangeWithIncludeByIndex) {
260265
}
261266

262267
TEST_F(VortexTest, WriteArrayStream) {
263-
auto file = vortex::VortexFile::Open(GetTestDataPath("test_data.vortex"));
268+
auto test_data_path = WriteTestData();
269+
auto file = vortex::VortexFile::Open(test_data_path);
264270
auto stream = file.CreateScanBuilder().IntoStream();
265271

266272
// Write the stream to a new Vortex file
273+
std::string test_output_path = GetUniqueTempFile("write_output.vortex");
267274
vortex::VortexWriteOptions write_options;
268-
ASSERT_NO_THROW(write_options.WriteArrayStream(stream, GetTestDataPath("test_output.vortex")));
275+
ASSERT_NO_THROW(write_options.WriteArrayStream(stream, test_output_path));
269276

270277
// Verify the written file
271-
auto written_file = vortex::VortexFile::Open(GetTestDataPath("test_output.vortex"));
278+
auto written_file = vortex::VortexFile::Open(test_output_path);
272279
ASSERT_EQ(written_file.RowCount(), 5);
273280

274281
// Verify data integrity by reading from the written file
@@ -280,7 +287,7 @@ TEST_F(VortexTest, WriteArrayStream) {
280287
}
281288

282289
TEST_F(VortexTest, ConcurrentMultiStreamRead) {
283-
std::string test_data_path_1m = GetTestDataPath("test_data_1m.vortex");
290+
std::string test_data_path_1m = GetUniqueTempFile("concurrent_1m.vortex");
284291
auto stream_1m = vortex::testing::CreateTestData1MStream();
285292
auto write_options = vortex::ffi::write_options_new();
286293
vortex::ffi::write_array_stream(std::move(write_options),
@@ -473,7 +480,7 @@ TEST_F(VortexTest, ScanBuilderWithProjectionSingleColumn) {
473480
}
474481

475482
TEST_F(VortexTest, OpenFromBuffer) {
476-
std::string test_file_path = GetTestDataPath("test_buffer.vortex");
483+
std::string test_file_path = GetUniqueTempFile("buffer.vortex");
477484
auto stream = vortex::testing::CreateTestDataStream();
478485
auto write_options = vortex::ffi::write_options_new();
479486
vortex::ffi::write_array_stream(std::move(write_options),

0 commit comments

Comments
 (0)