OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/task.h" | 14 #include "base/task.h" |
15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 #include "base/message_pump_win.h" | 20 #include "base/message_pump_win.h" |
21 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
22 #endif | 22 #endif |
23 #if defined(OS_POSIX) | |
24 #include "base/message_pump_libevent.h" | |
25 #endif | |
26 | 23 |
27 using base::PlatformThread; | 24 using base::PlatformThread; |
28 using base::Thread; | 25 using base::Thread; |
29 using base::Time; | 26 using base::Time; |
30 using base::TimeDelta; | 27 using base::TimeDelta; |
31 using base::TimeTicks; | 28 using base::TimeTicks; |
32 | 29 |
33 // TODO(darin): Platform-specific MessageLoop tests should be grouped together | 30 // TODO(darin): Platform-specific MessageLoop tests should be grouped together |
34 // to avoid chopping this file up with so many #ifdefs. | 31 // to avoid chopping this file up with so many #ifdefs. |
35 | 32 |
(...skipping 1517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1553 loop.Run(); | 1550 loop.Run(); |
1554 EXPECT_FALSE(loop.high_resolution_timers_enabled()); | 1551 EXPECT_FALSE(loop.high_resolution_timers_enabled()); |
1555 } | 1552 } |
1556 | 1553 |
1557 #endif // defined(OS_WIN) | 1554 #endif // defined(OS_WIN) |
1558 | 1555 |
1559 #if defined(OS_POSIX) && !defined(OS_NACL) | 1556 #if defined(OS_POSIX) && !defined(OS_NACL) |
1560 | 1557 |
1561 namespace { | 1558 namespace { |
1562 | 1559 |
1563 class QuitDelegate : public base::MessagePumpLibevent::Watcher { | 1560 class QuitDelegate : public MessageLoopForIO::Watcher { |
1564 public: | 1561 public: |
1565 virtual void OnFileCanWriteWithoutBlocking(int fd) { | 1562 virtual void OnFileCanWriteWithoutBlocking(int fd) { |
1566 MessageLoop::current()->Quit(); | 1563 MessageLoop::current()->Quit(); |
1567 } | 1564 } |
1568 virtual void OnFileCanReadWithoutBlocking(int fd) { | 1565 virtual void OnFileCanReadWithoutBlocking(int fd) { |
1569 MessageLoop::current()->Quit(); | 1566 MessageLoop::current()->Quit(); |
1570 } | 1567 } |
1571 }; | 1568 }; |
1572 | 1569 |
1573 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { | 1570 TEST(MessageLoopTest, FileDescriptorWatcherOutlivesMessageLoop) { |
1574 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. | 1571 // Simulate a MessageLoop that dies before an FileDescriptorWatcher. |
1575 // This could happen when people use the Singleton pattern or atexit. | 1572 // This could happen when people use the Singleton pattern or atexit. |
1576 | 1573 |
1577 // Create a file descriptor. Doesn't need to be readable or writable, | 1574 // Create a file descriptor. Doesn't need to be readable or writable, |
1578 // as we don't need to actually get any notifications. | 1575 // as we don't need to actually get any notifications. |
1579 // pipe() is just the easiest way to do it. | 1576 // pipe() is just the easiest way to do it. |
1580 int pipefds[2]; | 1577 int pipefds[2]; |
1581 int err = pipe(pipefds); | 1578 int err = pipe(pipefds); |
1582 ASSERT_EQ(0, err); | 1579 ASSERT_EQ(0, err); |
1583 int fd = pipefds[1]; | 1580 int fd = pipefds[1]; |
1584 { | 1581 { |
1585 // Arrange for controller to live longer than message loop. | 1582 // Arrange for controller to live longer than message loop. |
1586 base::MessagePumpLibevent::FileDescriptorWatcher controller; | 1583 MessageLoopForIO::FileDescriptorWatcher controller; |
1587 { | 1584 { |
1588 MessageLoopForIO message_loop; | 1585 MessageLoopForIO message_loop; |
1589 | 1586 |
1590 QuitDelegate delegate; | 1587 QuitDelegate delegate; |
1591 message_loop.WatchFileDescriptor(fd, | 1588 message_loop.WatchFileDescriptor(fd, |
1592 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); | 1589 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
1593 // and don't run the message loop, just destroy it. | 1590 // and don't run the message loop, just destroy it. |
1594 } | 1591 } |
1595 } | 1592 } |
1596 if (HANDLE_EINTR(close(pipefds[0])) < 0) | 1593 if (HANDLE_EINTR(close(pipefds[0])) < 0) |
1597 PLOG(ERROR) << "close"; | 1594 PLOG(ERROR) << "close"; |
1598 if (HANDLE_EINTR(close(pipefds[1])) < 0) | 1595 if (HANDLE_EINTR(close(pipefds[1])) < 0) |
1599 PLOG(ERROR) << "close"; | 1596 PLOG(ERROR) << "close"; |
1600 } | 1597 } |
1601 | 1598 |
1602 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { | 1599 TEST(MessageLoopTest, FileDescriptorWatcherDoubleStop) { |
1603 // Verify that it's ok to call StopWatchingFileDescriptor(). | 1600 // Verify that it's ok to call StopWatchingFileDescriptor(). |
1604 // (Errors only showed up in valgrind.) | 1601 // (Errors only showed up in valgrind.) |
1605 int pipefds[2]; | 1602 int pipefds[2]; |
1606 int err = pipe(pipefds); | 1603 int err = pipe(pipefds); |
1607 ASSERT_EQ(0, err); | 1604 ASSERT_EQ(0, err); |
1608 int fd = pipefds[1]; | 1605 int fd = pipefds[1]; |
1609 { | 1606 { |
1610 // Arrange for message loop to live longer than controller. | 1607 // Arrange for message loop to live longer than controller. |
1611 MessageLoopForIO message_loop; | 1608 MessageLoopForIO message_loop; |
1612 { | 1609 { |
1613 base::MessagePumpLibevent::FileDescriptorWatcher controller; | 1610 MessageLoopForIO::FileDescriptorWatcher controller; |
1614 | 1611 |
1615 QuitDelegate delegate; | 1612 QuitDelegate delegate; |
1616 message_loop.WatchFileDescriptor(fd, | 1613 message_loop.WatchFileDescriptor(fd, |
1617 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); | 1614 true, MessageLoopForIO::WATCH_WRITE, &controller, &delegate); |
1618 controller.StopWatchingFileDescriptor(); | 1615 controller.StopWatchingFileDescriptor(); |
1619 } | 1616 } |
1620 } | 1617 } |
1621 if (HANDLE_EINTR(close(pipefds[0])) < 0) | 1618 if (HANDLE_EINTR(close(pipefds[0])) < 0) |
1622 PLOG(ERROR) << "close"; | 1619 PLOG(ERROR) << "close"; |
1623 if (HANDLE_EINTR(close(pipefds[1])) < 0) | 1620 if (HANDLE_EINTR(close(pipefds[1])) < 0) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1691 base::Bind(&DestructionObserverProbe::Run, | 1688 base::Bind(&DestructionObserverProbe::Run, |
1692 new DestructionObserverProbe(&task_destroyed, | 1689 new DestructionObserverProbe(&task_destroyed, |
1693 &destruction_observer_called)), | 1690 &destruction_observer_called)), |
1694 kDelayMS); | 1691 kDelayMS); |
1695 delete loop; | 1692 delete loop; |
1696 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); | 1693 EXPECT_TRUE(observer.task_destroyed_before_message_loop()); |
1697 // The task should have been destroyed when we deleted the loop. | 1694 // The task should have been destroyed when we deleted the loop. |
1698 EXPECT_TRUE(task_destroyed); | 1695 EXPECT_TRUE(task_destroyed); |
1699 EXPECT_TRUE(destruction_observer_called); | 1696 EXPECT_TRUE(destruction_observer_called); |
1700 } | 1697 } |
OLD | NEW |