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

Unified Diff: base/message_loop_unittest.cc

Issue 6463013: Add support for base::Closure in the MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: work around __stdcall Created 9 years, 10 months 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
« base/message_loop.cc ('K') | « base/message_loop.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/message_loop_unittest.cc
diff --git a/base/message_loop_unittest.cc b/base/message_loop_unittest.cc
index 9576a5818dc902053216dc6cfe9cbbdaf779acec..482fe7e80634ebde25f14ac4905e7eb1f1cbdfec 100644
--- a/base/message_loop_unittest.cc
+++ b/base/message_loop_unittest.cc
@@ -4,6 +4,8 @@
#include <vector>
+#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/eintr_wrapper.h"
#include "base/logging.h"
#include "base/message_loop.h"
willchan no longer on Chromium 2011/02/15 01:41:11 This should go first.
@@ -92,6 +94,36 @@ class QuitMsgLoop : public base::RefCounted<QuitMsgLoop> {
~QuitMsgLoop() {}
};
+void RunTest_PostClosure(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Add tests to message loop
+ scoped_refptr<Foo> foo(new Foo());
+ std::string a("a"), b("b"), c("c"), d("d");
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test0, foo.get()));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1ConstRef, foo.get(), a));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1Ptr, foo.get(), &b));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1Int, foo.get(), 100));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test2Ptr, foo.get(), &a, &c));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test2Mixed, foo.get(), a, &d));
+
+ // After all tests, post a message that will shut down the message loop
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
+
+ // Now kick things off
+ MessageLoop::current()->Run();
+
+ EXPECT_EQ(foo->test_count(), 105);
+ EXPECT_EQ(foo->result(), "abacad");
+}
+
void RunTest_PostTask(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -123,6 +155,38 @@ void RunTest_PostTask(MessageLoop::Type message_loop_type) {
EXPECT_EQ(foo->result(), "abacad");
}
+void RunTest_PostClosure_SEH(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Add tests to message loop
+ scoped_refptr<Foo> foo(new Foo());
+ std::string a("a"), b("b"), c("c"), d("d");
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test0, foo.get()));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1ConstRef,foo.get(), a));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1Ptr,foo.get(), &b));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test1Int,foo.get(), 100));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test2Ptr,foo.get(), &a, &c));
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &Foo::Test2Mixed,foo.get(), a, &d));
+
+ // After all tests, post a message that will shut down the message loop
+ MessageLoop::current()->PostClosure(FROM_HERE, base::Bind(
+ &MessageLoop::Quit, base::Unretained(MessageLoop::current())));
+
+ // Now kick things off with the SEH block active.
+ MessageLoop::current()->set_exception_restoration(true);
+ MessageLoop::current()->Run();
+ MessageLoop::current()->set_exception_restoration(false);
+
+ EXPECT_EQ(foo->test_count(), 105);
+ EXPECT_EQ(foo->result(), "abacad");
+}
+
void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -156,6 +220,13 @@ void RunTest_PostTask_SEH(MessageLoop::Type message_loop_type) {
EXPECT_EQ(foo->result(), "abacad");
}
+// This Func runs slowly to simulate a large amount of work being done.
+static void SlowFunc(int pause_ms, int* quit_counter) {
+ PlatformThread::Sleep(pause_ms);
+ if (--(*quit_counter) == 0)
+ MessageLoop::current()->Quit();
+}
+
// This class runs slowly to simulate a large amount of work being done.
class SlowTask : public Task {
public:
@@ -163,9 +234,7 @@ class SlowTask : public Task {
: pause_ms_(pause_ms), quit_counter_(quit_counter) {
}
virtual void Run() {
- PlatformThread::Sleep(pause_ms_);
- if (--(*quit_counter_) == 0)
- MessageLoop::current()->Quit();
+ SlowFunc(pause_ms_, quit_counter_);
}
private:
int pause_ms_;
@@ -174,22 +243,52 @@ class SlowTask : public Task {
// This class records the time when Run was called in a Time object, which is
// useful for building a variety of MessageLoop tests.
-class RecordRunTimeTask : public SlowTask {
+static void RecordRunTimeFunc(Time* run_time, int* quit_counter) {
+ *run_time = Time::Now();
+
+ // Cause our Run function to take some time to execute. As a result we can
+ // count on subsequent RecordRunTimeFunc()s running at a future time,
+ // without worry about the resolution of our system clock being an issue.
+ SlowFunc(10, quit_counter);
+}
+
+// This class records the time when Run was called in a Time object, which is
+// useful for building a variety of MessageLoop tests.
+class RecordRunTimeTask : public Task {
public:
RecordRunTimeTask(Time* run_time, int* quit_counter)
- : SlowTask(10, quit_counter), run_time_(run_time) {
+ : run_time_(run_time), quit_counter_(quit_counter) {
}
virtual void Run() {
- *run_time_ = Time::Now();
- // Cause our Run function to take some time to execute. As a result we can
- // count on subsequent RecordRunTimeTask objects running at a future time,
- // without worry about the resolution of our system clock being an issue.
- SlowTask::Run();
+ RecordRunTimeFunc(run_time_, quit_counter_);
}
private:
Time* run_time_;
+ int* quit_counter_;
};
+void RunTest_PostDelayedClosure_Basic(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that PostDelayedClosureTask results in a delayed task.
+
+ const int kDelayMS = 100;
+
+ int num_tasks = 1;
+ Time run_time;
+
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
+ kDelayMS);
+
+ Time time_before_run = Time::Now();
+ loop.Run();
+ Time time_after_run = Time::Now();
+
+ EXPECT_EQ(0, num_tasks);
+ EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds());
+}
+
void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -211,6 +310,28 @@ void RunTest_PostDelayedTask_Basic(MessageLoop::Type message_loop_type) {
EXPECT_LT(kDelayMS, (time_after_run - time_before_run).InMilliseconds());
}
+void RunTest_PostDelayedClosure_InDelayOrder(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that two tasks with different delays run in the right order.
+
+ int num_tasks = 2;
+ Time run_time1, run_time2;
+
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks), 200);
+ // If we get a large pause in execution (due to a context switch) here, this
+ // test could fail.
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10);
+
+ loop.Run();
+ EXPECT_EQ(0, num_tasks);
+
+ EXPECT_TRUE(run_time2 < run_time1);
+}
+
void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -232,6 +353,38 @@ void RunTest_PostDelayedTask_InDelayOrder(MessageLoop::Type message_loop_type) {
EXPECT_TRUE(run_time2 < run_time1);
}
+void RunTest_PostDelayedClosure_InPostOrder(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that two tasks with the same delay run in the order in which they
+ // were posted.
+ //
+ // NOTE: This is actually an approximate test since the API only takes a
+ // "delay" parameter, so we are not exactly simulating two tasks that get
+ // posted at the exact same time. It would be nice if the API allowed us to
+ // specify the desired run time.
+
+ const int kDelayMS = 100;
+
+ int num_tasks = 2;
+ Time run_time1, run_time2;
+
+ loop.PostDelayedClosure(
+ FROM_HERE,
+ base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks),
+ kDelayMS);
+ loop.PostDelayedClosure(
+ FROM_HERE,
+ base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks),
+ kDelayMS);
+
+ loop.Run();
+ EXPECT_EQ(0, num_tasks);
+
+ EXPECT_TRUE(run_time1 < run_time2);
+}
+
void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -259,6 +412,32 @@ void RunTest_PostDelayedTask_InPostOrder(MessageLoop::Type message_loop_type) {
EXPECT_TRUE(run_time1 < run_time2);
}
+void RunTest_PostDelayedClosure_InPostOrder_2(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that a delayed task still runs after a normal tasks even if the
+ // normal tasks take a long time to run.
+
+ const int kPauseMS = 50;
+
+ int num_tasks = 2;
+ Time run_time;
+
+ loop.PostClosure(
+ FROM_HERE, base::Bind(&SlowFunc, kPauseMS, &num_tasks));
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks), 10);
+
+ Time time_before_run = Time::Now();
+ loop.Run();
+ Time time_after_run = Time::Now();
+
+ EXPECT_EQ(0, num_tasks);
+
+ EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds());
+}
+
void RunTest_PostDelayedTask_InPostOrder_2(
MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -285,6 +464,33 @@ void RunTest_PostDelayedTask_InPostOrder_2(
EXPECT_LT(kPauseMS, (time_after_run - time_before_run).InMilliseconds());
}
+void RunTest_PostDelayedClosure_InPostOrder_3(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that a delayed task still runs after a pile of normal tasks. The key
+ // difference between this test and the previous one is that here we return
+ // the MessageLoop a lot so we give the MessageLoop plenty of opportunities
+ // to maybe run the delayed task. It should know not to do so until the
+ // delayed task's delay has passed.
+
+ int num_tasks = 11;
+ Time run_time1, run_time2;
+
+ // Clutter the ML with tasks.
+ for (int i = 1; i < num_tasks; ++i)
+ loop.PostClosure(FROM_HERE,
+ base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks));
+
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 1);
+
+ loop.Run();
+ EXPECT_EQ(0, num_tasks);
+
+ EXPECT_TRUE(run_time2 > run_time1);
+}
+
void RunTest_PostDelayedTask_InPostOrder_3(
MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -311,6 +517,44 @@ void RunTest_PostDelayedTask_InPostOrder_3(
EXPECT_TRUE(run_time2 > run_time1);
}
+void RunTest_PostDelayedClosure_SharedTimer(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ // Test that the interval of the timer, used to run the next delayed task, is
+ // set to a value corresponding to when the next delayed task should run.
+
+ // By setting num_tasks to 1, we ensure that the first task to run causes the
+ // run loop to exit.
+ int num_tasks = 1;
+ Time run_time1, run_time2;
+
+ loop.PostDelayedClosure(
+ FROM_HERE,
+ base::Bind(&RecordRunTimeFunc, &run_time1, &num_tasks),
+ 1000000);
+ loop.PostDelayedClosure(
+ FROM_HERE, base::Bind(&RecordRunTimeFunc, &run_time2, &num_tasks), 10);
+
+ Time start_time = Time::Now();
+
+ loop.Run();
+ EXPECT_EQ(0, num_tasks);
+
+ // Ensure that we ran in far less time than the slower timer.
+ TimeDelta total_time = Time::Now() - start_time;
+ EXPECT_GT(5000, total_time.InMilliseconds());
+
+ // In case both timers somehow run at nearly the same time, sleep a little
+ // and then run all pending to force them both to have run. This is just
+ // encouraging flakiness if there is any.
+ PlatformThread::Sleep(100);
+ loop.RunAllPending();
+
+ EXPECT_TRUE(run_time1.is_null());
+ EXPECT_FALSE(run_time2.is_null());
+}
+
void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -348,16 +592,20 @@ void RunTest_PostDelayedTask_SharedTimer(MessageLoop::Type message_loop_type) {
#if defined(OS_WIN)
+void SubPumpFunc() {
+ MessageLoop::current()->SetNestableTasksAllowed(true);
+ MSG msg;
+ while (GetMessage(&msg, NULL, 0, 0)) {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ MessageLoop::current()->Quit();
+}
+
class SubPumpTask : public Task {
public:
virtual void Run() {
- MessageLoop::current()->SetNestableTasksAllowed(true);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- MessageLoop::current()->Quit();
+ SubPumpFunc();
}
};
@@ -370,6 +618,53 @@ class SubPumpQuitTask : public Task {
}
};
+// Workaround base::Bind not support __stdcall.
+//
+// TODO(ajwong): Add support for platform specific function types into Bind.
+void DoPostQuitMessage(int n) {
+ PostQuitMessage(n);
+}
+
+void RunTest_PostDelayedClosure_SharedTimer_SubPump() {
+ MessageLoop loop(MessageLoop::TYPE_UI);
+
+ // Test that the interval of the timer, used to run the next delayed task, is
+ // set to a value corresponding to when the next delayed task should run.
+
+ // By setting num_tasks to 1, we ensure that the first task to run causes the
+ // run loop to exit.
+ int num_tasks = 1;
+ Time run_time;
+
+ loop.PostClosure(FROM_HERE, base::Bind(&SubPumpFunc));
+
+ // This very delayed task should never run.
+ loop.PostDelayedClosure(
+ FROM_HERE,
+ base::Bind(&RecordRunTimeFunc, &run_time, &num_tasks),
+ 1000000);
+
+ // This slightly delayed task should run from within SubPumpTask::Run().
+ loop.PostDelayedClosure(FROM_HERE, base::Bind(&DoPostQuitMessage, 0), 10);
+
+ Time start_time = Time::Now();
+
+ loop.Run();
+ EXPECT_EQ(1, num_tasks);
+
+ // Ensure that we ran in far less time than the slower timer.
+ TimeDelta total_time = Time::Now() - start_time;
+ EXPECT_GT(5000, total_time.InMilliseconds());
+
+ // In case both timers somehow run at nearly the same time, sleep a little
+ // and then run all pending to force them both to have run. This is just
+ // encouraging flakiness if there is any.
+ PlatformThread::Sleep(100);
+ loop.RunAllPending();
+
+ EXPECT_TRUE(run_time.is_null());
+}
+
void RunTest_PostDelayedTask_SharedTimer_SubPump() {
MessageLoop loop(MessageLoop::TYPE_UI);
@@ -457,6 +752,18 @@ void RunTest_EnsureTaskDeletion_Chain(MessageLoop::Type message_loop_type) {
EXPECT_TRUE(c_was_deleted);
}
+void NestingFunc(int* depth) {
+ if (*depth > 0) {
+ *depth -= 1;
+ MessageLoop::current()->PostClosure(FROM_HERE,
+ base::Bind(&NestingFunc, depth));
+
+ MessageLoop::current()->SetNestableTasksAllowed(true);
+ MessageLoop::current()->Run();
+ }
+ MessageLoop::current()->Quit();
+}
+
class NestingTest : public Task {
public:
explicit NestingTest(int* depth) : depth_(depth) {
@@ -588,6 +895,16 @@ void RunTest_CrasherNasty(MessageLoop::Type message_loop_type) {
#endif // defined(OS_WIN)
+void RunTest_NestingClosure(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ int depth = 100;
+ MessageLoop::current()->PostClosure(FROM_HERE,
+ base::Bind(&NestingFunc, &depth));
+ MessageLoop::current()->Run();
+ EXPECT_EQ(depth, 0);
+}
+
void RunTest_Nesting(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -862,6 +1179,44 @@ class Recursive2Tasks : public Task {
#endif // defined(OS_WIN)
+void RunTest_RecursiveDenialClosure1(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ EXPECT_TRUE(MessageLoop::current()->NestableTasksAllowed());
+ TaskList order;
+ RecursiveTask recursive1(2, &order, 1, false);
+ RecursiveTask recursive2(2, &order, 2, false);
+ QuitTask quit_task(&order, 3);
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&RecursiveTask::Run, base::Unretained(&recursive1)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&RecursiveTask::Run, base::Unretained(&recursive2)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&QuitTask::Run, base::Unretained(&quit_task)));
+
+ MessageLoop::current()->Run();
+
+ // FIFO order.
+ ASSERT_EQ(14U, order.size());
+ EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
+ EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
+ EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
+ EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
+ EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
+}
+
void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -893,6 +1248,43 @@ void RunTest_RecursiveDenial1(MessageLoop::Type message_loop_type) {
EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
}
+void RunTest_RecursiveSupportClosure1(MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ TaskList order;
+ RecursiveTask recursive1(2, &order, 1, true);
+ RecursiveTask recursive2(2, &order, 2, true);
+ QuitTask quit_task(&order, 3);
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&RecursiveTask::Run, base::Unretained(&recursive1)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&RecursiveTask::Run, base::Unretained(&recursive2)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&QuitTask::Run, base::Unretained(&quit_task)));
+
+ MessageLoop::current()->Run();
+
+ // FIFO order.
+ ASSERT_EQ(14U, order.size());
+ EXPECT_EQ(order[ 0], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[ 1], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[ 2], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[ 3], TaskItem(RECURSIVE, 2, false));
+ EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
+ EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
+ EXPECT_EQ(order[ 6], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[ 7], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[ 8], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[ 9], TaskItem(RECURSIVE, 2, false));
+ EXPECT_EQ(order[10], TaskItem(RECURSIVE, 1, true));
+ EXPECT_EQ(order[11], TaskItem(RECURSIVE, 1, false));
+ EXPECT_EQ(order[12], TaskItem(RECURSIVE, 2, true));
+ EXPECT_EQ(order[13], TaskItem(RECURSIVE, 2, false));
+}
+
void RunTest_RecursiveSupport1(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -1037,6 +1429,37 @@ class TaskThatPumps : public OrderedTasks {
};
// Tests that non nestable tasks run in FIFO if there are no nested loops.
+void RunTest_NonNestableClosureWithNoNesting(
+ MessageLoop::Type message_loop_type) {
+ MessageLoop loop(message_loop_type);
+
+ TaskList order;
+
+ OrderedTasks order1(&order, 1);
+ OrderedTasks order2(&order, 2);
+ QuitTask quit_task(&order, 3);
+ MessageLoop::current()->PostNonNestableClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&order1)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&order2)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&QuitTask::Run, base::Unretained(&quit_task)));
+ MessageLoop::current()->Run();
+
+ // FIFO order.
+ ASSERT_EQ(6U, order.size());
+ EXPECT_EQ(order[ 0], TaskItem(ORDERERD, 1, true));
+ EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 1, false));
+ EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 2, true));
+ EXPECT_EQ(order[ 3], TaskItem(ORDERERD, 2, false));
+ EXPECT_EQ(order[ 4], TaskItem(QUITMESSAGELOOP, 3, true));
+ EXPECT_EQ(order[ 5], TaskItem(QUITMESSAGELOOP, 3, false));
+}
+
+// Tests that non nestable tasks run in FIFO if there are no nested loops.
void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) {
MessageLoop loop(message_loop_type);
@@ -1059,6 +1482,71 @@ void RunTest_NonNestableWithNoNesting(MessageLoop::Type message_loop_type) {
}
// Tests that non nestable tasks don't run when there's code in the call stack.
+void RunTest_NonNestableClosureInNestedLoop(
+ MessageLoop::Type message_loop_type,
+ bool use_delayed) {
+ MessageLoop loop(message_loop_type);
+
+ TaskList order;
+
+ TaskThatPumps task_that_pumps(&order, 1);
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&TaskThatPumps::Run, base::Unretained(&task_that_pumps)));
+ OrderedTasks ordered1(&order, 2);
+ if (use_delayed) {
+ MessageLoop::current()->PostNonNestableDelayedClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&ordered1)),
+ 1);
+ } else {
+ MessageLoop::current()->PostNonNestableClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&ordered1)));
+ }
+ OrderedTasks ordered3(&order, 3);
+ SleepTask sleep_task(&order, 4, 50);
+ OrderedTasks ordered5(&order, 5);
+ QuitTask quit_task(&order, 6);
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&ordered3)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&SleepTask::Run, base::Unretained(&sleep_task)));
+ MessageLoop::current()->PostClosure(
+ FROM_HERE,
+ base::Bind(&OrderedTasks::Run, base::Unretained(&ordered5)));
+ if (use_delayed) {
+ MessageLoop::current()->PostNonNestableDelayedClosure(
+ FROM_HERE,
+ base::Bind(&QuitTask::Run, base::Unretained(&quit_task)),
+ 2);
+ } else {
+ MessageLoop::current()->PostNonNestableClosure(
+ FROM_HERE,
+ base::Bind(&QuitTask::Run, base::Unretained(&quit_task)));
+ }
+
+ MessageLoop::current()->Run();
+
+ // FIFO order.
+ ASSERT_EQ(12U, order.size());
+ EXPECT_EQ(order[ 0], TaskItem(PUMPS, 1, true));
+ EXPECT_EQ(order[ 1], TaskItem(ORDERERD, 3, true));
+ EXPECT_EQ(order[ 2], TaskItem(ORDERERD, 3, false));
+ EXPECT_EQ(order[ 3], TaskItem(SLEEP, 4, true));
+ EXPECT_EQ(order[ 4], TaskItem(SLEEP, 4, false));
+ EXPECT_EQ(order[ 5], TaskItem(ORDERERD, 5, true));
+ EXPECT_EQ(order[ 6], TaskItem(ORDERERD, 5, false));
+ EXPECT_EQ(order[ 7], TaskItem(PUMPS, 1, false));
+ EXPECT_EQ(order[ 8], TaskItem(ORDERERD, 2, true));
+ EXPECT_EQ(order[ 9], TaskItem(ORDERERD, 2, false));
+ EXPECT_EQ(order[10], TaskItem(QUITMESSAGELOOP, 6, true));
+ EXPECT_EQ(order[11], TaskItem(QUITMESSAGELOOP, 6, false));
+}
+
+// Tests that non nestable tasks don't run when there's code in the call stack.
void RunTest_NonNestableInNestedLoop(MessageLoop::Type message_loop_type,
bool use_delayed) {
MessageLoop loop(message_loop_type);
@@ -1328,48 +1816,96 @@ void RunTest_WaitForIO() {
// that message loops work properly in all configurations. Of course, in some
// cases, a unit test may only be for a particular type of loop.
+TEST(MessageLoopTest, PostClosure) {
+ RunTest_PostClosure(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostClosure(MessageLoop::TYPE_UI);
+ RunTest_PostClosure(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostTask) {
RunTest_PostTask(MessageLoop::TYPE_DEFAULT);
RunTest_PostTask(MessageLoop::TYPE_UI);
RunTest_PostTask(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostClosure_SEH) {
+ RunTest_PostClosure_SEH(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostClosure_SEH(MessageLoop::TYPE_UI);
+ RunTest_PostClosure_SEH(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostTask_SEH) {
RunTest_PostTask_SEH(MessageLoop::TYPE_DEFAULT);
RunTest_PostTask_SEH(MessageLoop::TYPE_UI);
RunTest_PostTask_SEH(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_Basic) {
+ RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_Basic) {
RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_UI);
RunTest_PostDelayedTask_Basic(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_InDelayOrder) {
+ RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedClosure_InDelayOrder(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_InDelayOrder) {
RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_UI);
RunTest_PostDelayedTask_InDelayOrder(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_InPostOrder) {
+ RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedClosure_InPostOrder(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_InPostOrder) {
RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_UI);
RunTest_PostDelayedTask_InPostOrder(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_InPostOrder_2) {
+ RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedClosure_InPostOrder_2(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_InPostOrder_2) {
RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_UI);
RunTest_PostDelayedTask_InPostOrder_2(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_InPostOrder_3) {
+ RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedClosure_InPostOrder_3(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_InPostOrder_3) {
RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_UI);
RunTest_PostDelayedTask_InPostOrder_3(MessageLoop::TYPE_IO);
}
+TEST(MessageLoopTest, PostDelayedClosure_SharedTimer) {
+ RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_DEFAULT);
+ RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_UI);
+ RunTest_PostDelayedClosure_SharedTimer(MessageLoop::TYPE_IO);
+}
+
TEST(MessageLoopTest, PostDelayedTask_SharedTimer) {
RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_DEFAULT);
RunTest_PostDelayedTask_SharedTimer(MessageLoop::TYPE_UI);
@@ -1377,6 +1913,10 @@ TEST(MessageLoopTest, PostDelayedTask_SharedTimer) {
}
#if defined(OS_WIN)
+TEST(MessageLoopTest, PostDelayedClosure_SharedTimer_SubPump) {
+ RunTest_PostDelayedClosure_SharedTimer_SubPump();
+}
+
TEST(MessageLoopTest, PostDelayedTask_SharedTimer_SubPump) {
RunTest_PostDelayedTask_SharedTimer_SubPump();
}
@@ -1401,6 +1941,7 @@ TEST(MessageLoopTest, FAILS_EnsureTaskDeletion_Chain) {
}
#if defined(OS_WIN)
+// TODO(ajwong): Port the Crasher tests Closure.
TEST(MessageLoopTest, Crasher) {
RunTest_Crasher(MessageLoop::TYPE_DEFAULT);
RunTest_Crasher(MessageLoop::TYPE_UI);
@@ -1418,21 +1959,31 @@ TEST(MessageLoopTest, Nesting) {
RunTest_Nesting(MessageLoop::TYPE_DEFAULT);
RunTest_Nesting(MessageLoop::TYPE_UI);
RunTest_Nesting(MessageLoop::TYPE_IO);
+ RunTest_NestingClosure(MessageLoop::TYPE_DEFAULT);
+ RunTest_NestingClosure(MessageLoop::TYPE_UI);
+ RunTest_NestingClosure(MessageLoop::TYPE_IO);
}
TEST(MessageLoopTest, RecursiveDenial1) {
RunTest_RecursiveDenial1(MessageLoop::TYPE_DEFAULT);
RunTest_RecursiveDenial1(MessageLoop::TYPE_UI);
RunTest_RecursiveDenial1(MessageLoop::TYPE_IO);
+ RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_DEFAULT);
+ RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_UI);
+ RunTest_RecursiveDenialClosure1(MessageLoop::TYPE_IO);
}
TEST(MessageLoopTest, RecursiveSupport1) {
RunTest_RecursiveSupport1(MessageLoop::TYPE_DEFAULT);
RunTest_RecursiveSupport1(MessageLoop::TYPE_UI);
RunTest_RecursiveSupport1(MessageLoop::TYPE_IO);
+ RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_DEFAULT);
+ RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_UI);
+ RunTest_RecursiveSupportClosure1(MessageLoop::TYPE_IO);
}
#if defined(OS_WIN)
+// TODO(ajwong): Port these.
// This test occasionally hangs http://crbug.com/44567
TEST(MessageLoopTest, DISABLED_RecursiveDenial2) {
RunTest_RecursiveDenial2(MessageLoop::TYPE_DEFAULT);
@@ -1450,18 +2001,27 @@ TEST(MessageLoopTest, NonNestableWithNoNesting) {
RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_DEFAULT);
RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_UI);
RunTest_NonNestableWithNoNesting(MessageLoop::TYPE_IO);
+ RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_DEFAULT);
+ RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_UI);
+ RunTest_NonNestableClosureWithNoNesting(MessageLoop::TYPE_IO);
}
TEST(MessageLoopTest, NonNestableInNestedLoop) {
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, false);
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false);
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_DEFAULT, false);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_UI, false);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_IO, false);
}
TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) {
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true);
RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_UI, true);
+ RunTest_NonNestableClosureInNestedLoop(MessageLoop::TYPE_IO, true);
}
class DummyTask : public Task {
« base/message_loop.cc ('K') | « base/message_loop.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698