Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2752)

Unified Diff: base/timer/timer_unittest.cc

Issue 637983003: Add support to base::Timer for custom task runners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final rebase Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/timer/timer.cc ('k') | build/get_landmines.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/timer/timer_unittest.cc
diff --git a/base/timer/timer_unittest.cc b/base/timer/timer_unittest.cc
index 0fb2b454ad764f7497dca7878910bee3ca701238..1cbccd1626d558d912a267212325856aa3eff86c 100644
--- a/base/timer/timer_unittest.cc
+++ b/base/timer/timer_unittest.cc
@@ -4,10 +4,12 @@
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
+#include "base/test/test_simple_task_runner.h"
#include "base/timer/timer.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::TimeDelta;
+using base::SingleThreadTaskRunner;
namespace {
@@ -26,20 +28,32 @@ class OneShotTimerTester {
public:
explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10)
: did_run_(did_run),
- delay_ms_(milliseconds) {
+ delay_ms_(milliseconds),
+ quit_message_loop_(true) {
}
+
void Start() {
timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this,
&OneShotTimerTester::Run);
}
+
+ void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
+ quit_message_loop_ = false;
+ timer_.SetTaskRunner(task_runner);
+ }
+
private:
void Run() {
*did_run_ = true;
- base::MessageLoop::current()->QuitWhenIdle();
+ if (quit_message_loop_) {
+ base::MessageLoop::current()->QuitWhenIdle();
+ }
}
+
bool* did_run_;
base::OneShotTimer<OneShotTimerTester> timer_;
const unsigned delay_ms_;
+ bool quit_message_loop_;
};
class OneShotSelfDeletingTimerTester {
@@ -48,16 +62,19 @@ class OneShotSelfDeletingTimerTester {
did_run_(did_run),
timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) {
}
+
void Start() {
timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this,
&OneShotSelfDeletingTimerTester::Run);
}
+
private:
void Run() {
*did_run_ = true;
timer_.reset();
base::MessageLoop::current()->QuitWhenIdle();
}
+
bool* did_run_;
scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_;
};
@@ -71,6 +88,7 @@ class RepeatingTimerTester {
void Start() {
timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run);
}
+
private:
void Run() {
if (--counter_ == 0) {
@@ -79,6 +97,7 @@ class RepeatingTimerTester {
base::MessageLoop::current()->QuitWhenIdle();
}
}
+
bool* did_run_;
int counter_;
TimeDelta delay_;
@@ -310,6 +329,20 @@ TEST(TimerTest, OneShotSelfDeletingTimer) {
}
}
+TEST(TimerTest, OneShotTimer_CustomTaskRunner) {
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner =
+ new base::TestSimpleTaskRunner();
+
+ bool did_run = false;
+ OneShotTimerTester f(&did_run);
+ f.SetTaskRunner(task_runner);
+ f.Start();
+
+ EXPECT_FALSE(did_run);
+ task_runner->RunUntilIdle();
+ EXPECT_TRUE(did_run);
+}
+
TEST(TimerTest, RepeatingTimer) {
for (int i = 0; i < kNumTestingMessageLoops; i++) {
RunTest_RepeatingTimer(testing_message_loops[i],
« no previous file with comments | « base/timer/timer.cc ('k') | build/get_landmines.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698