[email protected] | 3fe674d | 2012-04-05 17:52:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 | |
| 5 | #include "base/observer_list.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 6 | #include "base/observer_list_threadsafe.h" |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
| 11 | #include "base/memory/weak_ptr.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 13 | #include "base/run_loop.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 14 | #include "base/threading/platform_thread.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 17 | namespace base { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | namespace { |
| 19 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | class Foo { |
| 21 | public: |
| 22 | virtual void Observe(int x) = 0; |
[email protected] | bf92cfbe | 2008-08-08 16:37:43 | [diff] [blame] | 23 | virtual ~Foo() {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | class Adder : public Foo { |
| 27 | public: |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 28 | explicit Adder(int scaler) : total(0), scaler_(scaler) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 29 | void Observe(int x) override { total += x * scaler_; } |
| 30 | ~Adder() override {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | int total; |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 32 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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) |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 40 | : list_(list), |
| 41 | doomed_(doomed) { |
| 42 | } |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 43 | ~Disrupter() override {} |
| 44 | void Observe(int x) override { list_->RemoveObserver(doomed_); } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 45 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | private: |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 47 | ObserverList<Foo>* list_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 48 | Foo* doomed_; |
| 49 | }; |
| 50 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 51 | class ThreadSafeDisrupter : public Foo { |
| 52 | public: |
| 53 | ThreadSafeDisrupter(ObserverListThreadSafe<Foo>* list, Foo* doomed) |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 54 | : list_(list), |
| 55 | doomed_(doomed) { |
| 56 | } |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 57 | ~ThreadSafeDisrupter() override {} |
| 58 | void Observe(int x) override { list_->RemoveObserver(doomed_); } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 59 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 60 | private: |
| 61 | ObserverListThreadSafe<Foo>* list_; |
| 62 | Foo* doomed_; |
| 63 | }; |
| 64 | |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 65 | template <typename ObserverListType> |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 66 | class AddInObserve : public Foo { |
| 67 | public: |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 68 | explicit AddInObserve(ObserverListType* observer_list) |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 69 | : added(false), |
| 70 | observer_list(observer_list), |
| 71 | adder(1) { |
| 72 | } |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 73 | |
mostynb | 9e096de | 2014-10-07 17:59:11 | [diff] [blame] | 74 | virtual void Observe(int x) override { |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 75 | if (!added) { |
| 76 | added = true; |
| 77 | observer_list->AddObserver(&adder); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool added; |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 82 | ObserverListType* observer_list; |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 83 | Adder adder; |
| 84 | }; |
| 85 | |
| 86 | |
[email protected] | ce29201 | 2009-11-05 18:46:47 | [diff] [blame] | 87 | static const int kThreadRunTime = 2000; // ms to run the multi-threaded test. |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 88 | |
| 89 | // A thread for use in the ThreadSafeObserver test |
| 90 | // which will add and remove itself from the notification |
| 91 | // list repeatedly. |
| 92 | class AddRemoveThread : public PlatformThread::Delegate, |
| 93 | public Foo { |
| 94 | public: |
| 95 | AddRemoveThread(ObserverListThreadSafe<Foo>* list, bool notify) |
| 96 | : list_(list), |
[email protected] | 2e58cbd | 2012-08-06 01:03:05 | [diff] [blame] | 97 | loop_(NULL), |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 98 | in_list_(false), |
| 99 | start_(Time::Now()), |
| 100 | count_observes_(0), |
| 101 | count_addtask_(0), |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 102 | do_notifies_(notify), |
[email protected] | c9f977b | 2013-04-25 12:17:15 | [diff] [blame] | 103 | weak_factory_(this) { |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 104 | } |
| 105 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 106 | ~AddRemoveThread() override {} |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 107 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 108 | void ThreadMain() override { |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 109 | loop_ = new MessageLoop(); // Fire up a message loop. |
[email protected] | 0586b0e | 2010-02-12 21:38:37 | [diff] [blame] | 110 | loop_->PostTask( |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 111 | FROM_HERE, |
| 112 | base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 113 | loop_->Run(); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 114 | //LOG(ERROR) << "Loop 0x" << std::hex << loop_ << " done. " << |
| 115 | // count_observes_ << ", " << count_addtask_; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 116 | delete loop_; |
| 117 | loop_ = reinterpret_cast<MessageLoop*>(0xdeadbeef); |
[email protected] | bd60d73 | 2009-01-05 19:40:53 | [diff] [blame] | 118 | delete this; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // This task just keeps posting to itself in an attempt |
| 122 | // to race with the notifier. |
| 123 | void AddTask() { |
| 124 | count_addtask_++; |
| 125 | |
| 126 | if ((Time::Now() - start_).InMilliseconds() > kThreadRunTime) { |
[email protected] | b026e35d | 2010-10-19 02:31:03 | [diff] [blame] | 127 | VLOG(1) << "DONE!"; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!in_list_) { |
| 132 | list_->AddObserver(this); |
| 133 | in_list_ = true; |
| 134 | } |
| 135 | |
| 136 | if (do_notifies_) { |
| 137 | list_->Notify(&Foo::Observe, 10); |
| 138 | } |
| 139 | |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 140 | loop_->PostTask( |
| 141 | FROM_HERE, |
| 142 | base::Bind(&AddRemoveThread::AddTask, weak_factory_.GetWeakPtr())); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void Quit() { |
[email protected] | a085953f | 2013-02-04 23:40:00 | [diff] [blame] | 146 | loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 147 | } |
| 148 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 149 | void Observe(int x) override { |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 150 | count_observes_++; |
| 151 | |
| 152 | // If we're getting called after we removed ourselves from |
| 153 | // the list, that is very bad! |
| 154 | DCHECK(in_list_); |
| 155 | |
| 156 | // This callback should fire on the appropriate thread |
| 157 | EXPECT_EQ(loop_, MessageLoop::current()); |
| 158 | |
| 159 | list_->RemoveObserver(this); |
| 160 | in_list_ = false; |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | ObserverListThreadSafe<Foo>* list_; |
| 165 | MessageLoop* loop_; |
| 166 | bool in_list_; // Are we currently registered for notifications. |
| 167 | // in_list_ is only used on |this| thread. |
| 168 | Time start_; // The time we started the test. |
| 169 | |
| 170 | int count_observes_; // Number of times we observed. |
| 171 | int count_addtask_; // Number of times thread AddTask was called |
| 172 | bool do_notifies_; // Whether these threads should do notifications. |
| 173 | |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 174 | base::WeakPtrFactory<AddRemoveThread> weak_factory_; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 175 | }; |
| 176 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | TEST(ObserverListTest, BasicTest) { |
| 178 | ObserverList<Foo> observer_list; |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 179 | Adder a(1), b(-1), c(1), d(-1), e(-1); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 180 | Disrupter evil(&observer_list, &c); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | |
| 182 | observer_list.AddObserver(&a); |
| 183 | observer_list.AddObserver(&b); |
| 184 | |
| 185 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 186 | |
| 187 | observer_list.AddObserver(&evil); |
| 188 | observer_list.AddObserver(&c); |
| 189 | observer_list.AddObserver(&d); |
| 190 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 191 | // Removing an observer not in the list should do nothing. |
| 192 | observer_list.RemoveObserver(&e); |
| 193 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 194 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 195 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 196 | EXPECT_EQ(20, a.total); |
| 197 | EXPECT_EQ(-20, b.total); |
| 198 | EXPECT_EQ(0, c.total); |
| 199 | EXPECT_EQ(-10, d.total); |
| 200 | EXPECT_EQ(0, e.total); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 201 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 202 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 203 | TEST(ObserverListThreadSafeTest, BasicTest) { |
| 204 | MessageLoop loop; |
| 205 | |
| 206 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 207 | new ObserverListThreadSafe<Foo>); |
| 208 | Adder a(1); |
| 209 | Adder b(-1); |
| 210 | Adder c(1); |
| 211 | Adder d(-1); |
| 212 | ThreadSafeDisrupter evil(observer_list.get(), &c); |
| 213 | |
| 214 | observer_list->AddObserver(&a); |
| 215 | observer_list->AddObserver(&b); |
| 216 | |
| 217 | observer_list->Notify(&Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 218 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 219 | |
| 220 | observer_list->AddObserver(&evil); |
| 221 | observer_list->AddObserver(&c); |
| 222 | observer_list->AddObserver(&d); |
| 223 | |
| 224 | observer_list->Notify(&Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 225 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 226 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 227 | EXPECT_EQ(20, a.total); |
| 228 | EXPECT_EQ(-20, b.total); |
| 229 | EXPECT_EQ(0, c.total); |
| 230 | EXPECT_EQ(-10, d.total); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 233 | TEST(ObserverListThreadSafeTest, RemoveObserver) { |
| 234 | MessageLoop loop; |
| 235 | |
| 236 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 237 | new ObserverListThreadSafe<Foo>); |
| 238 | Adder a(1), b(1); |
| 239 | |
[email protected] | 3fe674d | 2012-04-05 17:52:10 | [diff] [blame] | 240 | // A workaround for the compiler bug. See http://crbug.com/121960. |
| 241 | EXPECT_NE(&a, &b); |
| 242 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 243 | // Should do nothing. |
| 244 | observer_list->RemoveObserver(&a); |
| 245 | observer_list->RemoveObserver(&b); |
| 246 | |
| 247 | observer_list->Notify(&Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 248 | RunLoop().RunUntilIdle(); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 249 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 250 | EXPECT_EQ(0, a.total); |
| 251 | EXPECT_EQ(0, b.total); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 252 | |
| 253 | observer_list->AddObserver(&a); |
| 254 | |
| 255 | // Should also do nothing. |
| 256 | observer_list->RemoveObserver(&b); |
| 257 | |
| 258 | observer_list->Notify(&Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 259 | RunLoop().RunUntilIdle(); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 260 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 261 | EXPECT_EQ(10, a.total); |
| 262 | EXPECT_EQ(0, b.total); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 265 | TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { |
| 266 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 267 | new ObserverListThreadSafe<Foo>); |
| 268 | |
| 269 | Adder a(1), b(1), c(1); |
| 270 | |
| 271 | // No MessageLoop, so these should not be added. |
| 272 | observer_list->AddObserver(&a); |
| 273 | observer_list->AddObserver(&b); |
| 274 | |
| 275 | { |
| 276 | // Add c when there's a loop. |
| 277 | MessageLoop loop; |
| 278 | observer_list->AddObserver(&c); |
| 279 | |
| 280 | observer_list->Notify(&Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 281 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 282 | |
| 283 | EXPECT_EQ(0, a.total); |
| 284 | EXPECT_EQ(0, b.total); |
| 285 | EXPECT_EQ(10, c.total); |
| 286 | |
| 287 | // Now add a when there's a loop. |
| 288 | observer_list->AddObserver(&a); |
| 289 | |
| 290 | // Remove c when there's a loop. |
| 291 | observer_list->RemoveObserver(&c); |
| 292 | |
| 293 | // Notify again. |
| 294 | observer_list->Notify(&Foo::Observe, 20); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 295 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 296 | |
| 297 | EXPECT_EQ(20, a.total); |
| 298 | EXPECT_EQ(0, b.total); |
| 299 | EXPECT_EQ(10, c.total); |
| 300 | } |
| 301 | |
| 302 | // Removing should always succeed with or without a loop. |
| 303 | observer_list->RemoveObserver(&a); |
| 304 | |
| 305 | // Notifying should not fail but should also be a no-op. |
| 306 | MessageLoop loop; |
| 307 | observer_list->AddObserver(&b); |
| 308 | observer_list->Notify(&Foo::Observe, 30); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 309 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 310 | |
| 311 | EXPECT_EQ(20, a.total); |
| 312 | EXPECT_EQ(30, b.total); |
| 313 | EXPECT_EQ(10, c.total); |
| 314 | } |
| 315 | |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 316 | class FooRemover : public Foo { |
| 317 | public: |
| 318 | explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 319 | ~FooRemover() override {} |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 320 | |
| 321 | void AddFooToRemove(Foo* foo) { |
| 322 | foos_.push_back(foo); |
| 323 | } |
| 324 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 325 | void Observe(int x) override { |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 326 | std::vector<Foo*> tmp; |
| 327 | tmp.swap(foos_); |
| 328 | for (std::vector<Foo*>::iterator it = tmp.begin(); |
| 329 | it != tmp.end(); ++it) { |
| 330 | list_->RemoveObserver(*it); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | const scoped_refptr<ObserverListThreadSafe<Foo> > list_; |
| 336 | std::vector<Foo*> foos_; |
| 337 | }; |
| 338 | |
| 339 | TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) { |
| 340 | MessageLoop loop; |
| 341 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 342 | new ObserverListThreadSafe<Foo>); |
| 343 | |
[email protected] | 3703e92 | 2013-05-31 21:37:53 | [diff] [blame] | 344 | FooRemover a(observer_list.get()); |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 345 | Adder b(1); |
| 346 | |
| 347 | observer_list->AddObserver(&a); |
| 348 | observer_list->AddObserver(&b); |
| 349 | |
| 350 | a.AddFooToRemove(&a); |
| 351 | a.AddFooToRemove(&b); |
| 352 | |
| 353 | observer_list->Notify(&Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 354 | RunLoop().RunUntilIdle(); |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 355 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 356 | |
| 357 | // A test driver for a multi-threaded notification loop. Runs a number |
| 358 | // of observer threads, each of which constantly adds/removes itself |
| 359 | // from the observer list. Optionally, if cross_thread_notifies is set |
| 360 | // to true, the observer threads will also trigger notifications to |
| 361 | // all observers. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 362 | static void ThreadSafeObserverHarness(int num_threads, |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 363 | bool cross_thread_notifies) { |
| 364 | MessageLoop loop; |
| 365 | |
| 366 | const int kMaxThreads = 15; |
| 367 | num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads; |
| 368 | |
| 369 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 370 | new ObserverListThreadSafe<Foo>); |
| 371 | Adder a(1); |
| 372 | Adder b(-1); |
| 373 | Adder c(1); |
| 374 | Adder d(-1); |
| 375 | |
| 376 | observer_list->AddObserver(&a); |
| 377 | observer_list->AddObserver(&b); |
| 378 | |
| 379 | AddRemoveThread* threaded_observer[kMaxThreads]; |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 380 | base::PlatformThreadHandle threads[kMaxThreads]; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 381 | for (int index = 0; index < num_threads; index++) { |
| 382 | threaded_observer[index] = new AddRemoveThread(observer_list.get(), false); |
| 383 | EXPECT_TRUE(PlatformThread::Create(0, |
| 384 | threaded_observer[index], &threads[index])); |
| 385 | } |
| 386 | |
| 387 | Time start = Time::Now(); |
| 388 | while (true) { |
| 389 | if ((Time::Now() - start).InMilliseconds() > kThreadRunTime) |
| 390 | break; |
| 391 | |
| 392 | observer_list->Notify(&Foo::Observe, 10); |
| 393 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 394 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | for (int index = 0; index < num_threads; index++) { |
| 398 | threaded_observer[index]->Quit(); |
| 399 | PlatformThread::Join(threads[index]); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | TEST(ObserverListThreadSafeTest, CrossThreadObserver) { |
| 404 | // Use 7 observer threads. Notifications only come from |
| 405 | // the main thread. |
| 406 | ThreadSafeObserverHarness(7, false); |
| 407 | } |
| 408 | |
| 409 | TEST(ObserverListThreadSafeTest, CrossThreadNotifications) { |
| 410 | // Use 3 observer threads. Notifications will fire from |
| 411 | // the main thread and all 3 observer threads. |
| 412 | ThreadSafeObserverHarness(3, true); |
| 413 | } |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 414 | |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 415 | TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) { |
| 416 | MessageLoop* loop = new MessageLoop; |
| 417 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 418 | new ObserverListThreadSafe<Foo>); |
| 419 | |
| 420 | Adder a(1); |
| 421 | observer_list->AddObserver(&a); |
| 422 | delete loop; |
| 423 | // Test passes if we don't crash here. |
| 424 | observer_list->Notify(&Foo::Observe, 1); |
| 425 | } |
| 426 | |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 427 | TEST(ObserverListTest, Existing) { |
| 428 | ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 429 | Adder a(1); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 430 | AddInObserve<ObserverList<Foo> > b(&observer_list); |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 431 | |
| 432 | observer_list.AddObserver(&a); |
| 433 | observer_list.AddObserver(&b); |
| 434 | |
| 435 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 436 | |
| 437 | EXPECT_TRUE(b.added); |
| 438 | // B's adder should not have been notified because it was added during |
[email protected] | 6be50d7 | 2014-05-08 23:49:40 | [diff] [blame] | 439 | // notification. |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 440 | EXPECT_EQ(0, b.adder.total); |
| 441 | |
| 442 | // Notify again to make sure b's adder is notified. |
| 443 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 444 | EXPECT_EQ(1, b.adder.total); |
| 445 | } |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 446 | |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 447 | // Same as above, but for ObserverListThreadSafe |
| 448 | TEST(ObserverListThreadSafeTest, Existing) { |
| 449 | MessageLoop loop; |
| 450 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 451 | new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY)); |
| 452 | Adder a(1); |
| 453 | AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get()); |
| 454 | |
| 455 | observer_list->AddObserver(&a); |
| 456 | observer_list->AddObserver(&b); |
| 457 | |
| 458 | observer_list->Notify(&Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 459 | RunLoop().RunUntilIdle(); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 460 | |
| 461 | EXPECT_TRUE(b.added); |
| 462 | // B's adder should not have been notified because it was added during |
[email protected] | 6be50d7 | 2014-05-08 23:49:40 | [diff] [blame] | 463 | // notification. |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 464 | EXPECT_EQ(0, b.adder.total); |
| 465 | |
| 466 | // Notify again to make sure b's adder is notified. |
| 467 | observer_list->Notify(&Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 468 | RunLoop().RunUntilIdle(); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 469 | EXPECT_EQ(1, b.adder.total); |
| 470 | } |
| 471 | |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 472 | class AddInClearObserve : public Foo { |
| 473 | public: |
| 474 | explicit AddInClearObserve(ObserverList<Foo>* list) |
| 475 | : list_(list), added_(false), adder_(1) {} |
| 476 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 477 | void Observe(int /* x */) override { |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 478 | list_->Clear(); |
| 479 | list_->AddObserver(&adder_); |
| 480 | added_ = true; |
| 481 | } |
| 482 | |
| 483 | bool added() const { return added_; } |
| 484 | const Adder& adder() const { return adder_; } |
| 485 | |
| 486 | private: |
| 487 | ObserverList<Foo>* const list_; |
| 488 | |
| 489 | bool added_; |
| 490 | Adder adder_; |
| 491 | }; |
| 492 | |
| 493 | TEST(ObserverListTest, ClearNotifyAll) { |
| 494 | ObserverList<Foo> observer_list; |
| 495 | AddInClearObserve a(&observer_list); |
| 496 | |
| 497 | observer_list.AddObserver(&a); |
| 498 | |
| 499 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 500 | EXPECT_TRUE(a.added()); |
| 501 | EXPECT_EQ(1, a.adder().total) |
| 502 | << "Adder should observe once and have sum of 1."; |
| 503 | } |
| 504 | |
| 505 | TEST(ObserverListTest, ClearNotifyExistingOnly) { |
| 506 | ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 507 | AddInClearObserve a(&observer_list); |
| 508 | |
| 509 | observer_list.AddObserver(&a); |
| 510 | |
| 511 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 512 | EXPECT_TRUE(a.added()); |
| 513 | EXPECT_EQ(0, a.adder().total) |
| 514 | << "Adder should not observe, so sum should still be 0."; |
| 515 | } |
| 516 | |
[email protected] | 671b74d | 2011-06-09 03:41:18 | [diff] [blame] | 517 | class ListDestructor : public Foo { |
| 518 | public: |
| 519 | explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 520 | ~ListDestructor() override {} |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 521 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame^] | 522 | void Observe(int x) override { delete list_; } |
[email protected] | 2e58cbd | 2012-08-06 01:03:05 | [diff] [blame] | 523 | |
[email protected] | 671b74d | 2011-06-09 03:41:18 | [diff] [blame] | 524 | private: |
| 525 | ObserverList<Foo>* list_; |
| 526 | }; |
| 527 | |
| 528 | |
| 529 | TEST(ObserverListTest, IteratorOutlivesList) { |
| 530 | ObserverList<Foo>* observer_list = new ObserverList<Foo>; |
| 531 | ListDestructor a(observer_list); |
| 532 | observer_list->AddObserver(&a); |
| 533 | |
| 534 | FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
| 535 | // If this test fails, there'll be Valgrind errors when this function goes out |
| 536 | // of scope. |
| 537 | } |
| 538 | |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 539 | } // namespace |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 540 | } // namespace base |