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

Side by Side Diff: base/waitable_event_watcher_unittest.cc

Issue 16554: WaitableEvent (Closed)
Patch Set: Addresssing darin's comments (round 2) Created 11 years, 11 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/waitable_event_watcher_posix.cc ('k') | base/waitable_event_watcher_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/message_loop.h"
6 #include "base/platform_thread.h"
7 #include "base/waitable_event.h"
8 #include "base/waitable_event_watcher.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 using base::WaitableEvent;
12 using base::WaitableEventWatcher;
13
14 namespace {
15
16 class QuitDelegate : public WaitableEventWatcher::Delegate {
17 public:
18 virtual void OnWaitableEventSignaled(WaitableEvent* event) {
19 MessageLoop::current()->Quit();
20 }
21 };
22
23 class DecrementCountDelegate : public WaitableEventWatcher::Delegate {
24 public:
25 DecrementCountDelegate(int* counter) : counter_(counter) {
26 }
27 virtual void OnWaitableEventSignaled(WaitableEvent* object) {
28 --(*counter_);
29 }
30 private:
31 int* counter_;
32 };
33
34 } // namespace
35
36 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) {
37 MessageLoop message_loop(message_loop_type);
38
39 // A manual-reset event that is not yet signaled.
40 WaitableEvent event(true, false);
41
42 WaitableEventWatcher watcher;
43 EXPECT_EQ(NULL, watcher.GetWatchedObject());
44
45 QuitDelegate delegate;
46 watcher.StartWatching(&event, &delegate);
47 EXPECT_EQ(&event, watcher.GetWatchedObject());
48
49 event.Signal();
50
51 MessageLoop::current()->Run();
52
53 EXPECT_EQ(NULL, watcher.GetWatchedObject());
54 }
55
56 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) {
57 MessageLoop message_loop(message_loop_type);
58
59 // A manual-reset event that is not yet signaled.
60 WaitableEvent event(true, false);
61
62 WaitableEventWatcher watcher;
63
64 QuitDelegate delegate;
65 watcher.StartWatching(&event, &delegate);
66
67 watcher.StopWatching();
68 }
69
70 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) {
71 MessageLoop message_loop(message_loop_type);
72
73 // A manual-reset event that is not yet signaled.
74 WaitableEvent event(true, false);
75
76 WaitableEventWatcher watcher;
77
78 int counter = 1;
79 DecrementCountDelegate delegate(&counter);
80
81 watcher.StartWatching(&event, &delegate);
82
83 event.Signal();
84
85 // Let the background thread do its business
86 PlatformThread::Sleep(30);
87
88 watcher.StopWatching();
89
90 MessageLoop::current()->RunAllPending();
91
92 // Our delegate should not have fired.
93 EXPECT_EQ(1, counter);
94 }
95
96 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
97 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This
98 // ordinarily doesn't happen when people use the Thread class, but it can
99 // happen when people use the Singleton pattern or atexit.
100 WaitableEvent event(true, false);
101 {
102 WaitableEventWatcher watcher;
103 {
104 MessageLoop message_loop(message_loop_type);
105
106 QuitDelegate delegate;
107 watcher.StartWatching(&event, &delegate);
108 }
109 }
110 }
111
112 //-----------------------------------------------------------------------------
113
114 TEST(ObjectWatcherTest, BasicSignal) {
115 RunTest_BasicSignal(MessageLoop::TYPE_DEFAULT);
116 RunTest_BasicSignal(MessageLoop::TYPE_IO);
117 RunTest_BasicSignal(MessageLoop::TYPE_UI);
118 }
119
120 TEST(ObjectWatcherTest, BasicCancel) {
121 RunTest_BasicCancel(MessageLoop::TYPE_DEFAULT);
122 RunTest_BasicCancel(MessageLoop::TYPE_IO);
123 RunTest_BasicCancel(MessageLoop::TYPE_UI);
124 }
125
126 TEST(ObjectWatcherTest, CancelAfterSet) {
127 RunTest_CancelAfterSet(MessageLoop::TYPE_DEFAULT);
128 RunTest_CancelAfterSet(MessageLoop::TYPE_IO);
129 RunTest_CancelAfterSet(MessageLoop::TYPE_UI);
130 }
131
132 TEST(ObjectWatcherTest, OutlivesMessageLoop) {
133 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_DEFAULT);
134 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_IO);
135 RunTest_OutlivesMessageLoop(MessageLoop::TYPE_UI);
136 }
OLDNEW
« no previous file with comments | « base/waitable_event_watcher_posix.cc ('k') | base/waitable_event_watcher_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698