[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" |
stevenjb | 7669182 | 2015-04-29 21:43:03 | [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 | |
nick | c0b00106 | 2015-04-22 23:17:20 | [diff] [blame] | 74 | 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), |
robliao | 6f5e423 | 2015-04-23 22:45:32 | [diff] [blame] | 97 | loop_(nullptr), |
[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. |
stevenjb | 7669182 | 2015-04-29 21:43:03 | [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_) { |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 137 | list_->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 138 | } |
| 139 | |
stevenjb | 7669182 | 2015-04-29 21:43:03 | [diff] [blame] | 140 | loop_->PostTask( |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 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() { |
stevenjb | 7669182 | 2015-04-29 21:43:03 | [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 | |
mgiuca | 64ccf236 | 2014-11-10 06:44:23 | [diff] [blame] | 185 | EXPECT_TRUE(observer_list.HasObserver(&a)); |
| 186 | EXPECT_FALSE(observer_list.HasObserver(&c)); |
| 187 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 189 | |
| 190 | observer_list.AddObserver(&evil); |
| 191 | observer_list.AddObserver(&c); |
| 192 | observer_list.AddObserver(&d); |
| 193 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 194 | // Removing an observer not in the list should do nothing. |
| 195 | observer_list.RemoveObserver(&e); |
| 196 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(10)); |
| 198 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 199 | EXPECT_EQ(20, a.total); |
| 200 | EXPECT_EQ(-20, b.total); |
| 201 | EXPECT_EQ(0, c.total); |
| 202 | EXPECT_EQ(-10, d.total); |
| 203 | EXPECT_EQ(0, e.total); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 205 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 206 | TEST(ObserverListThreadSafeTest, BasicTest) { |
| 207 | MessageLoop loop; |
| 208 | |
| 209 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 210 | new ObserverListThreadSafe<Foo>); |
| 211 | Adder a(1); |
| 212 | Adder b(-1); |
| 213 | Adder c(1); |
| 214 | Adder d(-1); |
| 215 | ThreadSafeDisrupter evil(observer_list.get(), &c); |
| 216 | |
| 217 | observer_list->AddObserver(&a); |
| 218 | observer_list->AddObserver(&b); |
| 219 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 220 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 221 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 222 | |
| 223 | observer_list->AddObserver(&evil); |
| 224 | observer_list->AddObserver(&c); |
| 225 | observer_list->AddObserver(&d); |
| 226 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 227 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 228 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 229 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 230 | EXPECT_EQ(20, a.total); |
| 231 | EXPECT_EQ(-20, b.total); |
| 232 | EXPECT_EQ(0, c.total); |
| 233 | EXPECT_EQ(-10, d.total); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 236 | TEST(ObserverListThreadSafeTest, RemoveObserver) { |
| 237 | MessageLoop loop; |
| 238 | |
| 239 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 240 | new ObserverListThreadSafe<Foo>); |
| 241 | Adder a(1), b(1); |
| 242 | |
[email protected] | 3fe674d | 2012-04-05 17:52:10 | [diff] [blame] | 243 | // A workaround for the compiler bug. See http://crbug.com/121960. |
| 244 | EXPECT_NE(&a, &b); |
| 245 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 246 | // Should do nothing. |
| 247 | observer_list->RemoveObserver(&a); |
| 248 | observer_list->RemoveObserver(&b); |
| 249 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 250 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 251 | RunLoop().RunUntilIdle(); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 252 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 253 | EXPECT_EQ(0, a.total); |
| 254 | EXPECT_EQ(0, b.total); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 255 | |
| 256 | observer_list->AddObserver(&a); |
| 257 | |
| 258 | // Should also do nothing. |
| 259 | observer_list->RemoveObserver(&b); |
| 260 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 261 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 262 | RunLoop().RunUntilIdle(); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 263 | |
[email protected] | 14cf910 | 2012-08-23 09:59:19 | [diff] [blame] | 264 | EXPECT_EQ(10, a.total); |
| 265 | EXPECT_EQ(0, b.total); |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 268 | TEST(ObserverListThreadSafeTest, WithoutMessageLoop) { |
| 269 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 270 | new ObserverListThreadSafe<Foo>); |
| 271 | |
| 272 | Adder a(1), b(1), c(1); |
| 273 | |
| 274 | // No MessageLoop, so these should not be added. |
| 275 | observer_list->AddObserver(&a); |
| 276 | observer_list->AddObserver(&b); |
| 277 | |
| 278 | { |
| 279 | // Add c when there's a loop. |
| 280 | MessageLoop loop; |
| 281 | observer_list->AddObserver(&c); |
| 282 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 283 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 284 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 285 | |
| 286 | EXPECT_EQ(0, a.total); |
| 287 | EXPECT_EQ(0, b.total); |
| 288 | EXPECT_EQ(10, c.total); |
| 289 | |
| 290 | // Now add a when there's a loop. |
| 291 | observer_list->AddObserver(&a); |
| 292 | |
| 293 | // Remove c when there's a loop. |
| 294 | observer_list->RemoveObserver(&c); |
| 295 | |
| 296 | // Notify again. |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 297 | observer_list->Notify(FROM_HERE, &Foo::Observe, 20); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 298 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 299 | |
| 300 | EXPECT_EQ(20, a.total); |
| 301 | EXPECT_EQ(0, b.total); |
| 302 | EXPECT_EQ(10, c.total); |
| 303 | } |
| 304 | |
| 305 | // Removing should always succeed with or without a loop. |
| 306 | observer_list->RemoveObserver(&a); |
| 307 | |
| 308 | // Notifying should not fail but should also be a no-op. |
| 309 | MessageLoop loop; |
| 310 | observer_list->AddObserver(&b); |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 311 | observer_list->Notify(FROM_HERE, &Foo::Observe, 30); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 312 | RunLoop().RunUntilIdle(); |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 313 | |
| 314 | EXPECT_EQ(20, a.total); |
| 315 | EXPECT_EQ(30, b.total); |
| 316 | EXPECT_EQ(10, c.total); |
| 317 | } |
| 318 | |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 319 | class FooRemover : public Foo { |
| 320 | public: |
| 321 | explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 322 | ~FooRemover() override {} |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 323 | |
| 324 | void AddFooToRemove(Foo* foo) { |
| 325 | foos_.push_back(foo); |
| 326 | } |
| 327 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 328 | void Observe(int x) override { |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 329 | std::vector<Foo*> tmp; |
| 330 | tmp.swap(foos_); |
| 331 | for (std::vector<Foo*>::iterator it = tmp.begin(); |
| 332 | it != tmp.end(); ++it) { |
| 333 | list_->RemoveObserver(*it); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | const scoped_refptr<ObserverListThreadSafe<Foo> > list_; |
| 339 | std::vector<Foo*> foos_; |
| 340 | }; |
| 341 | |
| 342 | TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) { |
| 343 | MessageLoop loop; |
| 344 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 345 | new ObserverListThreadSafe<Foo>); |
| 346 | |
[email protected] | 3703e92 | 2013-05-31 21:37:53 | [diff] [blame] | 347 | FooRemover a(observer_list.get()); |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 348 | Adder b(1); |
| 349 | |
| 350 | observer_list->AddObserver(&a); |
| 351 | observer_list->AddObserver(&b); |
| 352 | |
| 353 | a.AddFooToRemove(&a); |
| 354 | a.AddFooToRemove(&b); |
| 355 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 356 | observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 357 | RunLoop().RunUntilIdle(); |
[email protected] | 3c0d45e | 2010-09-17 19:33:06 | [diff] [blame] | 358 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 359 | |
| 360 | // A test driver for a multi-threaded notification loop. Runs a number |
| 361 | // of observer threads, each of which constantly adds/removes itself |
| 362 | // from the observer list. Optionally, if cross_thread_notifies is set |
| 363 | // to true, the observer threads will also trigger notifications to |
| 364 | // all observers. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 365 | static void ThreadSafeObserverHarness(int num_threads, |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 366 | bool cross_thread_notifies) { |
| 367 | MessageLoop loop; |
| 368 | |
| 369 | const int kMaxThreads = 15; |
| 370 | num_threads = num_threads > kMaxThreads ? kMaxThreads : num_threads; |
| 371 | |
| 372 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 373 | new ObserverListThreadSafe<Foo>); |
| 374 | Adder a(1); |
| 375 | Adder b(-1); |
| 376 | Adder c(1); |
| 377 | Adder d(-1); |
| 378 | |
| 379 | observer_list->AddObserver(&a); |
| 380 | observer_list->AddObserver(&b); |
| 381 | |
| 382 | AddRemoveThread* threaded_observer[kMaxThreads]; |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 383 | base::PlatformThreadHandle threads[kMaxThreads]; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 384 | for (int index = 0; index < num_threads; index++) { |
| 385 | threaded_observer[index] = new AddRemoveThread(observer_list.get(), false); |
| 386 | EXPECT_TRUE(PlatformThread::Create(0, |
| 387 | threaded_observer[index], &threads[index])); |
| 388 | } |
| 389 | |
| 390 | Time start = Time::Now(); |
| 391 | while (true) { |
| 392 | if ((Time::Now() - start).InMilliseconds() > kThreadRunTime) |
| 393 | break; |
| 394 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 395 | observer_list->Notify(FROM_HERE, &Foo::Observe, 10); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 396 | |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 397 | RunLoop().RunUntilIdle(); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | for (int index = 0; index < num_threads; index++) { |
| 401 | threaded_observer[index]->Quit(); |
| 402 | PlatformThread::Join(threads[index]); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | TEST(ObserverListThreadSafeTest, CrossThreadObserver) { |
| 407 | // Use 7 observer threads. Notifications only come from |
| 408 | // the main thread. |
| 409 | ThreadSafeObserverHarness(7, false); |
| 410 | } |
| 411 | |
| 412 | TEST(ObserverListThreadSafeTest, CrossThreadNotifications) { |
| 413 | // Use 3 observer threads. Notifications will fire from |
| 414 | // the main thread and all 3 observer threads. |
| 415 | ThreadSafeObserverHarness(3, true); |
| 416 | } |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 417 | |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 418 | TEST(ObserverListThreadSafeTest, OutlivesMessageLoop) { |
| 419 | MessageLoop* loop = new MessageLoop; |
| 420 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 421 | new ObserverListThreadSafe<Foo>); |
| 422 | |
| 423 | Adder a(1); |
| 424 | observer_list->AddObserver(&a); |
| 425 | delete loop; |
| 426 | // Test passes if we don't crash here. |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 427 | observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 430 | TEST(ObserverListTest, Existing) { |
| 431 | ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 432 | Adder a(1); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 433 | AddInObserve<ObserverList<Foo> > b(&observer_list); |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 434 | |
| 435 | observer_list.AddObserver(&a); |
| 436 | observer_list.AddObserver(&b); |
| 437 | |
| 438 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 439 | |
| 440 | EXPECT_TRUE(b.added); |
| 441 | // B's adder should not have been notified because it was added during |
[email protected] | 6be50d7 | 2014-05-08 23:49:40 | [diff] [blame] | 442 | // notification. |
[email protected] | b3e2fad0 | 2008-10-31 03:32:06 | [diff] [blame] | 443 | EXPECT_EQ(0, b.adder.total); |
| 444 | |
| 445 | // Notify again to make sure b's adder is notified. |
| 446 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 447 | EXPECT_EQ(1, b.adder.total); |
| 448 | } |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 449 | |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 450 | // Same as above, but for ObserverListThreadSafe |
| 451 | TEST(ObserverListThreadSafeTest, Existing) { |
| 452 | MessageLoop loop; |
| 453 | scoped_refptr<ObserverListThreadSafe<Foo> > observer_list( |
| 454 | new ObserverListThreadSafe<Foo>(ObserverList<Foo>::NOTIFY_EXISTING_ONLY)); |
| 455 | Adder a(1); |
| 456 | AddInObserve<ObserverListThreadSafe<Foo> > b(observer_list.get()); |
| 457 | |
| 458 | observer_list->AddObserver(&a); |
| 459 | observer_list->AddObserver(&b); |
| 460 | |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 461 | observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 462 | RunLoop().RunUntilIdle(); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 463 | |
| 464 | EXPECT_TRUE(b.added); |
| 465 | // B's adder should not have been notified because it was added during |
[email protected] | 6be50d7 | 2014-05-08 23:49:40 | [diff] [blame] | 466 | // notification. |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 467 | EXPECT_EQ(0, b.adder.total); |
| 468 | |
| 469 | // Notify again to make sure b's adder is notified. |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 470 | observer_list->Notify(FROM_HERE, &Foo::Observe, 1); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 471 | RunLoop().RunUntilIdle(); |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 472 | EXPECT_EQ(1, b.adder.total); |
| 473 | } |
| 474 | |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 475 | class AddInClearObserve : public Foo { |
| 476 | public: |
| 477 | explicit AddInClearObserve(ObserverList<Foo>* list) |
| 478 | : list_(list), added_(false), adder_(1) {} |
| 479 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 480 | void Observe(int /* x */) override { |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 481 | list_->Clear(); |
| 482 | list_->AddObserver(&adder_); |
| 483 | added_ = true; |
| 484 | } |
| 485 | |
| 486 | bool added() const { return added_; } |
| 487 | const Adder& adder() const { return adder_; } |
| 488 | |
| 489 | private: |
| 490 | ObserverList<Foo>* const list_; |
| 491 | |
| 492 | bool added_; |
| 493 | Adder adder_; |
| 494 | }; |
| 495 | |
| 496 | TEST(ObserverListTest, ClearNotifyAll) { |
| 497 | ObserverList<Foo> observer_list; |
| 498 | AddInClearObserve a(&observer_list); |
| 499 | |
| 500 | observer_list.AddObserver(&a); |
| 501 | |
| 502 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 503 | EXPECT_TRUE(a.added()); |
| 504 | EXPECT_EQ(1, a.adder().total) |
| 505 | << "Adder should observe once and have sum of 1."; |
| 506 | } |
| 507 | |
| 508 | TEST(ObserverListTest, ClearNotifyExistingOnly) { |
| 509 | ObserverList<Foo> observer_list(ObserverList<Foo>::NOTIFY_EXISTING_ONLY); |
| 510 | AddInClearObserve a(&observer_list); |
| 511 | |
| 512 | observer_list.AddObserver(&a); |
| 513 | |
| 514 | FOR_EACH_OBSERVER(Foo, observer_list, Observe(1)); |
| 515 | EXPECT_TRUE(a.added()); |
| 516 | EXPECT_EQ(0, a.adder().total) |
| 517 | << "Adder should not observe, so sum should still be 0."; |
| 518 | } |
| 519 | |
[email protected] | 671b74d | 2011-06-09 03:41:18 | [diff] [blame] | 520 | class ListDestructor : public Foo { |
| 521 | public: |
| 522 | explicit ListDestructor(ObserverList<Foo>* list) : list_(list) {} |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 523 | ~ListDestructor() override {} |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 524 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 525 | void Observe(int x) override { delete list_; } |
[email protected] | 2e58cbd | 2012-08-06 01:03:05 | [diff] [blame] | 526 | |
[email protected] | 671b74d | 2011-06-09 03:41:18 | [diff] [blame] | 527 | private: |
| 528 | ObserverList<Foo>* list_; |
| 529 | }; |
| 530 | |
| 531 | |
| 532 | TEST(ObserverListTest, IteratorOutlivesList) { |
| 533 | ObserverList<Foo>* observer_list = new ObserverList<Foo>; |
| 534 | ListDestructor a(observer_list); |
| 535 | observer_list->AddObserver(&a); |
| 536 | |
| 537 | FOR_EACH_OBSERVER(Foo, *observer_list, Observe(0)); |
| 538 | // If this test fails, there'll be Valgrind errors when this function goes out |
| 539 | // of scope. |
| 540 | } |
| 541 | |
[email protected] | 84aebed | 2010-02-25 03:09:41 | [diff] [blame] | 542 | } // namespace |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 543 | } // namespace base |