tests: create Test instance only when it is needed

GitOrigin-RevId: 06bd6063018040bfee9e3081d77c24683940eb34
This commit is contained in:
Arseny Smirnov 2020-08-04 15:22:16 +03:00
parent 1b628d3ab7
commit f74d8ba023
2 changed files with 15 additions and 6 deletions

View File

@ -138,13 +138,13 @@ TestsRunner &TestsRunner::get_default() {
return default_runner; return default_runner;
} }
void TestsRunner::add_test(string name, unique_ptr<Test> test) { void TestsRunner::add_test(string name, std::function<unique_ptr<Test>()> test) {
for (auto &it : tests_) { for (auto &it : tests_) {
if (it.first == name) { if (it.first == name) {
LOG(FATAL) << "Test name collision " << name; LOG(FATAL) << "Test name collision " << name;
} }
} }
tests_.emplace_back(name, std::move(test)); tests_.emplace_back(name, TestInfo{std::move(test), nullptr});
} }
void TestsRunner::add_substr_filter(string str) { void TestsRunner::add_substr_filter(string str) {
@ -176,7 +176,7 @@ bool TestsRunner::run_all_step() {
while (state_.it != state_.end) { while (state_.it != state_.end) {
auto &name = tests_[state_.it].first; auto &name = tests_[state_.it].first;
auto test = tests_[state_.it].second.get(); auto &test = tests_[state_.it].second.test;
if (!state_.is_running) { if (!state_.is_running) {
bool ok = true; bool ok = true;
for (const auto &filter : substr_filters_) { for (const auto &filter : substr_filters_) {
@ -194,12 +194,17 @@ bool TestsRunner::run_all_step() {
state_.start = Time::now(); state_.start = Time::now();
state_.start_unadjusted = Time::now_unadjusted(); state_.start_unadjusted = Time::now_unadjusted();
state_.is_running = true; state_.is_running = true;
CHECK(!test);
test = tests_[state_.it].second.creator();
} }
if (test->step()) { if (test->step()) {
break; break;
} }
test = {};
auto passed = Time::now() - state_.start; auto passed = Time::now() - state_.start;
auto real_passed = Time::now_unadjusted() - state_.start_unadjusted; auto real_passed = Time::now_unadjusted() - state_.start_unadjusted;
if (real_passed + 1e-1 > passed) { if (real_passed + 1e-1 > passed) {

View File

@ -96,7 +96,7 @@ class TestsRunner : public TestContext {
public: public:
static TestsRunner &get_default(); static TestsRunner &get_default();
void add_test(string name, unique_ptr<Test> test); void add_test(string name, std::function<unique_ptr<Test>()> test);
void add_substr_filter(string str); void add_substr_filter(string str);
void set_stress_flag(bool flag); void set_stress_flag(bool flag);
void run_all(); void run_all();
@ -113,7 +113,11 @@ class TestsRunner : public TestContext {
}; };
bool stress_flag_{false}; bool stress_flag_{false};
vector<string> substr_filters_; vector<string> substr_filters_;
vector<std::pair<string, unique_ptr<Test>>> tests_; struct TestInfo {
std::function<unique_ptr<Test>()> creator;
unique_ptr<Test> test;
};
vector<std::pair<string, TestInfo>> tests_;
State state_; State state_;
unique_ptr<RegressionTester> regression_tester_; unique_ptr<RegressionTester> regression_tester_;
@ -125,7 +129,7 @@ template <class T>
class RegisterTest { class RegisterTest {
public: public:
explicit RegisterTest(string name, TestsRunner &runner = TestsRunner::get_default()) { explicit RegisterTest(string name, TestsRunner &runner = TestsRunner::get_default()) {
runner.add_test(name, make_unique<T>()); runner.add_test(name, [] { return make_unique<T>(); });
} }
}; };