-
Notifications
You must be signed in to change notification settings - Fork 17
/
AutoSignalTest.cpp
735 lines (582 loc) · 19.7 KB
/
AutoSignalTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/autowiring.h>
#include <thread>
namespace {
class CountsCopies {
public:
CountsCopies(void) = default;
CountsCopies(const CountsCopies&) { nCopies++; }
CountsCopies(CountsCopies&& rhs) { nMoves++; }
void operator=(const CountsCopies&) { nCopies++; }
void operator=(CountsCopies&& rhs) { nMoves++; }
static int nCopies;
static int nMoves;
};
int CountsCopies::nCopies = 0;
int CountsCopies::nMoves = 0;
}
class AutoSignalTest:
public testing::Test
{
void SetUp(void) override {
CountsCopies::nCopies = 0;
CountsCopies::nMoves = 0;
}
};
class RaisesASignal {
public:
autowiring::signal<void(int)> signal;
};
TEST_F(AutoSignalTest, SimpleSignalTest) {
autowiring::signal<void(int)> signal;
bool handler_called = false;
int val = 0;
// Register a handler directly on the signal:
auto registration =
signal += [&](int v) {
handler_called = true;
val = v;
};
// Trivially raise the signal:
signal(101);
ASSERT_TRUE(handler_called) << "Handler was not called on a stack-allocated signal";
ASSERT_EQ(101, val) << "Handler did not correctly copy a passed value";
// Unregister and verify we can still raise the signal:
signal -= registration;
handler_called = false;
signal(102);
ASSERT_FALSE(handler_called) << "Handler was called after being unregistered";
}
TEST_F(AutoSignalTest, SignalWithAutowiring) {
bool handler_called = false;
bool handler2_called = false;
int val = 202;
{
Autowired<RaisesASignal> ras;
// Register a signal handler:
ras(&RaisesASignal::signal) += [&](int v) {
handler_called = true;
val = v;
};
// Inject type type after the signal has been registered
AutoRequired<RaisesASignal>();
ras(&RaisesASignal::signal) += [&](int v) {
handler2_called = true;
};
// Now raise the signal, see what happens:
ras->signal(55);
// Verify that the handler got called with the correct value:
ASSERT_TRUE(handler_called) << "Signal handler was not invoked";
ASSERT_TRUE(handler2_called) << "Second Signal handler was not invoked";
ASSERT_EQ(55, val) << "Signal handler not called with the correct parameter as expected";
}
// Raise the signal again, this should not cause anything to break:
Autowired<RaisesASignal> ras;
handler_called = false;
ras->signal(99);
ASSERT_FALSE(handler_called) << "A handler was unexpectedly called after it should have been destroyed";
}
struct RaisesASignalDerived:
public RaisesASignal
{};
TEST_F(AutoSignalTest, SignalWithAutowiringDerived) {
bool handler_called = false;
bool wired = false;
int val = 202;
{
Autowired<RaisesASignal> ras;
// Register a signal handler on the base type
ras(&RaisesASignal::signal) += [&](int v) {
handler_called = true;
val = v;
};
ras.NotifyWhenAutowired([&]() {
wired = true;
});
// Inject derived type type after the signal has been registered
AutoRequired<RaisesASignalDerived> derived;
// Now raise the signal on the derived object and see what happens:
derived->signal(55);
// Verify that the handler got called with the correct value:
ASSERT_TRUE(wired) << "Signal handler was not autowired";
ASSERT_TRUE(handler_called) << "Signal handler was not invoked";
ASSERT_EQ(55, val) << "Signal handler not called with the correct parameter as expected";
}
// Raise the signal again, this should not cause anything to break:
Autowired<RaisesASignal> ras;
handler_called = false;
ras->signal(99);
ASSERT_FALSE(handler_called) << "A handler was unexpectedly called after it should have been destroyed";
}
struct ContainsRaises {
ContainsRaises(void) {
ras(&RaisesASignal::signal) += [this](int v) {
count++;
};
}
~ContainsRaises() {
static int i = 0;
i++;
}
Autowired<RaisesASignal> ras;
int count = 0;
};
TEST_F(AutoSignalTest, ConstructorAutowiredRegistration) {
bool handler_called = false;
int val = 202;
{
AutoCurrentContext rootContext;
auto ctxt = rootContext->Create<void>();
CurrentContextPusher pshr(ctxt);
(void)pshr;
{
Autowired<RaisesASignalDerived> rasDerived;
// Register a signal handler:
rasDerived(&RaisesASignal::signal) += [&](int v) {
handler_called = true;
val = v;
};
AutoRequired<ContainsRaises> cRas;
// Inject type type after the signal has been registered
AutoRequired<RaisesASignalDerived>();
// Now raise the signal, see what happens:
rasDerived->signal(55);
// Verify that the handler got called with the correct value:
ASSERT_TRUE(handler_called) << "Signal handler was not invoked";
ASSERT_TRUE(cRas->count > 0) << "Signal hander in object constructor was not invoked";
ASSERT_EQ(55, val) << "Signal handler not called with the correct parameter as expected";
}
// Raise the signal again, this should not cause anything to break:
Autowired<RaisesASignal> ras;
handler_called = false;
ras->signal(99);
ASSERT_FALSE(handler_called) << "A handler was unexpectedly called after it should have been destroyed";
}
}
TEST_F(AutoSignalTest, MultipleSlotsTest) {
autowiring::signal<void(void)> signal;
bool handler_called1 = false;
bool handler_called2 = false;
auto registration1 =
signal += [&] {
handler_called1 = true;
};
// Registration 2
signal += [&] {
handler_called2 = true;
};
// Trivially raise the signal:
signal();
ASSERT_TRUE(handler_called1) << "Handler 1 was not called on a stack-allocated signal";
ASSERT_TRUE(handler_called2) << "Handler 2 was not called on a stack-allocated signal";
// Unregister the first signal and reset the variables
signal -= registration1;
handler_called1 = false;
handler_called2 = false;
// Verify that registration 2 can still receive the signals
signal();
ASSERT_FALSE(handler_called1) << "Handler 1 was called after being unregistered";
ASSERT_TRUE(handler_called2) << "Handler 2 was inadvertently unregistered";
}
TEST_F(AutoSignalTest, RaiseASignalWithinASlotTest) {
autowiring::signal<void(void)> signal1;
autowiring::signal<void(void)> signal2;
bool handler_called1 = false;
bool handler_called2 = false;
signal1 += [&] {
handler_called1 = true;
signal2();
};
signal2 += [&] {
handler_called2 = true;
};
// Trivially raise the signal:
signal1();
ASSERT_TRUE(handler_called1) << "Handler 1 was not called on a stack-allocated signal";
ASSERT_TRUE(handler_called2) << "Handler 2 was not called on a stack-allocated signal";
}
TEST_F(AutoSignalTest, NodeRemoval) {
autowiring::signal<void(void)> signal1;
autowiring::signal<void(void)> signal2;
bool handler_called1 = false;
bool handler_called2 = false;
auto registration1 = signal1 += [&] { handler_called1 = true; };
auto registration2 = signal2 += [&] { handler_called2 = true; };
ASSERT_ANY_THROW(signal1 -= registration2) << "Removing a registration from a different signal than it was registered to failed to throw an exception";
signal1 -= registration1;
signal1();
signal2();
ASSERT_FALSE(handler_called1) << "Handler1 was called after being unregistered";
ASSERT_TRUE(handler_called2) << "Handler2 was removed after an invalid -= operation";
}
TEST_F(AutoSignalTest, CallOrdering) {
autowiring::signal<void(void)> signal1;
bool handler_called1 = false;
bool handler_called2 = false;
//handlers are inserted at the end, so this will be called first.
signal1 += [&] { handler_called1 = true; };
signal1 += [&] {
ASSERT_TRUE(handler_called1) << "Handler1 was not called before handler2";
handler_called2 = true;
};
signal1();
ASSERT_TRUE(handler_called1) << "Handler1 was called after being unregistered";
ASSERT_TRUE(handler_called2) << "Handler2 was removed after an invalid -= operation";
}
TEST_F(AutoSignalTest, SelfReferencingCall) {
typedef autowiring::signal<void(int)> signal_t;
signal_t signal1;
bool handler_called1 = false;
int magic_number = 123;
//The main test is just if this thing will compile
registration_t registration1 =
signal1 += [&](registration_t reg, int magic) {
ASSERT_EQ(magic, magic_number);
ASSERT_EQ(registration1, reg);
handler_called1 = true;
};
signal1(magic_number);
ASSERT_TRUE(handler_called1) << "Handler was not called!";
}
TEST_F(AutoSignalTest, SelfModifyingCall) {
typedef autowiring::signal<void(int)> signal_t;
signal_t signal1;
int handler_called1 = 0;
int handler_called2 = 0;
int handler_called3 = 0;
int magic_number = 123;
registration_t registration1 =
signal1 += [&](registration_t reg, int magic) {
ASSERT_EQ(magic, magic_number);
ASSERT_EQ(registration1, reg);
++handler_called1;
signal1 -= reg;
};
auto lambda3 = [&](int magic) {
++handler_called3;
};
registration_t registration2 =
signal1 += [&](registration_t reg, int magic) {
ASSERT_EQ(magic, magic_number);
ASSERT_EQ(registration2, reg);
++handler_called2;
//+= is an append operation, but because when we're traveling the list and we grab the next pointer
//*before* the function get's called, this append won't be picked up until the 2nd pass.
signal1 += std::move(lambda3);
signal1 -= reg;
};
signal1(magic_number);
signal1(magic_number);
ASSERT_EQ(handler_called1, 1) << "Handler was unable to remove itself!";
ASSERT_EQ(handler_called2, 1) << "Specific handler was unable to remove itself";
ASSERT_EQ(handler_called3, 1) << "Handler was unable to append to itself or was called prematurely.";
}
struct VBase
{
int someData;
};
struct Derived :
public virtual VBase
{
typedef autowiring::signal<void(void)> signal_t;
signal_t signal1;
};
TEST_F(AutoSignalTest, ComplexInheritanceSignal) {
int handlerCount = 0;
{
Autowired<Derived> derivedSlot;
// Register a signal handler:
derivedSlot(&Derived::signal1) += [&]{
++handlerCount;
};
// Inject type type after the signal has been registered
AutoRequired<Derived>();
// Now raise the signal, see what happens:
derivedSlot->signal1();
// Verify that the handler got called with the correct value:
ASSERT_TRUE(handlerCount > 0) << "Signal handler was not invoked";
}
}
TEST_F(AutoSignalTest, CanMoveSignal) {
autowiring::signal<void()> signal;
bool hit = false;
{
autowiring::signal<void()> innerSignal;
innerSignal += [&hit] { hit = true; };
autowiring::signal<void()> innerSignal2(std::move(innerSignal));
innerSignal();
ASSERT_FALSE(hit) << "A signal handler was hit on a signal that should have been moved";
innerSignal2();
ASSERT_TRUE(hit) << "Registered listeners were not correctly moved under move construction";
hit = false;
signal = std::move(innerSignal2);
innerSignal2();
ASSERT_FALSE(hit) << "A signal handler was hit on a signal that should have been moved";
}
signal();
ASSERT_TRUE(hit) << "Registered listeners were not correctly moved under move assignment";
}
TEST_F(AutoSignalTest, OneRemovesAll) {
autowiring::signal<void()> sig;
std::vector<registration_t> regs;
auto sentry = std::make_shared<bool>();
int nRun = 0;
{
auto clearall = [&, sentry] {
nRun++;
for (auto& reg : regs)
sig -= reg;
regs.clear();
ASSERT_EQ(3UL, sentry.use_count()) << "A registered lambda was destroyed before all lambdas were run";
};
static_assert(Decompose<decltype(&decltype(clearall)::operator())>::N == 0, "clearall lambda did not decompose correctly");
regs.push_back(sig += clearall);
regs.push_back(sig += clearall);
}
sig();
ASSERT_TRUE(sentry.unique()) << "Some signal handlers were leaked on teardown";
ASSERT_EQ(2, nRun) << "Should have executed all signals even if all signals were unlinked under a callback";
}
TEST_F(AutoSignalTest, SingleUnregisterFromMany) {
autowiring::signal<void()> sig;
size_t ct = 0;
auto l = [&] { ct++; };
sig += l;
sig += l;
auto r = sig += l;
sig += l;
sig += l;
sig += l;
sig -= r;
sig();
ASSERT_EQ(5UL, ct) << "Incorrect number of signals asserted";
}
namespace {
class ObjectA;
class ObjectB;
class ObjectA {
public:
ObjectA(void);
autowiring::signal<void()> x;
Autowired<ObjectB> b;
};
class ObjectB {
public:
ObjectB(void);
autowiring::signal<void()> y;
Autowired<ObjectA> a;
};
ObjectA::ObjectA(void) {
b(&ObjectB::y) += [] {};
}
ObjectB::ObjectB(void) {
a(&ObjectA::x) += [] {};
}
}
TEST_F(AutoSignalTest, CyclicRegistrationUnlink) {
std::weak_ptr<CoreContext> ctxt;
{
AutoCreateContext ctxt;
ctxt->SetUnlinkOnTeardown(true);
AutoRequired<ObjectA> a(ctxt);
AutoRequired<ObjectB> b(ctxt);
}
ASSERT_TRUE(ctxt.expired()) << "A context pointer was linked in an intentional cyclic dependency network";
}
TEST_F(AutoSignalTest, TrivialCountCase) {
autowiring::signal<void(const CountsCopies&)> sig;
sig(CountsCopies{});
ASSERT_EQ(0UL, CountsCopies::nCopies) << "Copy made under trivial forwarding";
}
TEST_F(AutoSignalTest, TrivialMoveCountCase) {
autowiring::signal<void(const std::unique_ptr<int>&)> sig;
// We have this line just to ensure it compiles
sig(std::unique_ptr<int>{ new int });
std::unique_ptr<int> val{ new int };
sig(std::move(val));
ASSERT_NE(nullptr, val) << "Value was moved when it should have been possible to operate on it in-place";
}
TEST_F(AutoSignalTest, TotalMoveCount) {
autowiring::signal<void(const CountsCopies&)> sig;
bool flag = true;
sig += [&] (const CountsCopies&) {
if(flag) {
// This recursive call will require that CountsCopies be moved at least once
sig(CountsCopies{});
flag = false;
}
};
// This should not require that the move ctor is invoked at all
sig(CountsCopies{});
ASSERT_EQ(1UL, CountsCopies::nMoves) << "Too many move constructions were required to invoke the signal";
}
TEST_F(AutoSignalTest, NecessaryMoveCase) {
autowiring::signal<void(const std::unique_ptr<int>&)> sig;
std::unique_ptr<int> mustBeMoved{ new int };
int callCt = 0;
sig += [&] (const std::unique_ptr<int>& cc) {
if (callCt == 0)
sig(std::move(mustBeMoved));
callCt++;
};
std::unique_ptr<int> val{ new int };
sig(std::move(val));
ASSERT_NE(nullptr, val) << "Value was moved when it should not have been moved";
ASSERT_EQ(nullptr, mustBeMoved) << "Value was not moved when it should have been moved";
}
TEST_F(AutoSignalTest, NestedSignalRegistration) {
autowiring::signal<void()> sig;
bool outer = false;
bool inner = false;
sig += [&] {
outer = true;
// This registration will occur too late for the handler to be called, because at the time
// of registration, the signal was already asserted.
sig += [&] {
inner = true;
};
};
sig();
ASSERT_TRUE(outer) << "Outer signal was not asserted even though it was registered in advance";
ASSERT_FALSE(inner) << "Inner signal was asserted even though it should not yet have been registered";
}
TEST_F(AutoSignalTest, NoLingeringListeners) {
autowiring::signal<void()> sig;
auto x = std::make_shared<bool>(false);
auto reg = sig += [x] {};
sig += [&] { sig -= reg; };
// Invoke the signal, which will defer unregistration of the other listener and should
// not cause leakage.
sig();
ASSERT_TRUE(x.unique()) << "Lambda function unregistration was leaked";
}
TEST_F(AutoSignalTest, PathologicalSyncTest) {
autowiring::signal<void()> sig;
bool proceed = true;
// This thread slams the signal in a tight loop
std::thread t([&] {
while (proceed)
sig();
});
// Make sure our thread exits properly
auto r = MakeAtExit([&] {
proceed = false;
t.join();
});
// Need to make sure that the signal is only ever asserted from the thread above:
auto tid = t.get_id();
bool succeeded = false;
size_t nAssertions = 0;
sig += [&, tid] {
succeeded &= (tid == std::this_thread::get_id());
nAssertions++;
};
// Register and unregister the same listener in a tight loop, this should create chaos
// We don't want to run this loop for an indefinite number of iterations because it
// creates a lot of work for the other thread, and the other thread may never be able
// to escape.
for (size_t nLoops = 0; nLoops < 10000; nLoops++) {
auto x = std::make_shared<bool>(false);
auto r = sig += [x] {
*x = x.unique();
};
sig -= r;
ASSERT_FALSE(*x) << "Lambda was invoked after it was destroyed";
}
// Delay until the thread finishes its work:
while (nAssertions < 1000)
std::this_thread::yield();
}
namespace {
class OuterType {
public:
autowiring::signal<void()> sig;
};
class WiresInOuterScope {
public:
WiresInOuterScope(void) {
s_wasHit = false;
s_isConstructed = true;
outer(&OuterType::sig) += [this] { s_wasHit = true; };
}
~WiresInOuterScope(void) {
s_isConstructed = false;
}
Autowired<OuterType> outer;
static bool s_wasHit;
static bool s_isConstructed;
};
bool WiresInOuterScope::s_wasHit = false;
bool WiresInOuterScope::s_isConstructed = false;
}
TEST_F(AutoSignalTest, OuterPostDereference) {
AutoRequired<OuterType> outer;
AutoCreateContext ctxt;
outer->sig += [&] {
// This will cause the outer context to be destroyed, which will cause one
// of the signal handlers to be reset. Unfortunatley, however, we aren't yet
// done hitting all signal handlers, so this statement will cause the very next
// invoked signal--registered in WiresInOuterScope's ctor--to be called even
// though its context is gone.
// This is the desired behavior.
ctxt.reset();
};
ctxt->Inject<WiresInOuterScope>();
// This should trigger an exception:
outer->sig();
ASSERT_FALSE(WiresInOuterScope::s_isConstructed) << "An object registered with a signal handle was unexpectedly leaked";
ASSERT_FALSE(WiresInOuterScope::s_wasHit) << "Signal handler was invoked even though its enclosing object should have been destroyed";
}
namespace {
class WithSignal {
public:
autowiring::signal<void()> sig;
};
}
TEST_F(AutoSignalTest, ReallocationCheck) {
AutoCurrentContext ctxt;
size_t hitCount = 0;
std::unique_ptr<Autowired<WithSignal>> contrived{
new Autowired<WithSignal>{}
};
(*contrived)(&WithSignal::sig) += [&] {
hitCount++;
// Delete the Autowired field while the signal handler is being called. This is intended to simulate
// a legal possible multithreaded scenario where a signal handler is being asserted at about the same
// time as an Autowired field is being destroyed.
contrived.reset();
Autowired<WithSignal> ws;
ws->sig();
};
ctxt->Inject<WithSignal>();
Autowired<WithSignal> sig3;
sig3->sig();
ASSERT_EQ(1UL, hitCount) << "A signal handler was hit even though the base Autowired field was destroyed";
}
TEST_F(AutoSignalTest, IsExecuting) {
autowiring::signal<void()> sig;
std::shared_ptr<bool> checksSig(
new bool{ false },
[&sig](bool* ptr) {
delete ptr;
ASSERT_FALSE(sig.is_executing()) << "Signal handler should not be marked as executing while in unregistration";
}
);
sig += [&, checksSig] {
ASSERT_TRUE(sig.is_executing()) << "Signal was not properly marked as executing inside a signal handler";
if (!*checksSig) {
*checksSig = true;
sig();
}
};
ASSERT_FALSE(sig.is_executing()) << "Signal was incorrectly marked as executing even though nothing is happening";
}
TEST_F(AutoSignalTest, NoLeaks) {
auto v = std::make_shared<bool>(false);
{
autowiring::signal<void()> x;
x += [v] {};
}
ASSERT_TRUE(v.unique()) << "Signal did not destroy all attached lambdas on its destruction";
}