Skip to content

Commit 37679e3

Browse files
Support simulation mode for unit test targets
1 parent 6536bb2 commit 37679e3

5 files changed

Lines changed: 65 additions & 6 deletions

File tree

fdbclient/fdbclient_test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
*/
2020

2121
#include "flow/UnitTestRunner.h"
22+
#include "fdbrpc/simulator.h"
23+
24+
namespace {
25+
26+
void initializeSimulation() {
27+
startNewSimulator(false);
28+
Sim2FileSystem::newFileSystem();
29+
}
30+
31+
} // namespace
2232

2333
int main(int argc, char** argv) {
24-
return runUnitTests(argc, argv, UnitTestRunnerConfig("fdbclient"));
34+
return runUnitTests(argc, argv, UnitTestRunnerConfig("fdbclient", initializeSimulation));
2535
}

fdbrpc/fdbrpc_test.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
*/
2020

2121
#include "flow/UnitTestRunner.h"
22+
#include "fdbrpc/simulator.h"
23+
24+
namespace {
25+
26+
void initializeSimulation() {
27+
startNewSimulator(false);
28+
Sim2FileSystem::newFileSystem();
29+
}
30+
31+
} // namespace
2232

2333
int main(int argc, char** argv) {
24-
return runUnitTests(argc, argv, UnitTestRunnerConfig("fdbrpc"));
34+
return runUnitTests(argc, argv, UnitTestRunnerConfig("fdbrpc", initializeSimulation));
2535
}

fdbserver/FDBServerUnitTestMain.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@
1919
*/
2020

2121
#include "flow/UnitTestRunner.h"
22+
#include "fdbrpc/simulator.h"
2223

2324
#ifndef FDBSERVER_UNIT_TEST_SUITE
2425
#error "FDBSERVER_UNIT_TEST_SUITE must be defined"
2526
#endif
2627

28+
namespace {
29+
30+
void initializeSimulation() {
31+
startNewSimulator(false);
32+
Sim2FileSystem::newFileSystem();
33+
}
34+
35+
} // namespace
36+
2737
int main(int argc, char** argv) {
28-
return runUnitTests(argc, argv, UnitTestRunnerConfig(FDBSERVER_UNIT_TEST_SUITE));
38+
return runUnitTests(argc, argv, UnitTestRunnerConfig(FDBSERVER_UNIT_TEST_SUITE, initializeSimulation));
2939
}

flow/UnitTestRunner.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct UnitTestRunnerOptions {
5353
int maxTestCases = -1;
5454
bool cleanupAfterTests = true;
5555
bool listTests = false;
56+
bool simulation = false;
5657
bool showHelp = false;
5758
};
5859

@@ -71,6 +72,7 @@ enum UnitTestRunnerOption {
7172
OPT_MAX_TEST_CASES,
7273
OPT_NO_CLEANUP,
7374
OPT_LIST,
75+
OPT_SIMULATION,
7476
};
7577

7678
CSimpleOpt::SOption unitTestRunnerOptions[] = { { OPT_HELP, "-h", SO_NONE },
@@ -83,6 +85,7 @@ CSimpleOpt::SOption unitTestRunnerOptions[] = { { OPT_HELP, "-h", SO_NONE },
8385
{ OPT_MAX_TEST_CASES, "--max-test-cases", SO_REQ_SEP },
8486
{ OPT_NO_CLEANUP, "--no-cleanup", SO_NONE },
8587
{ OPT_LIST, "--list", SO_NONE },
88+
{ OPT_SIMULATION, "--simulation", SO_NONE },
8689
SO_END_OF_OPTIONS };
8790

8891
void printUsage(const char* program, const UnitTestRunnerConfig& config) {
@@ -99,6 +102,7 @@ void printUsage(const char* program, const UnitTestRunnerConfig& config) {
99102
" --max-test-cases N Stop after N matching tests\n"
100103
" --no-cleanup Keep the data directory after each test\n"
101104
" --list Print matching test names without running them\n"
105+
" --simulation Run using Sim2 (when supported by the target)\n"
102106
" -h, --help Show this help\n",
103107
program,
104108
config.suiteName(),
@@ -181,6 +185,9 @@ bool parseArgs(int argc, char** argv, UnitTestRunnerOptions* options) {
181185
case OPT_LIST:
182186
options->listTests = true;
183187
break;
188+
case OPT_SIMULATION:
189+
options->simulation = true;
190+
break;
184191
default:
185192
fmt::print(stderr, "ERROR: Unknown option id {}\n", args.OptionId());
186193
return false;
@@ -335,7 +342,8 @@ Future<Void> stopNetworkAfter(Future<Void> what, std::string_view traceName, int
335342

336343
} // namespace
337344

338-
UnitTestRunnerConfig::UnitTestRunnerConfig(std::string_view sourceSubDir) : sourceSubDir(sourceSubDir) {}
345+
UnitTestRunnerConfig::UnitTestRunnerConfig(std::string_view sourceSubDir, SimulationInitializer simulationInitializer)
346+
: sourceSubDir(sourceSubDir), simulationInitializer(simulationInitializer) {}
339347

340348
std::string_view UnitTestRunnerConfig::suiteName() const {
341349
return sourceSubDir;
@@ -349,6 +357,14 @@ std::string UnitTestRunnerConfig::traceName() const {
349357
return std::string(sourceSubDir) + "_test";
350358
}
351359

360+
bool UnitTestRunnerConfig::supportsSimulation() const {
361+
return simulationInitializer != nullptr;
362+
}
363+
364+
void UnitTestRunnerConfig::initializeSimulation() const {
365+
simulationInitializer();
366+
}
367+
352368
int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config) {
353369
platformInit();
354370
Error::init();
@@ -365,6 +381,10 @@ int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config) {
365381
printUsage(argv[0], config);
366382
return 0;
367383
}
384+
if (options.simulation && !config.supportsSimulation()) {
385+
fmt::print(stderr, "ERROR: Simulation mode is not supported by the {} test target\n", config.suiteName());
386+
return 1;
387+
}
368388

369389
if (options.randomSeed == 0) {
370390
options.randomSeed = platform::getRandomSeed();
@@ -381,7 +401,11 @@ int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config) {
381401
return 1;
382402
}
383403

384-
g_network = newNet2(TLSConfig());
404+
if (options.simulation) {
405+
config.initializeSimulation();
406+
} else {
407+
g_network = newNet2(TLSConfig());
408+
}
385409
openTraceFile({}, 10 << 20, 10 << 20, ".", traceName);
386410

387411
int exitCode = 0;

flow/include/flow/UnitTestRunner.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@
2727

2828
class UnitTestRunnerConfig {
2929
public:
30-
explicit UnitTestRunnerConfig(std::string_view sourceSubDir);
30+
using SimulationInitializer = void (*)();
31+
32+
explicit UnitTestRunnerConfig(std::string_view sourceSubDir, SimulationInitializer simulationInitializer = nullptr);
3133

3234
std::string_view suiteName() const;
3335
std::string dataDir() const;
3436
std::string traceName() const;
37+
bool supportsSimulation() const;
38+
void initializeSimulation() const;
3539

3640
private:
3741
std::string_view sourceSubDir;
42+
SimulationInitializer simulationInitializer;
3843
};
3944

4045
int runUnitTests(int argc, char** argv, const UnitTestRunnerConfig& config);

0 commit comments

Comments
 (0)