Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2019 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/signal.hpp" |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace util { |
| 28 | namespace signal { |
| 29 | namespace tests { |
| 30 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Util) |
| 32 | BOOST_AUTO_TEST_SUITE(TestSignal) |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 33 | |
| 34 | class SignalOwner0 |
| 35 | { |
| 36 | public: |
| 37 | Signal<SignalOwner0> sig; |
| 38 | |
| 39 | public: |
| 40 | DECLARE_SIGNAL_EMIT(sig) |
| 41 | |
| 42 | bool |
| 43 | isSigEmpty() |
| 44 | { |
| 45 | return sig.isEmpty(); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | BOOST_AUTO_TEST_CASE(ZeroSlot) |
| 50 | { |
| 51 | SignalOwner0 so; |
| 52 | BOOST_CHECK_NO_THROW(so.emitSignal(sig)); |
| 53 | } |
| 54 | |
| 55 | BOOST_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 Shi | 018e30d | 2014-12-25 19:42:35 -0700 | [diff] [blame] | 69 | class SignalOwner1 |
| 70 | { |
| 71 | public: |
| 72 | Signal<SignalOwner1, int> sig; |
| 73 | |
| 74 | protected: |
| 75 | DECLARE_SIGNAL_EMIT(sig) |
| 76 | }; |
| 77 | |
| 78 | class SignalEmitter1 : public SignalOwner1 |
| 79 | { |
| 80 | public: |
| 81 | void |
| 82 | emitTestSignal() |
| 83 | { |
| 84 | this->emitSignal(sig, 8106); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | BOOST_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 Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(TwoArguments) |
| 103 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 104 | Signal<std::remove_pointer_t<decltype(this)>, int, int> sig; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 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 | |
| 117 | class RefObject |
| 118 | { |
| 119 | public: |
| 120 | RefObject() |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | RefObject(const RefObject& other) |
| 125 | { |
| 126 | ++s_copyCount; |
| 127 | } |
| 128 | |
| 129 | public: |
| 130 | static int s_copyCount; |
| 131 | }; |
| 132 | int RefObject::s_copyCount = 0; |
| 133 | |
| 134 | // Signal passes arguments by reference, |
| 135 | // but it also allows a handler that accept arguments by value |
| 136 | BOOST_AUTO_TEST_CASE(HandlerByVal) |
| 137 | { |
| 138 | RefObject refObject; |
| 139 | RefObject::s_copyCount = 0; |
| 140 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 141 | Signal<std::remove_pointer_t<decltype(this)>, RefObject> sig; |
| 142 | sig.connect([] (RefObject) {}); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 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 |
| 150 | BOOST_AUTO_TEST_CASE(HandlerByRef) |
| 151 | { |
| 152 | RefObject refObject; |
| 153 | RefObject::s_copyCount = 0; |
| 154 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 155 | Signal<std::remove_pointer_t<decltype(this)>, RefObject> sig; |
| 156 | sig.connect([] (const RefObject&) {}); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 157 | sig(refObject); |
| 158 | |
| 159 | BOOST_CHECK_EQUAL(RefObject::s_copyCount, 0); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(ManualDisconnect) |
| 163 | { |
| 164 | SignalOwner0 so; |
| 165 | |
| 166 | int hit = 0; |
| 167 | Connection c1 = so.sig.connect([&hit] { ++hit; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 169 | |
| 170 | so.emitSignal(sig); |
| 171 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 172 | |
| 173 | Connection c2 = c1; // make a copy |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(c2.isConnected(), true); |
| 175 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 176 | c2.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(c2.isConnected(), false); |
| 178 | BOOST_CHECK_EQUAL(c1.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 179 | 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 | |
| 186 | BOOST_AUTO_TEST_CASE(ManualDisconnectDestructed) |
| 187 | { |
Davide Pesavento | 409cc20 | 2015-09-19 14:13:16 +0200 | [diff] [blame] | 188 | auto so = make_unique<SignalOwner0>(); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 189 | |
| 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 Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(connection.isConnected(), true); |
Junxiao Shi | bbb2435 | 2015-02-28 21:23:51 -0700 | [diff] [blame] | 197 | so.reset(); // destruct Signal |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 199 | BOOST_CHECK_NO_THROW(connection.disconnect()); |
| 200 | } |
| 201 | |
| 202 | BOOST_AUTO_TEST_CASE(AutoDisconnect) |
| 203 | { |
| 204 | SignalOwner0 so; |
| 205 | |
| 206 | int hit = 0; |
| 207 | { |
| 208 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 209 | |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 211 | 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 | |
| 221 | BOOST_AUTO_TEST_CASE(AutoDisconnectAssign) |
| 222 | { |
| 223 | SignalOwner0 so; |
| 224 | |
| 225 | int hit1 = 0, hit2 = 0; |
| 226 | ScopedConnection sc = so.sig.connect([&hit1] { ++hit1; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 227 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 228 | |
| 229 | so.emitSignal(sig); |
| 230 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 called |
| 231 | |
| 232 | sc = so.sig.connect([&hit2] { ++hit2; }); // handler1 is disconnected |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 233 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 234 | |
| 235 | so.emitSignal(sig); |
| 236 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 not called |
| 237 | BOOST_CHECK_EQUAL(hit2, 1); // handler2 called |
| 238 | } |
| 239 | |
| 240 | BOOST_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 Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 251 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 252 | |
| 253 | sc = c1; // assign same connection |
| 254 | so.emitSignal(sig); |
| 255 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 257 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 258 | |
| 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 Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(c1.isConnected(), true); |
| 264 | BOOST_CHECK_EQUAL(c2.isConnected(), true); |
| 265 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | BOOST_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 Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 279 | |
| 280 | sc.release(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 281 | BOOST_CHECK_EQUAL(sc.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 282 | // sc goes out of scope, but not disconnecting |
| 283 | } |
| 284 | |
| 285 | so.emitSignal(sig); |
| 286 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
| 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_CASE(AutoDisconnectMove) |
| 290 | { |
| 291 | SignalOwner0 so; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 292 | int hit = 0; |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 293 | |
| 294 | unique_ptr<ScopedConnection> sc2; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 295 | { |
| 296 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 297 | |
| 298 | so.emitSignal(sig); |
| 299 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 301 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 302 | sc2 = make_unique<ScopedConnection>(std::move(sc)); // move constructor |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 303 | BOOST_CHECK_EQUAL(sc.isConnected(), false); |
| 304 | BOOST_CHECK_EQUAL(sc2->isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 305 | |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 306 | // sc goes out of scope, but without disconnecting |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | so.emitSignal(sig); |
| 310 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
Davide Pesavento | 3a3e188 | 2018-07-17 14:49:15 -0400 | [diff] [blame] | 311 | sc2.reset(); |
| 312 | |
| 313 | ScopedConnection sc3; |
| 314 | { |
| 315 | ScopedConnection sc = so.sig.connect([&hit] { ++hit; }); |
| 316 | |
| 317 | so.emitSignal(sig); |
| 318 | BOOST_CHECK_EQUAL(hit, 3); // handler called |
| 319 | BOOST_CHECK_EQUAL(sc.isConnected(), true); |
| 320 | BOOST_CHECK_EQUAL(sc3.isConnected(), false); |
| 321 | |
| 322 | sc3 = std::move(sc); // move assignment |
| 323 | BOOST_CHECK_EQUAL(sc.isConnected(), false); |
| 324 | BOOST_CHECK_EQUAL(sc3.isConnected(), true); |
| 325 | |
| 326 | // sc goes out of scope, but without disconnecting |
| 327 | } |
| 328 | |
| 329 | so.emitSignal(sig); |
| 330 | BOOST_CHECK_EQUAL(hit, 4); // handler called |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 333 | BOOST_AUTO_TEST_CASE(ConnectSingleShot) |
| 334 | { |
| 335 | SignalOwner0 so; |
| 336 | |
| 337 | int hit = 0; |
| 338 | so.sig.connectSingleShot([&hit] { ++hit; }); |
| 339 | |
| 340 | so.emitSignal(sig); |
| 341 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 342 | |
| 343 | so.emitSignal(sig); |
| 344 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 345 | } |
| 346 | |
| 347 | BOOST_AUTO_TEST_CASE(ConnectSingleShotDisconnected) |
| 348 | { |
| 349 | SignalOwner0 so; |
| 350 | |
| 351 | int hit = 0; |
| 352 | Connection conn = so.sig.connectSingleShot([&hit] { ++hit; }); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 353 | BOOST_CHECK_EQUAL(conn.isConnected(), true); |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 354 | conn.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 355 | BOOST_CHECK_EQUAL(conn.isConnected(), false); |
Junxiao Shi | ecc57b5 | 2015-01-01 10:47:08 -0700 | [diff] [blame] | 356 | |
| 357 | so.emitSignal(sig); |
| 358 | BOOST_CHECK_EQUAL(hit, 0); // handler not called |
| 359 | } |
| 360 | |
| 361 | BOOST_AUTO_TEST_CASE(ConnectSingleShot1) |
| 362 | { |
| 363 | SignalEmitter1 se; |
| 364 | |
| 365 | int hit = 0; |
| 366 | se.sig.connectSingleShot([&hit] (int) { ++hit; }); |
| 367 | |
| 368 | se.emitTestSignal(); |
| 369 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 370 | |
| 371 | se.emitTestSignal(); |
| 372 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 373 | } |
| 374 | |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 375 | BOOST_AUTO_TEST_CASE(ConnectInHandler) |
| 376 | { |
| 377 | SignalOwner0 so; |
| 378 | |
| 379 | int hit1 = 0, hit2 = 0; bool hasHandler2 = false; |
| 380 | so.sig.connect([&] { |
| 381 | ++hit1; |
| 382 | if (!hasHandler2) { |
| 383 | so.sig.connect([&] { ++hit2; }); |
| 384 | hasHandler2 = true; |
| 385 | } |
| 386 | }); |
| 387 | |
| 388 | so.emitSignal(sig); |
| 389 | BOOST_CHECK_EQUAL(hit1, 1); // handler1 called |
| 390 | BOOST_CHECK_EQUAL(hit2, 0); // handler2 not called |
| 391 | |
| 392 | // new subscription takes effect |
| 393 | so.emitSignal(sig); |
| 394 | BOOST_CHECK_EQUAL(hit1, 2); // handler1 called |
| 395 | BOOST_CHECK_EQUAL(hit2, 1); // handler2 called |
| 396 | } |
| 397 | |
| 398 | BOOST_AUTO_TEST_CASE(DisconnectSelfInHandler) |
| 399 | { |
| 400 | SignalOwner0 so; |
| 401 | |
| 402 | int hit = 0; |
| 403 | Connection connection; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 404 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Davide Pesavento | be7804f | 2015-10-23 00:53:47 +0200 | [diff] [blame] | 405 | connection = so.sig.connect([&so, &connection, &hit] { |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 406 | ++hit; |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 407 | BOOST_CHECK_EQUAL(connection.isConnected(), true); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 408 | connection.disconnect(); |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 410 | BOOST_CHECK_EQUAL(so.isSigEmpty(), false); // disconnecting hasn't taken effect |
Davide Pesavento | be7804f | 2015-10-23 00:53:47 +0200 | [diff] [blame] | 411 | }); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 412 | |
| 413 | so.emitSignal(sig); |
| 414 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
Chengyu Fan | f46482c | 2015-02-03 16:55:53 -0700 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(connection.isConnected(), false); |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 416 | |
| 417 | // disconnecting takes effect |
| 418 | BOOST_CHECK_EQUAL(so.isSigEmpty(), true); |
| 419 | so.emitSignal(sig); |
| 420 | BOOST_CHECK_EQUAL(hit, 1); // handler not called |
| 421 | } |
| 422 | |
| 423 | BOOST_AUTO_TEST_CASE(ThrowInHandler) |
| 424 | { |
| 425 | SignalOwner0 so; |
| 426 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame^] | 427 | class HandlerError : public std::exception |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 428 | { |
| 429 | }; |
| 430 | |
| 431 | int hit = 0; |
| 432 | so.sig.connect([&] { |
| 433 | ++hit; |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame^] | 434 | // use plain 'throw' to ensure that Signal does not depend on the internal |
| 435 | // machinery of NDN_THROW and that it can catch all exceptions regardless |
| 436 | // of how they are thrown by the application |
| 437 | throw HandlerError{}; |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 438 | }); |
| 439 | |
| 440 | BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError); |
| 441 | BOOST_CHECK_EQUAL(hit, 1); // handler called |
| 442 | |
| 443 | BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError); |
| 444 | BOOST_CHECK_EQUAL(hit, 2); // handler called |
| 445 | } |
| 446 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 447 | BOOST_AUTO_TEST_SUITE_END() // TestSignal |
| 448 | BOOST_AUTO_TEST_SUITE_END() // Util |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 449 | |
| 450 | } // namespace tests |
| 451 | } // namespace signal |
| 452 | } // namespace util |
| 453 | } // namespace ndn |