Skip to content

Commit

Permalink
#1896: tests: use unique filenames to allow parallel testing
Browse files Browse the repository at this point in the history
  • Loading branch information
nlslatt committed Aug 15, 2022
1 parent e89215e commit edab5ac
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
8 changes: 4 additions & 4 deletions tests/unit/collection/test_checkpoint.extended.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ TEST_F(TestCheckpoint, test_checkpoint_1) {
auto num_nodes = static_cast<int32_t>(theContext()->getNumNodes());

auto range = vt::Index3D(num_nodes, num_elms, 4);
std::string const checkpoint_name{"test_checkpoint_dir"};
std::string const checkpoint_name(getUniqueFilenameWithRanks());
std::string const expected_label{"test_checkpoint_1"};

{
Expand Down Expand Up @@ -251,7 +251,7 @@ TEST_F(TestCheckpoint, test_checkpoint_in_place_2) {
auto num_nodes = static_cast<int32_t>(theContext()->getNumNodes());

auto range = vt::Index3D(num_nodes, num_elms, 4);
auto checkpoint_name = "test_checkpoint_dir";
std::string const checkpoint_name(getUniqueFilenameWithRanks());
auto proxy = vt::theCollection()->constructCollective<TestCol>(
range, "test_checkpoint_in_place_2"
);
Expand Down Expand Up @@ -323,7 +323,7 @@ TEST_F(TestCheckpoint, test_checkpoint_in_place_3) {
auto num_nodes = static_cast<int32_t>(theContext()->getNumNodes());

auto range = vt::Index3D(num_nodes, num_elms, 4);
auto checkpoint_name = "test_checkpoint_dir_2";
std::string const checkpoint_name(getUniqueFilenameWithRanks());
auto proxy = vt::theCollection()->constructCollective<TestCol>(
range, "test_checkpoint_in_place_3"
);
Expand Down Expand Up @@ -400,7 +400,7 @@ TEST_F(TestCheckpoint, test_checkpoint_no_elements_on_root_rank) {
auto num_nodes = static_cast<int32_t>(theContext()->getNumNodes());

auto range = vt::Index3D(num_nodes, num_elms, 4);
auto checkpoint_name = "test_null_elm_checkpoint_dir";
std::string const checkpoint_name(getUniqueFilenameWithRanks());

{
auto proxy = vt::makeCollection<TestCol>("test_checkpoint_no_elements_on_root_rank")
Expand Down
21 changes: 19 additions & 2 deletions tests/unit/collection/test_lb.extended.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <gtest/gtest.h>

#include "test_parallel_harness.h"
#include "test_helpers.h"
#include "test_collection_common.h"
#include "data_message.h"

Expand All @@ -58,6 +59,7 @@

#include <nlohmann/json.hpp>
#include <memory>
#include <sstream>

#include <dirent.h>

Expand Down Expand Up @@ -274,8 +276,23 @@ INSTANTIATE_TEST_SUITE_P(
struct TestParallelHarnessWithLBDataDumping : TestParallelHarnessParam<int> {
virtual void addAdditionalArgs() override {
static char vt_lb_data[]{"--vt_lb_data"};
static char vt_lb_data_dir[]{"--vt_lb_data_dir=test_data_dir"};
static char vt_lb_data_file[]{"--vt_lb_data_file=test_data_outfile.%p.json"};

std::string lb_dir_file(getUniqueFilename("_dir"));
std::string lb_dir_flag("--vt_lb_data_dir=");
std::string lb_dir_arg = lb_dir_flag + lb_dir_file;
char *vt_lb_data_dir = strdup(lb_dir_arg.c_str());

std::stringstream ss;
ss << "--vt_lb_data_file=test_data_outfile";
int init = 0;
MPI_Initialized(&init);
if (init) {
int num_ranks = 0;
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
ss << "_" << num_ranks;
}
ss << ".%p.json";
char *vt_lb_data_file = strdup(ss.str().c_str());

addArgs(vt_lb_data, vt_lb_data_dir, vt_lb_data_file);
}
Expand Down
19 changes: 13 additions & 6 deletions tests/unit/runtime/test_initialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <gtest/gtest.h>

#include "test_parallel_harness.h"
#include "test_helpers.h"

#include <vt/collective/startup.h>

Expand Down Expand Up @@ -170,13 +171,16 @@ TEST_F(TestInitialization, test_initialize_with_file_and_args) {
static char cli_argument[]{"--cli_argument=100"};
static char vt_no_terminate[]{"--vt_no_terminate"};
static char vt_lb_name[]{"--vt_lb_name=RotateLB"};
static char vt_input_config[]{"--vt_input_config=test_cfg.toml"};

std::string config_file(getUniqueFilenameWithRanks(".toml"));
std::string config_flag("--vt_input_config=");
std::string vt_input_config = config_flag + config_file;

std::vector<char *> custom_args;
custom_args.emplace_back(prog_name);
custom_args.emplace_back(cli_argument);
custom_args.emplace_back(vt_no_terminate);
custom_args.emplace_back(vt_input_config);
custom_args.emplace_back(strdup(vt_input_config.c_str()));
custom_args.emplace_back(vt_lb_name);
custom_args.emplace_back(nullptr);

Expand All @@ -188,7 +192,7 @@ TEST_F(TestInitialization, test_initialize_with_file_and_args) {
int this_rank;
MPI_Comm_rank(comm, &this_rank);
if (this_rank == 0) {
std::ofstream cfg_file_{"test_cfg.toml", std::ofstream::out | std::ofstream::trunc};
std::ofstream cfg_file_{config_file.c_str(), std::ofstream::out | std::ofstream::trunc};
cfg_file_ << "vt_lb_name = RandomLB\n";
cfg_file_.close();
}
Expand All @@ -213,13 +217,16 @@ TEST_F(TestInitialization, test_initialize_with_file_args_and_appconfig) {
static char cli_argument[]{"--cli_argument=100"};
static char vt_no_terminate[]{"--vt_no_terminate"};
static char vt_lb_name[]{"--vt_lb_name=RotateLB"};
static char vt_input_config[]{"--vt_input_config=test_cfg.toml"};

std::string config_file(getUniqueFilenameWithRanks(".toml"));
std::string config_flag("--vt_input_config=");
std::string vt_input_config = config_flag + config_file;

std::vector<char*> custom_args;
custom_args.emplace_back(prog_name);
custom_args.emplace_back(cli_argument);
custom_args.emplace_back(vt_no_terminate);
custom_args.emplace_back(vt_input_config);
custom_args.emplace_back(strdup(vt_input_config.c_str()));
custom_args.emplace_back(vt_lb_name);
custom_args.emplace_back(nullptr);

Expand All @@ -234,7 +241,7 @@ TEST_F(TestInitialization, test_initialize_with_file_args_and_appconfig) {
int this_rank;
MPI_Comm_rank(comm, &this_rank);
if (this_rank == 0) {
std::ofstream cfg_file_{"test_cfg.toml", std::ofstream::out | std::ofstream::trunc};
std::ofstream cfg_file_{config_file.c_str(), std::ofstream::out | std::ofstream::trunc};
cfg_file_ << "vt_lb_name = RandomLB\n";
cfg_file_.close();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "vt/context/context.h"
#include <mpi.h>
#include <gtest/gtest.h>
#include <sstream>

namespace vt { namespace tests { namespace unit {

Expand Down Expand Up @@ -75,6 +76,31 @@ inline bool isOversubscribed() {
return num_ranks > CMAKE_DETECTED_MAX_NUM_NODES;
}

/**
* Get a unique filename based on the unit test name.
*/
inline std::string getUniqueFilename(const std::string& ext = "") {
std::stringstream ss;
ss << testing::UnitTest::GetInstance()->current_test_info()->test_suite_name()
<< "_" << testing::UnitTest::GetInstance()->current_test_info()->name()
<< ext;
std::string str(ss.str());
std::replace(str.begin(), str.end(), '/', '_');
return str;
}

/**
* Construct a filename containing the number of ranks so that
* concurrently-running tests will not cause file system race conditions.
* Do not call this from .nompi.cc tests or from addAdditionalArgs().
*/
inline std::string getUniqueFilenameWithRanks(const std::string& ext = "") {
auto ranks = theContext()->getNumNodes();
std::stringstream ss;
ss << getUniqueFilename() << "_" << ranks << ext;
return ss.str();
}

/**
* The following helper macros (these have to be macros, because GTEST_SKIP
* won't work from nested call) are meant to ensure that the test will be
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_parallel_harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ struct TestParallelHarnessAny : TestHarnessAny<TestBase> {
* addArgs(vt_lb_data, vt_lb_data_dir, vt_lb_data_file);
* }
* };
*
* Make sure all filenames used will be unique across all tests,
* parameterizations, and MPI rank counts.
*/
virtual void addAdditionalArgs() {}

Expand Down
9 changes: 5 additions & 4 deletions tests/unit/trace/test_trace_spec_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <vt/trace/file_spec/spec.h>

#include "test_parallel_harness.h"
#include "test_helpers.h"

#include <fstream>

Expand All @@ -57,7 +58,7 @@ using TestTraceSpec = TestParallelHarness;
TEST_F(TestTraceSpec, test_trace_spec_1) {
using Spec = vt::trace::file_spec::TraceSpec;

std::string file_name = "test_trace_spec_1.txt";
std::string const file_name(getUniqueFilenameWithRanks(".txt"));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -88,7 +89,7 @@ TEST_F(TestTraceSpec, test_trace_spec_1) {
TEST_F(TestTraceSpec, test_trace_spec_2) {
using Spec = vt::trace::file_spec::TraceSpec;

std::string file_name = "test_trace_spec_2.txt";
std::string const file_name(getUniqueFilenameWithRanks(".txt"));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -122,7 +123,7 @@ TEST_F(TestTraceSpec, test_trace_spec_2) {
TEST_F(TestTraceSpec, test_trace_spec_3) {
using Spec = vt::trace::file_spec::TraceSpec;

std::string file_name = "test_trace_spec_3.txt";
std::string const file_name(getUniqueFilenameWithRanks(".txt"));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -167,7 +168,7 @@ TEST_F(TestTraceSpec, test_trace_spec_3) {
TEST_F(TestTraceSpec, test_trace_spec_4) {
using Spec = vt::trace::file_spec::TraceSpec;

std::string file_name = "test_trace_spec_4.txt";
std::string const file_name(getUniqueFilenameWithRanks(".txt"));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down

0 comments on commit edab5ac

Please sign in to comment.