blob: eb02324ca7d83555b13dc7f38bd9974151997f5f [file] [log] [blame]
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 Regents of the University of California.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070020 */
21
22#include "util/signal.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace util {
28namespace signal {
29namespace tests {
30
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Util)
32BOOST_AUTO_TEST_SUITE(TestSignal)
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070033
34class SignalOwner0
35{
36public:
37 Signal<SignalOwner0> sig;
38
39public:
40 DECLARE_SIGNAL_EMIT(sig)
41
42 bool
43 isSigEmpty()
44 {
45 return sig.isEmpty();
46 }
47};
48
49BOOST_AUTO_TEST_CASE(ZeroSlot)
50{
51 SignalOwner0 so;
52 BOOST_CHECK_NO_THROW(so.emitSignal(sig));
53}
54
55BOOST_AUTO_TEST_CASE(TwoListeners)
56{
57 SignalOwner0 so;
58
59 int hit1 = 0, hit2 = 0;
60 so.sig.connect([&hit1] { ++hit1; });
61 so.sig.connect([&hit2] { ++hit2; });
62
63 so.emitSignal(sig);
64
65 BOOST_CHECK_EQUAL(hit1, 1);
66 BOOST_CHECK_EQUAL(hit2, 1);
67}
68
Junxiao Shi018e30d2014-12-25 19:42:35 -070069class SignalOwner1
70{
71public:
72 Signal<SignalOwner1, int> sig;
73
74protected:
75 DECLARE_SIGNAL_EMIT(sig)
76};
77
78class SignalEmitter1 : public SignalOwner1
79{
80public:
81 void
82 emitTestSignal()
83 {
84 this->emitSignal(sig, 8106);
85 }
86};
87
88BOOST_AUTO_TEST_CASE(OneArgument)
89{
90 SignalEmitter1 se;
91
92 int hit = 0;
93 se.sig.connect([&hit] (int a) {
94 ++hit;
95 BOOST_CHECK_EQUAL(a, 8106);
96 });
97 se.emitTestSignal();
98
99 BOOST_CHECK_EQUAL(hit, 1);
100}
101
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700102BOOST_AUTO_TEST_CASE(TwoArguments)
103{
104 Signal<std::remove_pointer<decltype(this)>::type, int, int> sig;
105
106 int hit = 0;
107 sig.connect([&hit] (int a, int b) {
108 ++hit;
109 BOOST_CHECK_EQUAL(a, 21);
110 BOOST_CHECK_EQUAL(b, 22);
111 });
112 sig(21, 22);
113
114 BOOST_CHECK_EQUAL(hit, 1);
115}
116
117class RefObject
118{
119public:
120 RefObject()
121 {
122 }
123
124 RefObject(const RefObject& other)
125 {
126 ++s_copyCount;
127 }
128
129public:
130 static int s_copyCount;
131};
132int RefObject::s_copyCount = 0;
133
134// Signal passes arguments by reference,
135// but it also allows a handler that accept arguments by value
136BOOST_AUTO_TEST_CASE(HandlerByVal)
137{
138 RefObject refObject;
139 RefObject::s_copyCount = 0;
140
141 Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig;
142 sig.connect([] (RefObject ro) {});
143 sig(refObject);
144
145 BOOST_CHECK_EQUAL(RefObject::s_copyCount, 1);
146}
147
148// Signal passes arguments by reference, and no copying
149// is necessary when handler accepts arguments by reference
150BOOST_AUTO_TEST_CASE(HandlerByRef)
151{
152 RefObject refObject;
153 RefObject::s_copyCount = 0;
154
155 Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig;
156 sig.connect([] (const RefObject& ro) {});
157 sig(refObject);
158
159 BOOST_CHECK_EQUAL(RefObject::s_copyCount, 0);
160}
161
162BOOST_AUTO_TEST_CASE(ManualDisconnect)
163{
164 SignalOwner0 so;
165
166 int hit = 0;
167 Connection c1 = so.sig.connect([&hit] { ++hit; });
Chengyu Fanf46482c2015-02-03 16:55:53 -0700168 BOOST_CHECK_EQUAL(c1.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700169
170 so.emitSignal(sig);
171 BOOST_CHECK_EQUAL(hit, 1); // handler called
172
173 Connection c2 = c1; // make a copy
Chengyu Fanf46482c2015-02-03 16:55:53 -0700174 BOOST_CHECK_EQUAL(c2.isConnected(), true);
175 BOOST_CHECK_EQUAL(c1.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700176 c2.disconnect();
Chengyu Fanf46482c2015-02-03 16:55:53 -0700177 BOOST_CHECK_EQUAL(c2.isConnected(), false);
178 BOOST_CHECK_EQUAL(c1.isConnected(), false);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700179 so.emitSignal(sig);
180 BOOST_CHECK_EQUAL(hit, 1); // handler not called
181
182 BOOST_CHECK_NO_THROW(c2.disconnect());
183 BOOST_CHECK_NO_THROW(c1.disconnect());
184}
185
186BOOST_AUTO_TEST_CASE(ManualDisconnectDestructed)
187{
Davide Pesavento409cc202015-09-19 14:13:16 +0200188 auto so = make_unique<SignalOwner0>();
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700189
190 int hit = 0;
191 Connection connection = so->sig.connect([&hit] { ++hit; });
192
193 so->emitSignal(sig);
194 BOOST_CHECK_EQUAL(hit, 1); // handler called
195
Chengyu Fanf46482c2015-02-03 16:55:53 -0700196 BOOST_CHECK_EQUAL(connection.isConnected(), true);
Junxiao Shibbb24352015-02-28 21:23:51 -0700197 so.reset(); // destruct Signal
Chengyu Fanf46482c2015-02-03 16:55:53 -0700198 BOOST_CHECK_EQUAL(connection.isConnected(), false);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700199 BOOST_CHECK_NO_THROW(connection.disconnect());
200}
201
202BOOST_AUTO_TEST_CASE(AutoDisconnect)
203{
204 SignalOwner0 so;
205
206 int hit = 0;
207 {
208 ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
209
Chengyu Fanf46482c2015-02-03 16:55:53 -0700210 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700211 so.emitSignal(sig);
212 BOOST_CHECK_EQUAL(hit, 1); // handler called
213
214 // sc goes out of scope, disconnecting
215 }
216
217 so.emitSignal(sig);
218 BOOST_CHECK_EQUAL(hit, 1); // handler not called
219}
220
221BOOST_AUTO_TEST_CASE(AutoDisconnectAssign)
222{
223 SignalOwner0 so;
224
225 int hit1 = 0, hit2 = 0;
226 ScopedConnection sc = so.sig.connect([&hit1] { ++hit1; });
Chengyu Fanf46482c2015-02-03 16:55:53 -0700227 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700228
229 so.emitSignal(sig);
230 BOOST_CHECK_EQUAL(hit1, 1); // handler1 called
231
232 sc = so.sig.connect([&hit2] { ++hit2; }); // handler1 is disconnected
Chengyu Fanf46482c2015-02-03 16:55:53 -0700233 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700234
235 so.emitSignal(sig);
236 BOOST_CHECK_EQUAL(hit1, 1); // handler1 not called
237 BOOST_CHECK_EQUAL(hit2, 1); // handler2 called
238}
239
240BOOST_AUTO_TEST_CASE(AutoDisconnectAssignSame)
241{
242 SignalOwner0 so;
243
244 int hit = 0;
245 Connection c1 = so.sig.connect([&hit] { ++hit; });
246
247 ScopedConnection sc(c1);
248 so.emitSignal(sig);
249 BOOST_CHECK_EQUAL(hit, 1); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700250 BOOST_CHECK_EQUAL(c1.isConnected(), true);
251 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700252
253 sc = c1; // assign same connection
254 so.emitSignal(sig);
255 BOOST_CHECK_EQUAL(hit, 2); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700256 BOOST_CHECK_EQUAL(c1.isConnected(), true);
257 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700258
259 Connection c2 = c1;
260 sc = c2; // assign a copy of same connection
261 so.emitSignal(sig);
262 BOOST_CHECK_EQUAL(hit, 3); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700263 BOOST_CHECK_EQUAL(c1.isConnected(), true);
264 BOOST_CHECK_EQUAL(c2.isConnected(), true);
265 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700266}
267
268BOOST_AUTO_TEST_CASE(AutoDisconnectRelease)
269{
270 SignalOwner0 so;
271
272 int hit = 0;
273 {
274 ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
275
276 so.emitSignal(sig);
277 BOOST_CHECK_EQUAL(hit, 1); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700278 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700279
280 sc.release();
Chengyu Fanf46482c2015-02-03 16:55:53 -0700281 BOOST_CHECK_EQUAL(sc.isConnected(), false);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700282 // sc goes out of scope, but not disconnecting
283 }
284
285 so.emitSignal(sig);
286 BOOST_CHECK_EQUAL(hit, 2); // handler called
287}
288
289BOOST_AUTO_TEST_CASE(AutoDisconnectMove)
290{
291 SignalOwner0 so;
292 unique_ptr<ScopedConnection> sc2;
293
294 int hit = 0;
295 {
296 ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
297
298 so.emitSignal(sig);
299 BOOST_CHECK_EQUAL(hit, 1); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700300 BOOST_CHECK_EQUAL(sc.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700301
302 sc2.reset(new ScopedConnection(std::move(sc)));
Chengyu Fanf46482c2015-02-03 16:55:53 -0700303 BOOST_CHECK_EQUAL(sc.isConnected(), false);
304 BOOST_CHECK_EQUAL(sc2->isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700305
306 // sc goes out of scope, but not disconnecting
307 }
308
309 so.emitSignal(sig);
310 BOOST_CHECK_EQUAL(hit, 2); // handler called
311}
312
Junxiao Shiecc57b52015-01-01 10:47:08 -0700313BOOST_AUTO_TEST_CASE(ConnectSingleShot)
314{
315 SignalOwner0 so;
316
317 int hit = 0;
318 so.sig.connectSingleShot([&hit] { ++hit; });
319
320 so.emitSignal(sig);
321 BOOST_CHECK_EQUAL(hit, 1); // handler called
322
323 so.emitSignal(sig);
324 BOOST_CHECK_EQUAL(hit, 1); // handler not called
325}
326
327BOOST_AUTO_TEST_CASE(ConnectSingleShotDisconnected)
328{
329 SignalOwner0 so;
330
331 int hit = 0;
332 Connection conn = so.sig.connectSingleShot([&hit] { ++hit; });
Chengyu Fanf46482c2015-02-03 16:55:53 -0700333 BOOST_CHECK_EQUAL(conn.isConnected(), true);
Junxiao Shiecc57b52015-01-01 10:47:08 -0700334 conn.disconnect();
Chengyu Fanf46482c2015-02-03 16:55:53 -0700335 BOOST_CHECK_EQUAL(conn.isConnected(), false);
Junxiao Shiecc57b52015-01-01 10:47:08 -0700336
337 so.emitSignal(sig);
338 BOOST_CHECK_EQUAL(hit, 0); // handler not called
339}
340
341BOOST_AUTO_TEST_CASE(ConnectSingleShot1)
342{
343 SignalEmitter1 se;
344
345 int hit = 0;
346 se.sig.connectSingleShot([&hit] (int) { ++hit; });
347
348 se.emitTestSignal();
349 BOOST_CHECK_EQUAL(hit, 1); // handler called
350
351 se.emitTestSignal();
352 BOOST_CHECK_EQUAL(hit, 1); // handler not called
353}
354
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700355BOOST_AUTO_TEST_CASE(ConnectInHandler)
356{
357 SignalOwner0 so;
358
359 int hit1 = 0, hit2 = 0; bool hasHandler2 = false;
360 so.sig.connect([&] {
361 ++hit1;
362 if (!hasHandler2) {
363 so.sig.connect([&] { ++hit2; });
364 hasHandler2 = true;
365 }
366 });
367
368 so.emitSignal(sig);
369 BOOST_CHECK_EQUAL(hit1, 1); // handler1 called
370 BOOST_CHECK_EQUAL(hit2, 0); // handler2 not called
371
372 // new subscription takes effect
373 so.emitSignal(sig);
374 BOOST_CHECK_EQUAL(hit1, 2); // handler1 called
375 BOOST_CHECK_EQUAL(hit2, 1); // handler2 called
376}
377
378BOOST_AUTO_TEST_CASE(DisconnectSelfInHandler)
379{
380 SignalOwner0 so;
381
382 int hit = 0;
383 Connection connection;
Chengyu Fanf46482c2015-02-03 16:55:53 -0700384 BOOST_CHECK_EQUAL(connection.isConnected(), false);
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200385 connection = so.sig.connect([&so, &connection, &hit] {
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700386 ++hit;
Chengyu Fanf46482c2015-02-03 16:55:53 -0700387 BOOST_CHECK_EQUAL(connection.isConnected(), true);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700388 connection.disconnect();
Chengyu Fanf46482c2015-02-03 16:55:53 -0700389 BOOST_CHECK_EQUAL(connection.isConnected(), false);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700390 BOOST_CHECK_EQUAL(so.isSigEmpty(), false); // disconnecting hasn't taken effect
Davide Pesaventobe7804f2015-10-23 00:53:47 +0200391 });
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700392
393 so.emitSignal(sig);
394 BOOST_CHECK_EQUAL(hit, 1); // handler called
Chengyu Fanf46482c2015-02-03 16:55:53 -0700395 BOOST_CHECK_EQUAL(connection.isConnected(), false);
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700396
397 // disconnecting takes effect
398 BOOST_CHECK_EQUAL(so.isSigEmpty(), true);
399 so.emitSignal(sig);
400 BOOST_CHECK_EQUAL(hit, 1); // handler not called
401}
402
403BOOST_AUTO_TEST_CASE(ThrowInHandler)
404{
405 SignalOwner0 so;
406
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700407 struct HandlerError : public std::exception
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700408 {
409 };
410
411 int hit = 0;
412 so.sig.connect([&] {
413 ++hit;
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700414 BOOST_THROW_EXCEPTION(HandlerError());
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700415 });
416
417 BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError);
418 BOOST_CHECK_EQUAL(hit, 1); // handler called
419
420 BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError);
421 BOOST_CHECK_EQUAL(hit, 2); // handler called
422}
423
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100424BOOST_AUTO_TEST_SUITE_END() // TestSignal
425BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi8d71fdb2014-12-07 21:55:19 -0700426
427} // namespace tests
428} // namespace signal
429} // namespace util
430} // namespace ndn