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

Side by Side Diff: base/message_loop_unittest.cc

Issue 2098020: Jankometer: Generalize the code more. Add better support for monitoring IO thread. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Address darin's comments. Created 10 years, 6 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 unified diff | Download patch
« no previous file with comments | « base/message_loop.cc ('k') | base/message_pump_libevent.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/eintr_wrapper.h"
5 #include "base/logging.h" 6 #include "base/logging.h"
6 #include "base/message_loop.h" 7 #include "base/message_loop.h"
7 #include "base/platform_thread.h" 8 #include "base/platform_thread.h"
8 #include "base/ref_counted.h" 9 #include "base/ref_counted.h"
10 #include "base/task.h"
9 #include "base/thread.h" 11 #include "base/thread.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 13
12 #if defined(OS_WIN) 14 #if defined(OS_WIN)
13 #include "base/message_pump_win.h" 15 #include "base/message_pump_win.h"
14 #include "base/scoped_handle.h" 16 #include "base/scoped_handle.h"
15 #endif 17 #endif
16 #if defined(OS_POSIX) 18 #if defined(OS_POSIX)
17 #include "base/message_pump_libevent.h" 19 #include "base/message_pump_libevent.h"
18 #endif 20 #endif
(...skipping 1429 matching lines...) Expand 10 before | Expand all | Expand 10 after
1448 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false); 1450 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, false);
1449 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false); 1451 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, false);
1450 } 1452 }
1451 1453
1452 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) { 1454 TEST(MessageLoopTest, NonNestableDelayedInNestedLoop) {
1453 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true); 1455 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_DEFAULT, true);
1454 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true); 1456 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_UI, true);
1455 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true); 1457 RunTest_NonNestableInNestedLoop(MessageLoop::TYPE_IO, true);
1456 } 1458 }
1457 1459
1460 class DummyTask : public Task {
1461 public:
1462 DummyTask(int num_tasks) : num_tasks_(num_tasks) {}
1463
1464 virtual void Run() {
1465 if (num_tasks_ > 1) {
1466 MessageLoop::current()->PostTask(
1467 FROM_HERE,
1468 new DummyTask(num_tasks_ - 1));
1469 } else {
1470 MessageLoop::current()->Quit();
1471 }
1472 }
1473
1474 private:
1475 const int num_tasks_;
1476 };
1477
1478 class DummyTaskObserver : public MessageLoop::TaskObserver {
1479 public:
1480 DummyTaskObserver(int num_tasks)
1481 : num_tasks_started_(0),
1482 num_tasks_processed_(0),
1483 num_tasks_(num_tasks) {}
1484
1485 virtual ~DummyTaskObserver() {}
1486
1487 virtual void WillProcessTask(base::TimeTicks /* birth_time */) {
1488 num_tasks_started_++;
1489 EXPECT_LE(num_tasks_started_, num_tasks_);
1490 EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
1491 }
1492
1493 virtual void DidProcessTask() {
1494 num_tasks_processed_++;
1495 EXPECT_LE(num_tasks_started_, num_tasks_);
1496 EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
1497 }
1498
1499 int num_tasks_started() const { return num_tasks_started_; }
1500 int num_tasks_processed() const { return num_tasks_processed_; }
1501
1502 private:
1503 int num_tasks_started_;
1504 int num_tasks_processed_;
1505 const int num_tasks_;
1506
1507 DISALLOW_COPY_AND_ASSIGN(DummyTaskObserver);
1508 };
1509
1510 TEST(MessageLoopTest, TaskObserver) {
1511 const int kNumTasks = 6;
1512 DummyTaskObserver observer(kNumTasks);
1513
1514 MessageLoop loop;
1515 loop.AddTaskObserver(&observer);
1516 loop.PostTask(FROM_HERE, new DummyTask(kNumTasks));
1517 loop.Run();
1518 loop.RemoveTaskObserver(&observer);
1519
1520 EXPECT_EQ(kNumTasks, observer.num_tasks_started());
1521 EXPECT_EQ(kNumTasks, observer.num_tasks_processed());
1522 }
1523
1458 #if defined(OS_WIN) 1524 #if defined(OS_WIN)
1459 TEST(MessageLoopTest, Dispatcher) { 1525 TEST(MessageLoopTest, Dispatcher) {
1460 // This test requires a UI loop 1526 // This test requires a UI loop
1461 RunTest_Dispatcher(MessageLoop::TYPE_UI); 1527 RunTest_Dispatcher(MessageLoop::TYPE_UI);
1462 } 1528 }
1463 1529
1464 TEST(MessageLoopTest, DispatcherWithMessageHook) { 1530 TEST(MessageLoopTest, DispatcherWithMessageHook) {
1465 // This test requires a UI loop 1531 // This test requires a UI loop
1466 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI); 1532 RunTest_DispatcherWithMessageHook(MessageLoop::TYPE_UI);
1467 } 1533 }
1468 1534
1469 TEST(MessageLoopTest, IOHandler) { 1535 TEST(MessageLoopTest, IOHandler) {
1470 RunTest_IOHandler(); 1536 RunTest_IOHandler();
1471 } 1537 }
1472 1538
1473 TEST(MessageLoopTest, WaitForIO) { 1539 TEST(MessageLoopTest, WaitForIO) {
1474 RunTest_WaitForIO(); 1540 RunTest_WaitForIO();
1475 } 1541 }
1476 #endif // defined(OS_WIN) 1542 #endif // defined(OS_WIN)
1477 1543
1478 #if defined(OS_POSIX) 1544 #if defined(OS_POSIX)
1479 1545
1480 namespace { 1546 namespace {
1481 1547
1482 class QuitDelegate : public 1548 class QuitDelegate : public base::MessagePumpLibevent::Watcher {
1483 base::MessagePumpLibevent::Watcher {
1484 public: 1549 public:
1485 virtual void OnFileCanWriteWithoutBlocking(int fd) { 1550 virtual void OnFileCanWriteWithoutBlocking(int fd) {
1486 MessageLoop::current()->Quit(); 1551 MessageLoop::current()->Quit();
1487 } 1552 }
1488 virtual void OnFileCanReadWithoutBlocking(int fd) { 1553 virtual void OnFileCanReadWithoutBlocking(int fd) {
1489 MessageLoop::current()->Quit(); 1554 MessageLoop::current()->Quit();
1490 } 1555 }
1491 }; 1556 };
1492 1557
1493 } // namespace
1494
1495 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { 1558 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) {
1496 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. 1559 // Simulate a MessageLoop that dies before an FileDescriptorWatcher.
1497 // This could happen when people use the Singleton pattern or atexit. 1560 // This could happen when people use the Singleton pattern or atexit.
1498 // This is disabled for now because it fails (valgrind shows 1561 // This is disabled for now because it fails (valgrind shows
1499 // invalid reads), and it's not clear any code relies on this... 1562 // invalid reads), and it's not clear any code relies on this...
1500 1563
1501 // Create a file descriptor. Doesn't need to be readable or writable, 1564 // Create a file descriptor. Doesn't need to be readable or writable,
1502 // as we don't need to actually get any notifications. 1565 // as we don't need to actually get any notifications.
1503 // pipe() is just the easiest way to do it. 1566 // pipe() is just the easiest way to do it.
1504 int pipefds[2]; 1567 int pipefds[2];
1505 int err = pipe(pipefds); 1568 int err = pipe(pipefds);
1506 ASSERT_TRUE(err == 0); 1569 ASSERT_TRUE(err == 0);
1507 int fd = pipefds[1]; 1570 int fd = pipefds[1];
1508 { 1571 {
1509 // Arrange for controller to live longer than message loop. 1572 // Arrange for controller to live longer than message loop.
1510 base::MessagePumpLibevent::FileDescriptorWatcher controller; 1573 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1511 { 1574 {
1512 MessageLoopForIO message_loop; 1575 MessageLoopForIO message_loop;
1513 1576
1514 QuitDelegate delegate; 1577 QuitDelegate delegate;
1515 message_loop.WatchFileDescriptor(fd, 1578 message_loop.WatchFileDescriptor(fd,
1516 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); 1579 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1517 // and don't run the message loop, just destroy it. 1580 // and don't run the message loop, just destroy it.
1518 } 1581 }
1519 } 1582 }
1520 close(pipefds[0]); 1583 HANDLE_EINTR(close(pipefds[0]));
1521 close(pipefds[1]); 1584 HANDLE_EINTR(close(pipefds[1]));
1522 } 1585 }
1523 1586
1524 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { 1587 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) {
1525 // Verify that it's ok to call StopWatchingFileDescriptor(). 1588 // Verify that it's ok to call StopWatchingFileDescriptor().
1526 // (Errors only showed up in valgrind.) 1589 // (Errors only showed up in valgrind.)
1527 int pipefds[2]; 1590 int pipefds[2];
1528 int err = pipe(pipefds); 1591 int err = pipe(pipefds);
1529 ASSERT_TRUE(err == 0); 1592 ASSERT_TRUE(err == 0);
1530 int fd = pipefds[1]; 1593 int fd = pipefds[1];
1531 { 1594 {
1532 // Arrange for message loop to live longer than controller. 1595 // Arrange for message loop to live longer than controller.
1533 MessageLoopForIO message_loop; 1596 MessageLoopForIO message_loop;
1534 { 1597 {
1535 base::MessagePumpLibevent::FileDescriptorWatcher controller; 1598 base::MessagePumpLibevent::FileDescriptorWatcher controller;
1536 1599
1537 QuitDelegate delegate; 1600 QuitDelegate delegate;
1538 message_loop.WatchFileDescriptor(fd, 1601 message_loop.WatchFileDescriptor(fd,
1539 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); 1602 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate);
1540 controller.StopWatchingFileDescriptor(); 1603 controller.StopWatchingFileDescriptor();
1541 } 1604 }
1542 } 1605 }
1543 close(pipefds[0]); 1606 HANDLE_EINTR(close(pipefds[0]));
1544 close(pipefds[1]); 1607 HANDLE_EINTR(close(pipefds[1]));
1545 } 1608 }
1546 1609
1610 } // namespace
1611
1547 #endif // defined(OS_POSIX) 1612 #endif // defined(OS_POSIX)
OLDNEW
« no previous file with comments | « base/message_loop.cc ('k') | base/message_pump_libevent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698