Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: webrtc/base/sigslot_unittest.cc

Issue 2846593005: Relanding #2: Fixing crash that can occur if signal is modified while firing. (Closed)
Patch Set: Fixing issue with disconnect_all, adding test, simplifying code a bit Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/sigslot.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 sigslot::signal<> signal; 265 sigslot::signal<> signal;
266 SigslotReceiver<> receiver; 266 SigslotReceiver<> receiver;
267 receiver.Connect(&signal); 267 receiver.Connect(&signal);
268 268
269 // Fire the signal after copying the receiver, expecting the copied receiver 269 // Fire the signal after copying the receiver, expecting the copied receiver
270 // to be notified. 270 // to be notified.
271 SigslotReceiver<> copied_receiver(receiver); 271 SigslotReceiver<> copied_receiver(receiver);
272 signal(); 272 signal();
273 EXPECT_EQ(1, copied_receiver.signal_count()); 273 EXPECT_EQ(1, copied_receiver.signal_count());
274 } 274 }
275
276 // Just used for the test below.
277 class Disconnector : public sigslot::has_slots<> {
278 public:
279 Disconnector(SigslotReceiver<>* receiver1, SigslotReceiver<>* receiver2)
280 : receiver1_(receiver1), receiver2_(receiver2) {}
281
282 void Connect(sigslot::signal<>* signal) {
283 signal_ = signal;
284 signal->connect(this, &Disconnector::Disconnect);
285 }
286
287 private:
288 void Disconnect() {
289 receiver1_->Disconnect();
290 receiver2_->Disconnect();
291 signal_->disconnect(this);
292 }
293
294 sigslot::signal<>* signal_;
295 SigslotReceiver<>* receiver1_;
296 SigslotReceiver<>* receiver2_;
297 };
298
299 // Test that things work as expected if a signal is disconnected from a slot
300 // while it's firing.
301 TEST(SigslotTest, DisconnectFromSignalWhileFiring) {
302 sigslot::signal<> signal;
303 SigslotReceiver<> receiver1;
304 SigslotReceiver<> receiver2;
305 SigslotReceiver<> receiver3;
306 Disconnector disconnector(&receiver1, &receiver2);
307
308 // From this ordering, receiver1 should receive the signal, then the
309 // disconnector will be invoked, causing receiver2 to be disconnected before
310 // it receives the signal. And receiver3 should also receive the signal,
311 // since it was never disconnected.
312 receiver1.Connect(&signal);
313 disconnector.Connect(&signal);
314 receiver2.Connect(&signal);
315 receiver3.Connect(&signal);
316 signal();
317
318 EXPECT_EQ(1, receiver1.signal_count());
319 EXPECT_EQ(0, receiver2.signal_count());
320 EXPECT_EQ(1, receiver3.signal_count());
321 }
322
323 // Uses disconnect_all instead of disconnect.
324 class Disconnector2 : public sigslot::has_slots<> {
325 public:
326 void Connect(sigslot::signal<>* signal) {
327 signal_ = signal;
328 signal->connect(this, &Disconnector2::Disconnect);
329 }
330
331 private:
332 void Disconnect() {
333 signal_->disconnect_all();
334 }
335
336 sigslot::signal<>* signal_;
337 };
338
339 // Test that things work as expected if a signal is disconnected from a slot
340 // while it's firing using disconnect_all.
341 TEST(SigslotTest, CallDisconnectAllWhileSignalFiring) {
342 sigslot::signal<> signal;
343 SigslotReceiver<> receiver1;
344 SigslotReceiver<> receiver2;
345 Disconnector2 disconnector;
346
347 // From this ordering, receiver1 should receive the signal, then the
348 // disconnector will be invoked, causing receiver2 to be disconnected before
349 // it receives the signal.
350 receiver1.Connect(&signal);
351 disconnector.Connect(&signal);
352 receiver2.Connect(&signal);
353 signal();
354
355 EXPECT_EQ(1, receiver1.signal_count());
356 EXPECT_EQ(0, receiver2.signal_count());
357 }
OLDNEW
« no previous file with comments | « webrtc/base/sigslot.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698