From 2a1272586d0ab6719820ea25ac596683b33d0676 Mon Sep 17 00:00:00 2001 From: "darin@google.com" Date: Tue, 5 Aug 2008 23:16:41 +0000 Subject: ObjectWatcher needs to know when the current thread's MessageLoop is being destroyed. This might also be generically useful, so I added a new API on ML to observe when the ML is being destroyed. The notification is sent to observers just prior to ML::current() being modified to return NULL. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@407 0039d316-1c4b-4281-b951-d872f2087c98 --- base/object_watcher_unittest.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'base/object_watcher_unittest.cc') diff --git a/base/object_watcher_unittest.cc b/base/object_watcher_unittest.cc index 51ed968..6c3fcce 100644 --- a/base/object_watcher_unittest.cc +++ b/base/object_watcher_unittest.cc @@ -27,6 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include + #include "base/message_loop.h" #include "base/object_watcher.h" #include "testing/gtest/include/gtest/gtest.h" @@ -113,3 +115,34 @@ TEST(ObjectWatcherTest, CancelAfterSet) { CloseHandle(event); } + +// Used so we can simulate a MessageLoop that dies before an ObjectWatcher. +// This ordinarily doesn't happen when people use the Thread class, but it can +// happen when people use the Singleton pattern or atexit. +static unsigned __stdcall ThreadFunc(void* param) { + HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL); // not signaled + { + base::ObjectWatcher watcher; + { + MessageLoop message_loop; + + QuitDelegate delegate; + watcher.StartWatching(event, &delegate); + } + } + CloseHandle(event); + return 0; +} + +TEST(ObjectWatcherTest, OutlivesMessageLoop) { + unsigned int thread_id; + HANDLE thread = reinterpret_cast( + _beginthreadex(NULL, + 0, + ThreadFunc, + NULL, + 0, + &thread_id)); + WaitForSingleObject(thread, INFINITE); + CloseHandle(thread); +} -- cgit v1.1