license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 5 | #include "base/message_loop.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | #include "base/observer_list.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 7 | #include "base/observer_list_threadsafe.h" |
| 8 | #include "base/platform_thread.h" |
| 9 | #include "base/ref_counted.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 12 | using base::Time; |
| 13 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | class ObserverListTest : public testing::Test { |
| 17 | }; |
| 18 | |
| 19 | class Foo { |
| 20 | public: |
| 21 | virtual void Observe(int x) = 0; |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 22 | virtual ~Foo() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | class Adder : public Foo { |
| 26 | public: |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 27 | explicit Adder(int scaler) : total(0), scaler_(scaler) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | virtual void Observe(int x) { |
| 29 | total += x * scaler_; |
| 30 | } |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 31 | virtual ~Adder() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 32 | int total; |
| 33 | private: |
| 34 | int scaler_; |
| 35 | }; |
| 36 | |
| 37 | class Disrupter : public Foo { |
| 38 | public: |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 39 | Disrupter(ObserverList<Foo>* list, Foo* doomed) |
| 40 | : list_(list), doomed_(doomed) { } |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 41 | virtual ~Disrupter() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | virtual void Observe(int x) { |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 43 | list_->RemoveObserver(doomed_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | } |
| 45 | private: |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 46 | ObserverList<Foo>* list_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | Foo* doomed_; |
| 48 | }; |
| 49 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 50 | class ThreadSafeDisrupter : public Foo { |
| 51 | public: |
| 52 | ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) |
| 53 | : list_(list), doomed_(doomed) { } |
| 54 | virtual ~ThreadSafeDisrupter() { } |
| 55 | virtual void Observe(int x) { |
| 56 | list_->RemoveObserver(doomed_); |
| 57 | } |
| 58 | private: |
| 59 | ObserverListThreadSafe<Foo>* list_; |
| 60 | Foo* doomed_; |
| 61 | }; |
| 62 | |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 63 | class AddInObserve : public Foo { |
| 64 | public: |
| 65 | AddInObserve(ObserverList<Foo>* observer_list) |
| 66 | : added(false), |
| 67 | observer_list(observer_list), |
| 68 | adder(1) { |
| 69 | } |
| 70 | virtual void Observe(int x) { |
| 71 | if (!added) { |
| 72 | added = true; |
| 73 | observer_list->AddObserver(&adder); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool added; |
| 78 | ObserverList<Foo>* observer_list; |
| 79 | Adder adder; |
| 80 | }; |
| 81 | |
| 82 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 83 | class ObserverListThreadSafeTest : public testing::Test { |
| 84 | }; |
| 85 | |
| 86 | static const int kThreadRunTime = 10000; // ms to run the multi-threaded test. |
| 87 | |
| 88 | // A thread for use in the ThreadSafeObserver test |
| 89 | // which will add and remove itself from the notification |
| 90 | // list repeatedly. |
| 91 | class AddRemoveThread : public PlatformThread::Delegate, |
| 92 | public Foo { |
| 93 | public: |
| 94 | AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify) |
| 95 | : list_(list), |
| 96 | in_list_(false), |
| 97 | start_(Time::Now()), |
| 98 | count_observes_(0), |
| 99 | count_addtask_(0), |
| 100 | do_notifies_(notify) { |
| 101 | factory_ = new ScopedRunnableMethodFactory<AddRemoveThread>(this); |
| 102 | } |
| 103 | |
| 104 | ~AddRemoveThread() { |
| 105 | delete factory_; |
| 106 | } |
| 107 | |
| 108 | void ThreadMain() { |
| 109 | loop_ = new MessageLoop(); // Fire up a message loop. |
| 110 | loop_->PostTask(FROM_HERE, |
| 111 | factory_->NewRunnableMethod(&AddRemoveThread::AddTask)); |
| 112 | loop_->Run(); |
| 113 | //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << count_observes_ << ", " << count_addtask_; |
| 114 | delete loop_; |
| 115 | loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); |
| 116 | } |
| 117 | |
| 118 | // This task just keeps posting to itself in an attempt |
| 119 | // to race with the notifier. |
| 120 | void AddTask() { |
| 121 | count_addtask_++; |
| 122 | |
| 123 | if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) { |
| 124 | LOG(INFO) << "DONE!"; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (!in_list_) { |
| 129 | list_->AddObserver(this); |
| 130 | in_list_ = true; |
| 131 | } |
| 132 | |
| 133 | if (do_notifies_) { |
| 134 | list_->Notify(&Foo::Observe, 10); |
| 135 | } |
| 136 | |
| 137 | loop_->PostDelayedTask(FROM_HERE, |
| 138 | factory_->NewRunnableMethod(&AddRemoveThread::AddTask), 0); |
| 139 | } |
| 140 | |
| 141 | void Quit() { |
| 142 | loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 143 | } |
| 144 | |
| 145 | virtual void Observe(int x) { |
| 146 | count_observes_++; |
| 147 | |
| 148 | // If we're getting called after we removed ourselves from |
| 149 | // the list, that is very bad! |
| 150 | DCHECK(in_list_); |
| 151 | |
| 152 | // This callback should fire on the appropriate thread |
| 153 | EXPECT_EQ(loop_, MessageLoop::current()); |
| 154 | |
| 155 | list_->RemoveObserver(this); |
| 156 | in_list_ = false; |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | ObserverListThreadSafe<Foo>* list_; |
| 161 | MessageLoop* loop_; |
| 162 | bool in_list_; // Are we currently registered for notifications. |
| 163 | // in_list_ is only used on |this| thread. |
| 164 | Time start_; // The time we started the test. |
| 165 | |
| 166 | int count_observes_; // Number of times we observed. |
| 167 | int count_addtask_; // Number of times thread AddTask was called |
| 168 | bool do_notifies_; // Whether these threads should do notifications. |
| 169 | |
| 170 | ScopedRunnableMethodFactory<AddRemoveThread>* factory_; |
| 171 | }; |
| 172 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 173 | } // namespace |
| 174 | |
| 175 | TEST(ObserverListTest, BasicTest) { |
| 176 | ObserverList<Foo> observer_list; |
| 177 | Adder a(1), b(-1), c(1), d(-1); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 178 | Disrupter evil(&observer_list, &c); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | |
| 180 | observer_list.AddObserver(&a); |
| 181 | observer_list.AddObserver(&b); |
| 182 | |
| 183 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 184 | |
| 185 | observer_list.AddObserver(&evil); |
| 186 | observer_list.AddObserver(&c); |
| 187 | observer_list.AddObserver(&d); |
| 188 | |
| 189 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 190 | |
| 191 | EXPECT_EQ(a.total, 20); |
| 192 | EXPECT_EQ(b.total, -20); |
| 193 | EXPECT_EQ(c.total, 0); |
| 194 | EXPECT_EQ(d.total, -10); |
| 195 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 196 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 197 | TEST(ObserverListThreadSafeTest, BasicTest) { |
| 198 | MessageLoop loop; |
| 199 | |
| 200 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 201 | new ObserverListThreadSafe<Foo>); |
| 202 | Adder a(1); |
| 203 | Adder b(-1); |
| 204 | Adder c(1); |
| 205 | Adder d(-1); |
| 206 | ThreadSafeDisrupter evil(observer_list.get(), &c); |
| 207 | |
| 208 | observer_list->AddObserver(&a); |
| 209 | observer_list->AddObserver(&b); |
| 210 | |
| 211 | observer_list->Notify(&Foo::Observe, 10); |
| 212 | loop.RunAllPending(); |
| 213 | |
| 214 | observer_list->AddObserver(&evil); |
| 215 | observer_list->AddObserver(&c); |
| 216 | observer_list->AddObserver(&d); |
| 217 | |
| 218 | observer_list->Notify(&Foo::Observe, 10); |
| 219 | loop.RunAllPending(); |
| 220 | |
| 221 | EXPECT_EQ(a.total, 20); |
| 222 | EXPECT_EQ(b.total, -20); |
| 223 | EXPECT_EQ(c.total, 0); |
| 224 | EXPECT_EQ(d.total, -10); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // A test driver for a multi-threaded notification loop. Runs a number |
| 229 | // of observer threads, each of which constantly adds/removes itself |
| 230 | // from the observer list. Optionally, if cross_thread_notifies is set |
| 231 | // to true, the observer threads will also trigger notifications to |
| 232 | // all observers. |
| 233 | static void ThreadSafeObserverHarness(int num_threads, |
| 234 | bool cross_thread_notifies) { |
| 235 | MessageLoop loop; |
| 236 | |
| 237 | const int kMaxThreads = 15; |
| 238 | num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads; |
| 239 | |
| 240 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 241 | new ObserverListThreadSafe<Foo>); |
| 242 | Adder a(1); |
| 243 | Adder b(-1); |
| 244 | Adder c(1); |
| 245 | Adder d(-1); |
| 246 | |
| 247 | observer_list->AddObserver(&a); |
| 248 | observer_list->AddObserver(&b); |
| 249 | |
| 250 | AddRemoveThread* threaded_observer[kMaxThreads]; |
| 251 | PlatformThreadHandle threads[kMaxThreads]; |
| 252 | for (int index = 0; index < num_threads; index++) { |
| 253 | threaded_observer[index] = new AddRemoveThread(observer_list.get(), false); |
| 254 | EXPECT_TRUE(PlatformThread::Create(0, |
| 255 | threaded_observer[index], &threads[index])); |
| 256 | } |
| 257 | |
| 258 | Time start = Time::Now(); |
| 259 | while (true) { |
| 260 | if ((Time::Now() - start).InMilliseconds() > kThreadRunTime) |
| 261 | break; |
| 262 | |
| 263 | observer_list->Notify(&Foo::Observe, 10); |
| 264 | |
| 265 | loop.RunAllPending(); |
| 266 | } |
| 267 | |
| 268 | for (int index = 0; index < num_threads; index++) { |
| 269 | threaded_observer[index]->Quit(); |
| 270 | PlatformThread::Join(threads[index]); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | TEST(ObserverListThreadSafeTest, CrossThreadObserver) { |
| 275 | // Use 7 observer threads. Notifications only come from |
| 276 | // the main thread. |
| 277 | ThreadSafeObserverHarness(7, false); |
| 278 | } |
| 279 | |
| 280 | TEST(ObserverListThreadSafeTest, CrossThreadNotifications) { |
| 281 | // Use 3 observer threads. Notifications will fire from |
| 282 | // the main thread and all 3 observer threads. |
| 283 | ThreadSafeObserverHarness(3, true); |
| 284 | } |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 285 | |
| 286 | TEST(ObserverListTest, Existing) { |
| 287 | ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 288 | Adder a(1); |
| 289 | AddInObserve b(&observer_list); |
| 290 | |
| 291 | observer_list.AddObserver(&a); |
| 292 | observer_list.AddObserver(&b); |
| 293 | |
| 294 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 295 | |
| 296 | EXPECT_TRUE(b.added); |
| 297 | // B's adder should not have been notified because it was added during |
| 298 | // notificaiton. |
| 299 | EXPECT_EQ(0, b.adder.total); |
| 300 | |
| 301 | // Notify again to make sure b's adder is notified. |
| 302 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 303 | EXPECT_EQ(1, b.adder.total); |
| 304 | } |