PageRenderTime 83ms CodeModel.GetById 19ms RepoModel.GetById 3ms app.codeStats 1ms

/indra/llcommon/tests/lleventfilter_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 293 lines | 173 code | 12 blank | 108 comment | 0 complexity | 98a1cd0488c74986569438bf84d4b70d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lleventfilter_test.cpp
  3. * @author Nat Goodspeed
  4. * @date 2009-03-06
  5. * @brief Test for lleventfilter.
  6. *
  7. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. // Precompiled header
  29. #include "linden_common.h"
  30. // associated header
  31. #include "lleventfilter.h"
  32. // STL headers
  33. // std headers
  34. // external library headers
  35. // other Linden headers
  36. #include "../test/lltut.h"
  37. #include "stringize.h"
  38. #include "listener.h"
  39. #include "tests/wrapllerrs.h"
  40. /*****************************************************************************
  41. * Test classes
  42. *****************************************************************************/
  43. // Strictly speaking, we're testing LLEventTimeoutBase rather than the
  44. // production LLEventTimeout (using LLTimer) because we don't want every test
  45. // run to pause for some number of seconds until we reach a real timeout. But
  46. // as we've carefully put all functionality except actual LLTimer calls into
  47. // LLEventTimeoutBase, that should suffice. We're not not not trying to test
  48. // LLTimer here.
  49. class TestEventTimeout: public LLEventTimeoutBase
  50. {
  51. public:
  52. TestEventTimeout():
  53. mElapsed(true)
  54. {}
  55. TestEventTimeout(LLEventPump& source):
  56. LLEventTimeoutBase(source),
  57. mElapsed(true)
  58. {}
  59. // test hook
  60. void forceTimeout(bool timeout=true) { mElapsed = timeout; }
  61. protected:
  62. virtual void setCountdown(F32 seconds) { mElapsed = false; }
  63. virtual bool countdownElapsed() const { return mElapsed; }
  64. private:
  65. bool mElapsed;
  66. };
  67. /*****************************************************************************
  68. * TUT
  69. *****************************************************************************/
  70. namespace tut
  71. {
  72. struct filter_data
  73. {
  74. // The resemblance between this test data and that in llevents_tut.cpp
  75. // is not coincidental.
  76. filter_data():
  77. pumps(LLEventPumps::instance()),
  78. mainloop(pumps.obtain("mainloop")),
  79. listener0("first"),
  80. listener1("second")
  81. {}
  82. LLEventPumps& pumps;
  83. LLEventPump& mainloop;
  84. Listener listener0;
  85. Listener listener1;
  86. void check_listener(const std::string& desc, const Listener& listener, const LLSD& got)
  87. {
  88. ensure_equals(STRINGIZE(listener << ' ' << desc),
  89. listener.getLastEvent(), got);
  90. }
  91. };
  92. typedef test_group<filter_data> filter_group;
  93. typedef filter_group::object filter_object;
  94. filter_group filtergrp("lleventfilter");
  95. template<> template<>
  96. void filter_object::test<1>()
  97. {
  98. set_test_name("LLEventMatching");
  99. LLEventPump& driver(pumps.obtain("driver"));
  100. listener0.reset(0);
  101. // Listener isn't derived from LLEventTrackable specifically to test
  102. // various connection-management mechanisms. But that means we have a
  103. // couple of transient Listener objects, one of which is listening to
  104. // a persistent LLEventPump. Capture those connections in local
  105. // LLTempBoundListener instances so they'll disconnect
  106. // on destruction.
  107. LLTempBoundListener temp1(
  108. listener0.listenTo(driver));
  109. // Construct a pattern LLSD: desired Event must have a key "foo"
  110. // containing string "bar"
  111. LLEventMatching filter(driver, LLSD().insert("foo", "bar"));
  112. listener1.reset(0);
  113. LLTempBoundListener temp2(
  114. listener1.listenTo(filter));
  115. driver.post(1);
  116. check_listener("direct", listener0, LLSD(1));
  117. check_listener("filtered", listener1, LLSD(0));
  118. // Okay, construct an LLSD map matching the pattern
  119. LLSD data;
  120. data["foo"] = "bar";
  121. data["random"] = 17;
  122. driver.post(data);
  123. check_listener("direct", listener0, data);
  124. check_listener("filtered", listener1, data);
  125. }
  126. template<> template<>
  127. void filter_object::test<2>()
  128. {
  129. set_test_name("LLEventTimeout::actionAfter()");
  130. LLEventPump& driver(pumps.obtain("driver"));
  131. TestEventTimeout filter(driver);
  132. listener0.reset(0);
  133. LLTempBoundListener temp1(
  134. listener0.listenTo(filter));
  135. // Use listener1.call() as the Action for actionAfter(), since it
  136. // already provides a way to sense the call
  137. listener1.reset(0);
  138. // driver --> filter --> listener0
  139. filter.actionAfter(20,
  140. boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
  141. // Okay, (fake) timer is ticking. 'filter' can only sense the timer
  142. // when we pump mainloop. Do that right now to take the logic path
  143. // before either the anticipated event arrives or the timer expires.
  144. mainloop.post(17);
  145. check_listener("no timeout 1", listener1, LLSD(0));
  146. // Expected event arrives...
  147. driver.post(1);
  148. check_listener("event passed thru", listener0, LLSD(1));
  149. // Should have canceled the timer. Verify that by asserting that the
  150. // time has expired, then pumping mainloop again.
  151. filter.forceTimeout();
  152. mainloop.post(17);
  153. check_listener("no timeout 2", listener1, LLSD(0));
  154. // Verify chained actionAfter() calls, that is, that a second
  155. // actionAfter() resets the timer established by the first
  156. // actionAfter().
  157. filter.actionAfter(20,
  158. boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
  159. // Since our TestEventTimeout class isn't actually manipulating time
  160. // (quantities of seconds), only a bool "elapsed" flag, sense that by
  161. // forcing the flag between actionAfter() calls.
  162. filter.forceTimeout();
  163. // Pumping mainloop here would result in a timeout (as we'll verify
  164. // below). This state simulates a ticking timer that has not yet timed
  165. // out. But now, before a mainloop event lets 'filter' recognize
  166. // timeout on the previous actionAfter() call, pretend we're pushing
  167. // that timeout farther into the future.
  168. filter.actionAfter(20,
  169. boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
  170. // Look ma, no timeout!
  171. mainloop.post(17);
  172. check_listener("no timeout 3", listener1, LLSD(0));
  173. // Now let the updated actionAfter() timer expire.
  174. filter.forceTimeout();
  175. // Notice the timeout.
  176. mainloop.post(17);
  177. check_listener("timeout", listener1, LLSD("timeout"));
  178. // Timing out cancels the timer. Verify that.
  179. listener1.reset(0);
  180. filter.forceTimeout();
  181. mainloop.post(17);
  182. check_listener("no timeout 4", listener1, LLSD(0));
  183. // Reset the timer and then cancel() it.
  184. filter.actionAfter(20,
  185. boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
  186. // neither expired nor satisified
  187. mainloop.post(17);
  188. check_listener("no timeout 5", listener1, LLSD(0));
  189. // cancel
  190. filter.cancel();
  191. // timeout!
  192. filter.forceTimeout();
  193. mainloop.post(17);
  194. check_listener("no timeout 6", listener1, LLSD(0));
  195. }
  196. template<> template<>
  197. void filter_object::test<3>()
  198. {
  199. set_test_name("LLEventTimeout::eventAfter()");
  200. LLEventPump& driver(pumps.obtain("driver"));
  201. TestEventTimeout filter(driver);
  202. listener0.reset(0);
  203. LLTempBoundListener temp1(
  204. listener0.listenTo(filter));
  205. filter.eventAfter(20, LLSD("timeout"));
  206. // Okay, (fake) timer is ticking. 'filter' can only sense the timer
  207. // when we pump mainloop. Do that right now to take the logic path
  208. // before either the anticipated event arrives or the timer expires.
  209. mainloop.post(17);
  210. check_listener("no timeout 1", listener0, LLSD(0));
  211. // Expected event arrives...
  212. driver.post(1);
  213. check_listener("event passed thru", listener0, LLSD(1));
  214. // Should have canceled the timer. Verify that by asserting that the
  215. // time has expired, then pumping mainloop again.
  216. filter.forceTimeout();
  217. mainloop.post(17);
  218. check_listener("no timeout 2", listener0, LLSD(1));
  219. // Set timer again.
  220. filter.eventAfter(20, LLSD("timeout"));
  221. // Now let the timer expire.
  222. filter.forceTimeout();
  223. // Notice the timeout.
  224. mainloop.post(17);
  225. check_listener("timeout", listener0, LLSD("timeout"));
  226. // Timing out cancels the timer. Verify that.
  227. listener0.reset(0);
  228. filter.forceTimeout();
  229. mainloop.post(17);
  230. check_listener("no timeout 3", listener0, LLSD(0));
  231. }
  232. template<> template<>
  233. void filter_object::test<4>()
  234. {
  235. set_test_name("LLEventTimeout::errorAfter()");
  236. WrapLL_ERRS capture;
  237. LLEventPump& driver(pumps.obtain("driver"));
  238. TestEventTimeout filter(driver);
  239. listener0.reset(0);
  240. LLTempBoundListener temp1(
  241. listener0.listenTo(filter));
  242. filter.errorAfter(20, "timeout");
  243. // Okay, (fake) timer is ticking. 'filter' can only sense the timer
  244. // when we pump mainloop. Do that right now to take the logic path
  245. // before either the anticipated event arrives or the timer expires.
  246. mainloop.post(17);
  247. check_listener("no timeout 1", listener0, LLSD(0));
  248. // Expected event arrives...
  249. driver.post(1);
  250. check_listener("event passed thru", listener0, LLSD(1));
  251. // Should have canceled the timer. Verify that by asserting that the
  252. // time has expired, then pumping mainloop again.
  253. filter.forceTimeout();
  254. mainloop.post(17);
  255. check_listener("no timeout 2", listener0, LLSD(1));
  256. // Set timer again.
  257. filter.errorAfter(20, "timeout");
  258. // Now let the timer expire.
  259. filter.forceTimeout();
  260. // Notice the timeout.
  261. std::string threw;
  262. try
  263. {
  264. mainloop.post(17);
  265. }
  266. catch (const WrapLL_ERRS::FatalException& e)
  267. {
  268. threw = e.what();
  269. }
  270. ensure_contains("errorAfter() timeout exception", threw, "timeout");
  271. // Timing out cancels the timer. Verify that.
  272. listener0.reset(0);
  273. filter.forceTimeout();
  274. mainloop.post(17);
  275. check_listener("no timeout 3", listener0, LLSD(0));
  276. }
  277. } // namespace tut
  278. /*****************************************************************************
  279. * Link dependencies
  280. *****************************************************************************/
  281. #include "llsdutil.cpp"