Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "util/signal.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace util { |
| 28 | namespace signal { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(UtilSignal) |
| 32 | |
| 33 | class SignalOwner0 |
| 34 | { |
| 35 | public: |
| 36 | Signal<SignalOwner0> sig; |
| 37 | |
| 38 | public: |
| 39 | DECLARE_SIGNAL_EMIT(sig) |
| 40 | |
| 41 | bool |
| 42 | isSigEmpty() |
| 43 | { |
| 44 | return sig.isEmpty(); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(ZeroSlot) |
| 49 | { |
| 50 | SignalOwner0 so; |
| 51 | BOOST_CHECK_NO_THROW(so.emitSignal(sig)); |
| 52 | } |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(TwoListeners) |
| 55 | { |
| 56 | SignalOwner0 so; |
| 57 | |
| 58 | int hit1 = 0, hit2 = 0; |
| 59 | so.sig.connect([&hit1] { ++hit1; }); |
| 60 | so.sig.connect([&hit2] { ++hit2; }); |
| 61 | |
| 62 | so.emitSignal(sig); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(hit1, 1); |
| 65 | BOOST_CHECK_EQUAL(hit2, 1); |
| 66 | } |
| 67 | |
Junxiao Shi | 018e30d | 2014-12-25 19:42:35 -0700 | [diff] [blame] | 68 | class SignalOwner1 |
| 69 | { |
| 70 | public: |
| 71 | Signal<SignalOwner1, int> sig; |
| 72 | |
| 73 | protected: |
| 74 | DECLARE_SIGNAL_EMIT(sig) |
| 75 | }; |
| 76 | |
| 77 | class SignalEmitter1 : public SignalOwner1 |
| 78 | { |
| 79 | public: |
| 80 | void |
| 81 | emitTestSignal() |
| 82 | { |
| 83 | this->emitSignal(sig, 8106); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(OneArgument) |
| 88 | { |
| 89 | SignalEmitter1 se; |
| 90 | |
| 91 | int hit = 0; |
| 92 | se.sig.connect([&hit] (int a) { |
| 93 | ++hit; |
| 94 | BOOST_CHECK_EQUAL(a, 8106); |
| 95 | }); |
| 96 | se.emitTestSignal(); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(hit, 1); |
| 99 | } |
| 100 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(TwoArguments) |
| 102 | { |
| 103 | Signal<std::remove_pointer<decltype(this)>::type, int, int> sig; |
| 104 | |
| 105 | int hit = 0; |
| 106 | sig.connect([&hit] (int a, int b) { |
| 107 | ++hit; |
| 108 | BOOST_CHECK_EQUAL(a, 21); |
| 109 | BOOST_CHECK_EQUAL(b, 22); |
| 110 | }); |
| 111 | sig(21, 22); |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(hit, 1); |
| 114 | } |
| 115 | |
| 116 | class RefObject |
| 117 | { |
| 118 | public: |
| 119 | RefObject() |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | RefObject(const RefObject& other) |
| 124 | { |
| 125 | ++s_copyCount; |
| 126 | } |
| 127 | |
| 128 | public: |
| 129 | static int s_copyCount; |
| 130 | }; |
| 131 | int RefObject::s_copyCount = 0; |
| 132 | |
| 133 | // Signal passes arguments by reference, |
| 134 | // but it also allows a handler that accept arguments by value |
| 135 | BOOST_AUTO_TEST_CASE(HandlerByVal) |
| 136 | { |
| 137 | RefObject refObject; |
| 138 | RefObject::s_copyCount = 0; |
| 139 | |
| 140 | Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig; |
| 141 | sig.connect([] (RefObject ro) {}); |
| 142 | sig(refObject); |
| 143 | |
| 144 | BOOST_CHECK_EQUAL(RefObject::s_copyCount, 1); |
| 145 | } |
| 146 | |
| 147 | // Signal passes arguments by reference, and no copying |
| 148 | // is necessary when handler accepts arguments by reference |
| 149 | BOOST_AUTO_TEST_CASE(HandlerByRef) |
| 150 | { |
| 151 | RefObject refObject; |
| 152 | RefObject::s_copyCount = 0; |
| 153 | |
| 154 | Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig; |
| 155 | sig.connect([] (const RefObject& ro) {}); |
| 156 | sig(refObject); |
| 157 | |
| 158 | BOOST_CHECK_EQUAL(RefObject::s_copyCount, 0); |
| 159 | } |
| 160 | |
| 161 | BOOST_AUTO_TEST_CASE(ManualDisconnect) |
| 162 | { |
| 163 | SignalOwner0 so; |
| 164 | |
| 165 | int hit = 0; |
| 166 | Connection c1 = so.sig.connect([&hit] { ++hit; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 168 | |
| 169 | so.emitSignal(sig); |
| 170 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 171 | |
| 172 | Connection c2 = c1; // make a copy |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 173 | BOOST_CHECK_EQUAL(c2.isConnected(), true); |
| 174 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 175 | c2.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(c2.isConnected(), false); |
| 177 | BOOST_CHECK_EQUAL(c1.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 178 | so.emitSignal(sig); |
| 179 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 180 | |
| 181 | BOOST_CHECK_NO_THROW(c2.disconnect()); |
| 182 | BOOST_CHECK_NO_THROW(c1.disconnect()); |
| 183 | } |
| 184 | |
| 185 | BOOST_AUTO_TEST_CASE(ManualDisconnectDestructed) |
| 186 | { |
| 187 | unique_ptr<SignalOwner0> so(new SignalOwner0()); |
| 188 | |
| 189 | int hit = 0; |
| 190 | Connection connection = so->sig.connect([&hit] { ++hit; }); |
| 191 | |
| 192 | so->emitSignal(sig); |
| 193 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 194 | |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(connection.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 196 | so.reset(); // destruct EventEmitter |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 198 | BOOST_CHECK_NO_THROW(connection.disconnect()); |
| 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(AutoDisconnect) |
| 202 | { |
| 203 | SignalOwner0 so; |
| 204 | |
| 205 | int hit = 0; |
| 206 | { |
| 207 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 208 | |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 210 | so.emitSignal(sig); |
| 211 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 212 | |
| 213 | // sc goes out of scope, disconnecting |
| 214 | } |
| 215 | |
| 216 | so.emitSignal(sig); |
| 217 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(AutoDisconnectAssign) |
| 221 | { |
| 222 | SignalOwner0 so; |
| 223 | |
| 224 | int hit1 = 0, hit2 = 0; |
| 225 | ScopedConnection sc = so.sig.connect([&hit1] { ++hit1; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 227 | |
| 228 | so.emitSignal(sig); |
| 229 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 called |
| 230 | |
| 231 | sc = so.sig.connect([&hit2] { ++hit2; }); // handler1 is disconnected |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 232 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 233 | |
| 234 | so.emitSignal(sig); |
| 235 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 not called |
| 236 | BOOST_CHECK_EQUAL(hit2, 1); // handler2 called |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_CASE(AutoDisconnectAssignSame) |
| 240 | { |
| 241 | SignalOwner0 so; |
| 242 | |
| 243 | int hit = 0; |
| 244 | Connection c1 = so.sig.connect([&hit] { ++hit; }); |
| 245 | |
| 246 | ScopedConnection sc(c1); |
| 247 | so.emitSignal(sig); |
| 248 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 250 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 251 | |
| 252 | sc = c1; // assign same connection |
| 253 | so.emitSignal(sig); |
| 254 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 256 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 257 | |
| 258 | Connection c2 = c1; |
| 259 | sc = c2; // assign a copy of same connection |
| 260 | so.emitSignal(sig); |
| 261 | BOOST_CHECK_EQUAL(hit, 3); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 263 | BOOST_CHECK_EQUAL(c2.isConnected(), true); |
| 264 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE(AutoDisconnectRelease) |
| 268 | { |
| 269 | SignalOwner0 so; |
| 270 | |
| 271 | int hit = 0; |
| 272 | { |
| 273 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 274 | |
| 275 | so.emitSignal(sig); |
| 276 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 278 | |
| 279 | sc.release(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(sc.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 281 | // sc goes out of scope, but not disconnecting |
| 282 | } |
| 283 | |
| 284 | so.emitSignal(sig); |
| 285 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
| 286 | } |
| 287 | |
| 288 | BOOST_AUTO_TEST_CASE(AutoDisconnectMove) |
| 289 | { |
| 290 | SignalOwner0 so; |
| 291 | unique_ptr<ScopedConnection> sc2; |
| 292 | |
| 293 | int hit = 0; |
| 294 | { |
| 295 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 296 | |
| 297 | so.emitSignal(sig); |
| 298 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 300 | |
| 301 | sc2.reset(new ScopedConnection(std::move(sc))); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL(sc.isConnected(), false); |
| 303 | BOOST_CHECK_EQUAL(sc2->isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 304 | |
| 305 | // sc goes out of scope, but not disconnecting |
| 306 | } |
| 307 | |
| 308 | so.emitSignal(sig); |
| 309 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
| 310 | } |
| 311 | |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 312 | BOOST_AUTO_TEST_CASE(ConnectSingleShot) |
| 313 | { |
| 314 | SignalOwner0 so; |
| 315 | |
| 316 | int hit = 0; |
| 317 | so.sig.connectSingleShot([&hit] { ++hit; }); |
| 318 | |
| 319 | so.emitSignal(sig); |
| 320 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 321 | |
| 322 | so.emitSignal(sig); |
| 323 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(ConnectSingleShotDisconnected) |
| 327 | { |
| 328 | SignalOwner0 so; |
| 329 | |
| 330 | int hit = 0; |
| 331 | Connection conn = so.sig.connectSingleShot([&hit] { ++hit; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 332 | BOOST_CHECK_EQUAL(conn.isConnected(), true); |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 333 | conn.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(conn.isConnected(), false); |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 335 | |
| 336 | so.emitSignal(sig); |
| 337 | BOOST_CHECK_EQUAL(hit, 0); // handler not called |
| 338 | } |
| 339 | |
| 340 | BOOST_AUTO_TEST_CASE(ConnectSingleShot1) |
| 341 | { |
| 342 | SignalEmitter1 se; |
| 343 | |
| 344 | int hit = 0; |
| 345 | se.sig.connectSingleShot([&hit] (int) { ++hit; }); |
| 346 | |
| 347 | se.emitTestSignal(); |
| 348 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 349 | |
| 350 | se.emitTestSignal(); |
| 351 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 352 | } |
| 353 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 354 | BOOST_AUTO_TEST_CASE(ConnectInHandler) |
| 355 | { |
| 356 | SignalOwner0 so; |
| 357 | |
| 358 | int hit1 = 0, hit2 = 0; bool hasHandler2 = false; |
| 359 | so.sig.connect([&] { |
| 360 | ++hit1; |
| 361 | if (!hasHandler2) { |
| 362 | so.sig.connect([&] { ++hit2; }); |
| 363 | hasHandler2 = true; |
| 364 | } |
| 365 | }); |
| 366 | |
| 367 | so.emitSignal(sig); |
| 368 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 called |
| 369 | BOOST_CHECK_EQUAL(hit2, 0); // handler2 not called |
| 370 | |
| 371 | // new subscription takes effect |
| 372 | so.emitSignal(sig); |
| 373 | BOOST_CHECK_EQUAL(hit1, 2); // handler1 called |
| 374 | BOOST_CHECK_EQUAL(hit2, 1); // handler2 called |
| 375 | } |
| 376 | |
| 377 | BOOST_AUTO_TEST_CASE(DisconnectSelfInHandler) |
| 378 | { |
| 379 | SignalOwner0 so; |
| 380 | |
| 381 | int hit = 0; |
| 382 | Connection connection; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 383 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 4427345 | 2015-02-15 20:37:21 -0700 | [diff] [blame^] | 384 | connection = so.sig.connect(bind([] (int& hit, SignalOwner0& so, Connection& connection) { |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 385 | ++hit; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 386 | BOOST_CHECK_EQUAL(connection.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 387 | connection.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 388 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(so.isSigEmpty(), false); // disconnecting hasn't taken effect |
Junxiao Shi | 4427345 | 2015-02-15 20:37:21 -0700 | [diff] [blame^] | 390 | }, ref(hit), ref(so), ref(connection))); |
| 391 | // Bug 2302, 2523: variables needs to be bound to the handler; |
Junxiao Shi | 2cec707 | 2014-12-19 19:37:40 -0700 | [diff] [blame] | 392 | // lambda capture won't work because closure would be destructed at .disconnect |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 393 | |
| 394 | so.emitSignal(sig); |
| 395 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 396 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 397 | |
| 398 | // disconnecting takes effect |
| 399 | BOOST_CHECK_EQUAL(so.isSigEmpty(), true); |
| 400 | so.emitSignal(sig); |
| 401 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 402 | } |
| 403 | |
| 404 | BOOST_AUTO_TEST_CASE(ThrowInHandler) |
| 405 | { |
| 406 | SignalOwner0 so; |
| 407 | |
| 408 | struct HandlerError |
| 409 | { |
| 410 | }; |
| 411 | |
| 412 | int hit = 0; |
| 413 | so.sig.connect([&] { |
| 414 | ++hit; |
| 415 | throw HandlerError(); |
| 416 | }); |
| 417 | |
| 418 | BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError); |
| 419 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 420 | |
| 421 | BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError); |
| 422 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
| 423 | } |
| 424 | |
| 425 | BOOST_AUTO_TEST_CASE(DestructInHandler) |
| 426 | { |
| 427 | unique_ptr<SignalOwner0> so(new SignalOwner0()); |
| 428 | |
| 429 | int hit = 0; |
| 430 | so->sig.connect([&] { |
| 431 | ++hit; |
| 432 | so.reset(); |
| 433 | }); |
| 434 | |
| 435 | BOOST_CHECK_NO_THROW(so->emitSignal(sig)); |
| 436 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 437 | BOOST_CHECK(so == nullptr); |
| 438 | } |
| 439 | |
| 440 | BOOST_AUTO_TEST_SUITE_END() |
| 441 | |
| 442 | } // namespace tests |
| 443 | } // namespace signal |
| 444 | } // namespace util |
| 445 | } // namespace ndn |