summaryrefslogtreecommitdiffstats
path: root/base/object_watcher_unittest.cc
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-04 17:46:47 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-04 17:46:47 +0000
commitb49054718ac2c49f629427c41a4ff2a6ba4bdb09 (patch)
tree0611d3d1d64574356dd3705ee66f7309a251bec3 /base/object_watcher_unittest.cc
parent1c15248a5f6ffd48c99bca9d2fbd6d4b375e991c (diff)
downloadchromium_src-b49054718ac2c49f629427c41a4ff2a6ba4bdb09.zip
chromium_src-b49054718ac2c49f629427c41a4ff2a6ba4bdb09.tar.gz
chromium_src-b49054718ac2c49f629427c41a4ff2a6ba4bdb09.tar.bz2
Revise the ObjectWatcher API to be one-to-one with the object being watched. This greatly simplifies the implementation and API.
Now consumers can use ObjectWatcher as a "smart pointer" class, which automatically cleans-up after itself when it goes out of scope. I also switched away from the Task based API to one that is more compatible with MessageLoop::WatchObject. That allows me to make minimal changes to existing code, and it also means that consumers do not have to allocate Task objects to use this API. In this CL, I included changes to make a couple consumers use ObjectWatcher instead of ML::WatchObject. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@322 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/object_watcher_unittest.cc')
-rw-r--r--base/object_watcher_unittest.cc112
1 files changed, 30 insertions, 82 deletions
diff --git a/base/object_watcher_unittest.cc b/base/object_watcher_unittest.cc
index 16e6ae5..51ed968 100644
--- a/base/object_watcher_unittest.cc
+++ b/base/object_watcher_unittest.cc
@@ -34,13 +34,19 @@
namespace {
typedef testing::Test ObjectWatcherTest;
-class DecrementCountTask : public Task {
+class QuitDelegate : public base::ObjectWatcher::Delegate {
public:
- DecrementCountTask(int* counter) : counter_(counter) {
+ virtual void OnObjectSignaled(HANDLE object) {
+ MessageLoop::current()->Quit();
}
- virtual void Run() {
- if (--(*counter_) == 0)
- MessageLoop::current()->Quit();
+};
+
+class DecrementCountDelegate : public base::ObjectWatcher::Delegate {
+ public:
+ DecrementCountDelegate(int* counter) : counter_(counter) {
+ }
+ virtual void OnObjectSignaled(HANDLE object) {
+ --(*counter_);
}
private:
int* counter_;
@@ -54,7 +60,8 @@ TEST(ObjectWatcherTest, BasicSignal) {
// A manual-reset event that is not yet signaled.
HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
- bool ok = watcher.AddWatch(FROM_HERE, event, new MessageLoop::QuitTask());
+ QuitDelegate delegate;
+ bool ok = watcher.StartWatching(event, &delegate);
EXPECT_TRUE(ok);
SetEvent(event);
@@ -70,98 +77,39 @@ TEST(ObjectWatcherTest, BasicCancel) {
// A manual-reset event that is not yet signaled.
HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
- bool ok = watcher.AddWatch(FROM_HERE, event, new MessageLoop::QuitTask());
+ QuitDelegate delegate;
+ bool ok = watcher.StartWatching(event, &delegate);
EXPECT_TRUE(ok);
- watcher.CancelWatch(event);
+ watcher.StopWatching();
CloseHandle(event);
}
-TEST(ObjectWatcherTest, ManySignal) {
- base::ObjectWatcher watcher;
-
- const int kNumObjects = 2000;
-
- int counter = kNumObjects;
- HANDLE events[kNumObjects];
-
- for (int i = 0; i < kNumObjects; ++i) {
- // A manual-reset event that is not yet signaled.
- events[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
-
- bool ok = watcher.AddWatch(
- FROM_HERE, events[i], new DecrementCountTask(&counter));
- EXPECT_TRUE(ok);
- }
-
- for (int i = 0; i < kNumObjects; ++i)
- SetEvent(events[i]);
-
- MessageLoop::current()->Run();
-
- for (int i = 0; i < kNumObjects; ++i)
- CloseHandle(events[i]);
-}
-
-TEST(ObjectWatcherTest, ManyCancel) {
- base::ObjectWatcher watcher;
-
- const int kNumObjects = 2000;
-
- int counter = kNumObjects;
- HANDLE events[kNumObjects];
-
- for (int i = 0; i < kNumObjects; ++i) {
- // A manual-reset event that is not yet signaled.
- events[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
-
- bool ok = watcher.AddWatch(
- FROM_HERE, events[i], new DecrementCountTask(&counter));
- EXPECT_TRUE(ok);
- }
-
- for (int i = 0; i < kNumObjects; ++i)
- watcher.CancelWatch(events[i]);
-
- for (int i = 0; i < kNumObjects; ++i)
- CloseHandle(events[i]);
-}
TEST(ObjectWatcherTest, CancelAfterSet) {
base::ObjectWatcher watcher;
- const int kNumObjects = 50;
+ int counter = 1;
+ DecrementCountDelegate delegate(&counter);
- int counter = kNumObjects;
- HANDLE events[kNumObjects];
-
- for (int i = 0; i < kNumObjects; ++i) {
// A manual-reset event that is not yet signaled.
- events[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
+ HANDLE event = CreateEvent(NULL, TRUE, FALSE, NULL);
- bool ok = watcher.AddWatch(
- FROM_HERE, events[i], new DecrementCountTask(&counter));
- EXPECT_TRUE(ok);
- }
+ bool ok = watcher.StartWatching(event, &delegate);
+ EXPECT_TRUE(ok);
- for (int i = 0; i < kNumObjects; ++i) {
- SetEvent(events[i]);
+ SetEvent(event);
- // Let the background thread do its business
- SleepEx(10, TRUE);
+ // Let the background thread do its business
+ Sleep(30);
- // Occasionally pump some tasks. Sometimes we cancel a watch after its
- // task has run and other times we do nothing.
- if (i % 3 == 0)
- MessageLoop::current()->RunAllPending();
-
- if (watcher.CancelWatch(events[i]))
- --counter;
- }
+ watcher.StopWatching();
- EXPECT_EQ(0, counter);
+ MessageLoop::current()->RunAllPending();
- for (int i = 0; i < kNumObjects; ++i)
- CloseHandle(events[i]);
+ // Our delegate should not have fired.
+ EXPECT_EQ(1, counter);
+
+ CloseHandle(event);
}