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 3, 2022
1 parent e8ca3b5 commit f9983cf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
16 changes: 12 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,9 @@ 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(addNumNodesToFilename(
"test_checkpoint_1_dir_"
));
std::string const expected_label{"test_checkpoint_1"};

{
Expand Down Expand Up @@ -251,7 +253,9 @@ 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(addNumNodesToFilename(
"test_checkpoint_in_place_2_dir_"
));
auto proxy = vt::theCollection()->constructCollective<TestCol>(
range, "test_checkpoint_in_place_2"
);
Expand Down Expand Up @@ -323,7 +327,9 @@ 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(addNumNodesToFilename(
"test_checkpoint_in_place_3_dir_"
));
auto proxy = vt::theCollection()->constructCollective<TestCol>(
range, "test_checkpoint_in_place_3"
);
Expand Down Expand Up @@ -400,7 +406,9 @@ 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(addNumNodesToFilename(
"test_null_elm_checkpoint_dir_"
));

{
auto proxy = vt::makeCollection<TestCol>("test_checkpoint_no_elements_on_root_rank")
Expand Down
21 changes: 17 additions & 4 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,7 +171,13 @@ 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(addNumNodesToFilename("test_cfg_", ".toml"));
std::string config_flag("--vt_input_config=");
std::string config_arg = config_flag + config_file;
char vt_input_config[100];
memset(vt_input_config, 0, 100);
strncpy(vt_input_config, config_arg.c_str(), config_arg.size());

std::vector<char *> custom_args;
custom_args.emplace_back(prog_name);
Expand All @@ -188,7 +195,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,7 +220,13 @@ 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(addNumNodesToFilename("test_appcfg_", ".toml"));
std::string config_flag("--vt_input_config=");
std::string config_arg = config_flag + config_file;
char vt_input_config[100];
memset(vt_input_config, 0, 100);
strncpy(vt_input_config, config_arg.c_str(), config_arg.size());

std::vector<char*> custom_args;
custom_args.emplace_back(prog_name);
Expand All @@ -234,7 +247,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
15 changes: 15 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,20 @@ inline bool isOversubscribed() {
return num_ranks > CMAKE_DETECTED_MAX_NUM_NODES;
}

/**
* 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.
*/
inline std::string addNumNodesToFilename(
const std::string &before_num_ranks, const std::string &after_num_ranks = ""
) {
auto ranks = theContext()->getNumNodes();
std::stringstream ss;
ss << before_num_ranks << ranks << after_num_ranks;
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
17 changes: 13 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,9 @@ 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(addNumNodesToFilename(
"test_trace_spec_1_", ".txt"
));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -88,7 +91,9 @@ 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(addNumNodesToFilename(
"test_trace_spec_2_", ".txt"
));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -122,7 +127,9 @@ 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(addNumNodesToFilename(
"test_trace_spec_3_", ".txt"
));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down Expand Up @@ -167,7 +174,9 @@ 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(addNumNodesToFilename(
"test_trace_spec_4_", ".txt"
));
if (theContext()->getNode() == 0) {
std::ofstream out(file_name);
out << ""
Expand Down

0 comments on commit f9983cf

Please sign in to comment.