Commit e120a88b authored by mithro's avatar mithro Committed by Commit bot

Adding tracing and gtest pretty printing support to TestPendingTask.

These functions where invaluable when trying to debug and fix the
cc::OrderedTestRunner as they let me easily inspect the tasks without having to
fire up GDB. They should also be useful in development / testing of other task
runners.

Added a very simple test for ShouldRunBefore which shows this stuff working. If
that test fails, the output looks like the following;
-------------------------------------------------------------------------------
../../base/test/test_pending_task_unittest.cc:49: Failure
Value of: task_after.ShouldRunBefore(task_first)
  Actual: false
Expected: true
TestPendingTask(
  {"delay":2000,"nestability":"NESTABLE","post_time":0,
   "posting_function":"Unknown@Unknown:-1","run_at":2000}
).ShouldRunBefore(TestPendingTask(
  {"delay":1000,"nestability":"NESTABLE","post_time":0,
   "posting_function":"Unknown@Unknown:-1","run_at":1000})
)
-------------------------------------------------------------------------------

BUG=380889

Review URL: https://codereview.chromium.org/491743002

Cr-Commit-Position: refs/heads/master@{#292370}
parent 287ca0fe
......@@ -593,6 +593,7 @@
'test/expectations/expectation_unittest.cc',
'test/expectations/parser_unittest.cc',
'test/histogram_tester_unittest.cc',
'test/test_pending_task_unittest.cc',
'test/test_reg_util_win_unittest.cc',
'test/trace_event_analyzer_unittest.cc',
'threading/non_thread_safe_unittest.cc',
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/test/test_pending_task.h"
namespace base {
......@@ -32,4 +34,44 @@ bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
TestPendingTask::~TestPendingTask() {}
void TestPendingTask::AsValueInto(base::debug::TracedValue* state) const {
state->SetInteger("run_at", GetTimeToRun().ToInternalValue());
state->SetString("posting_function", location.ToString());
state->SetInteger("post_time", post_time.ToInternalValue());
state->SetInteger("delay", delay.ToInternalValue());
switch (nestability) {
case NESTABLE:
state->SetString("nestability", "NESTABLE");
break;
case NON_NESTABLE:
state->SetString("nestability", "NON_NESTABLE");
break;
}
state->SetInteger("delay", delay.ToInternalValue());
}
scoped_refptr<base::debug::ConvertableToTraceFormat> TestPendingTask::AsValue()
const {
scoped_refptr<base::debug::TracedValue> state =
new base::debug::TracedValue();
AsValueInto(state);
return state;
}
std::string TestPendingTask::ToString() const {
std::string output("TestPendingTask(");
AsValue()->AppendAsTraceFormat(&output);
output += ")";
return output;
}
std::ostream& operator<<(std::ostream& os, const TestPendingTask& task) {
PrintTo(task, &os);
return os;
}
void PrintTo(const TestPendingTask& task, std::ostream* os) {
*os << task.ToString();
}
} // namespace base
......@@ -5,7 +5,10 @@
#ifndef BASE_TEST_TEST_PENDING_TASK_H_
#define BASE_TEST_TEST_PENDING_TASK_H_
#include <string>
#include "base/callback.h"
#include "base/debug/trace_event_argument.h"
#include "base/location.h"
#include "base/time/time.h"
......@@ -51,8 +54,19 @@ struct TestPendingTask {
TimeTicks post_time;
TimeDelta delay;
TestNestability nestability;
// Functions for using test pending task with tracing, useful in unit
// testing.
void AsValueInto(base::debug::TracedValue* state) const;
scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const;
std::string ToString() const;
};
// gtest helpers which allow pretty printing of the tasks, very useful in unit
// testing.
std::ostream& operator<<(std::ostream& os, const TestPendingTask& task);
void PrintTo(const TestPendingTask& task, std::ostream* os);
} // namespace base
#endif // BASE_TEST_TEST_TASK_RUNNER_H_
#endif // BASE_TEST_TEST_PENDING_TASK_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test/test_pending_task.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
TEST(TestPendingTaskTest, TraceSupport) {
base::TestPendingTask task;
// Check that TestPendingTask can be sent to the trace subsystem.
TRACE_EVENT1("test", "TestPendingTask::TraceSupport", "task", task.AsValue());
// Just a basic check that the trace output has *something* in it.
EXPECT_THAT(task.AsValue()->ToString(), ::testing::HasSubstr("post_time"));
}
TEST(TestPendingTaskTest, ToString) {
base::TestPendingTask task;
// Just a basic check that ToString has *something* in it.
EXPECT_THAT(task.ToString(), ::testing::StartsWith("TestPendingTask("));
}
TEST(TestPendingTaskTest, GTestPrettyPrint) {
base::TestPendingTask task;
// Check that gtest is calling the TestPendingTask's PrintTo method.
EXPECT_THAT(::testing::PrintToString(task),
::testing::StartsWith("TestPendingTask("));
// Check that pretty printing works with the gtest iostreams operator.
EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << task, "TestPendingTask(");
}
TEST(TestPendingTaskTest, ShouldRunBefore) {
base::TestPendingTask task_first;
task_first.delay = base::TimeDelta::FromMilliseconds(1);
base::TestPendingTask task_after;
task_after.delay = base::TimeDelta::FromMilliseconds(2);
EXPECT_FALSE(task_after.ShouldRunBefore(task_first))
<< task_after << ".ShouldRunBefore(" << task_first << ")\n";
EXPECT_TRUE(task_first.ShouldRunBefore(task_after))
<< task_first << ".ShouldRunBefore(" << task_after << ")\n";
}
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment