Skip to content

Commit 4db5fe3

Browse files
committed
Relax CHECK condition in benchmark_runner.cc
If the benchmark state contains an error, do not expect any iterations has been run. This allows using SkipWithError() and return early from the benchmark function.
1 parent cc04db6 commit 4db5fe3

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/benchmark_runner.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void RunInThread(const BenchmarkInstance* b, IterationCount iters,
117117
? internal::ThreadTimer::CreateProcessCpuTime()
118118
: internal::ThreadTimer::Create());
119119
State st = b->Run(iters, thread_id, &timer, manager);
120-
CHECK(st.iterations() >= st.max_iterations)
120+
CHECK(st.error_occurred() || st.iterations() >= st.max_iterations)
121121
<< "Benchmark returned before State::KeepRunning() returned false!";
122122
{
123123
MutexLock l(manager->GetBenchmarkMutex());

test/skip_with_error_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ int AddCases(const char* base_name, std::initializer_list<TestCase> const& v) {
6161

6262
} // end namespace
6363

64+
void BM_error_no_running(benchmark::State& state) {
65+
state.SkipWithError("error message");
66+
}
67+
BENCHMARK(BM_error_no_running);
68+
ADD_CASES("BM_error_no_running", {{"", true, "error message"}});
69+
6470
void BM_error_before_running(benchmark::State& state) {
6571
state.SkipWithError("error message");
6672
while (state.KeepRunning()) {

0 commit comments

Comments
 (0)