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