/codebase/src-cpp/test/ng6/object/SendInteractionTest.cpp

https://github.com/graeme-muller/portico · C++ · 482 lines · 340 code · 44 blank · 98 comment · 0 complexity · 8911baeedf1b8a2cda1a103f5f621e69 MD5 · raw file

  1. /*
  2. * Copyright 2008 The Portico Project
  3. *
  4. * This file is part of portico.
  5. *
  6. * portico is free software; you can redistribute it and/or modify
  7. * it under the terms of the Common Developer and Distribution License (CDDL)
  8. * as published by Sun Microsystems. For more information see the LICENSE file.
  9. *
  10. * Use of this software is strictly AT YOUR OWN RISK!!!
  11. * If something bad happens you do not have permission to come crying to me.
  12. * (that goes for your lawyer as well)
  13. *
  14. */
  15. #include "SendInteractionTest.h"
  16. // Register test suite with the global repository
  17. CPPUNIT_TEST_SUITE_REGISTRATION( SendInteractionTest );
  18. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SendInteractionTest, "SendInteractionTest" );
  19. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SendInteractionTest, "sendInteraction" );
  20. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SendInteractionTest, "objectManagement" );
  21. /////////////////////////////////////////////////////////////////////////////////////////////
  22. ////////////////////////////////// Constructors/Destructors /////////////////////////////////
  23. /////////////////////////////////////////////////////////////////////////////////////////////
  24. SendInteractionTest::SendInteractionTest()
  25. {
  26. this->defaultFederate = new TestNG6Federate( "defaultFederate" );
  27. this->listenerFederate = new TestNG6Federate( "listenerFederate" );
  28. }
  29. SendInteractionTest::~SendInteractionTest()
  30. {
  31. delete this->listenerFederate;
  32. delete this->defaultFederate;
  33. }
  34. /////////////////////////////////////////////////////////////////////////////////////////////
  35. /////////////////////////////// Test Setup and Helper Methods ///////////////////////////////
  36. /////////////////////////////////////////////////////////////////////////////////////////////
  37. void SendInteractionTest::setUp()
  38. {
  39. this->defaultFederate->quickCreate();
  40. this->defaultFederate->quickJoin();
  41. this->listenerFederate->quickJoin();
  42. // get the class handles
  43. this->xHandle = defaultFederate->quickICHandle( "InteractionRoot.X" );
  44. this->yHandle = defaultFederate->quickICHandle( "InteractionRoot.X.Y" );
  45. this->xaHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xa" );
  46. this->xbHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xb" );
  47. this->xcHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xc" );
  48. this->yaHandle = defaultFederate->quickPCHandle( "InteractionRoot.X.Y", "ya" );
  49. this->ybHandle = defaultFederate->quickPCHandle( "InteractionRoot.X.Y", "yb" );
  50. this->ycHandle = defaultFederate->quickPCHandle( "InteractionRoot.X.Y", "yc" );
  51. // do pub/sub
  52. this->defaultFederate->quickPublish( "InteractionRoot.X.Y" );
  53. this->listenerFederate->quickSubscribe( "InteractionRoot.X.Y" );
  54. // enable the time profiles
  55. defaultFederate->quickEnableRegulating( 5.0 );
  56. listenerFederate->quickEnableAsync();
  57. listenerFederate->quickEnableConstrained();
  58. // fill out the phvps
  59. this->phvps = defaultFederate->createPHVPS( 6 );
  60. this->phvps->add( xaHandle, "xa", 3 );
  61. this->phvps->add( xbHandle, "xb", 3 );
  62. this->phvps->add( xcHandle, "xc", 3 );
  63. this->phvps->add( yaHandle, "ya", 3 );
  64. this->phvps->add( ybHandle, "yb", 3 );
  65. this->phvps->add( ycHandle, "yc", 3 );
  66. }
  67. void SendInteractionTest::tearDown()
  68. {
  69. delete phvps;
  70. this->listenerFederate->quickResign();
  71. this->defaultFederate->quickResign();
  72. this->defaultFederate->quickDestroy();
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////
  75. //////////////////////////////////// Test Helper Methods ///////////////////////////////////
  76. ////////////////////////////////////////////////////////////////////////////////////////////
  77. void SendInteractionTest::validateReceivedRO()
  78. {
  79. // wait for the listener to receive it
  80. TestNG6Interaction *received = listenerFederate->fedamb->waitForROInteraction( yHandle );
  81. // validate the contents of the interaction
  82. int result = strcmp( "xa", received->getParameter(xaHandle) );
  83. CPPUNIT_ASSERT_EQUAL( 0, result );
  84. result = strcmp( "xb", received->getParameter(xbHandle) );
  85. CPPUNIT_ASSERT_EQUAL( 0, result );
  86. result = strcmp( "xc", received->getParameter(xcHandle) );
  87. CPPUNIT_ASSERT_EQUAL( 0, result );
  88. result = strcmp( "ya", received->getParameter(yaHandle) );
  89. CPPUNIT_ASSERT_EQUAL( 0, result );
  90. result = strcmp( "yb", received->getParameter(ybHandle) );
  91. CPPUNIT_ASSERT_EQUAL( 0, result );
  92. result = strcmp( "yc", received->getParameter(ycHandle) );
  93. CPPUNIT_ASSERT_EQUAL( 0, result );
  94. // clean up
  95. delete received;
  96. }
  97. void SendInteractionTest::validateReceivedTSO()
  98. {
  99. // wait for the listener to receive it
  100. TestNG6Interaction *received = listenerFederate->fedamb->waitForTSOInteraction( yHandle );
  101. // validate the contents of the interaction
  102. int result = strcmp( "xa", received->getParameter(xaHandle) );
  103. CPPUNIT_ASSERT_EQUAL( 0, result );
  104. result = strcmp( "xb", received->getParameter(xbHandle) );
  105. CPPUNIT_ASSERT_EQUAL( 0, result );
  106. result = strcmp( "xc", received->getParameter(xcHandle) );
  107. CPPUNIT_ASSERT_EQUAL( 0, result );
  108. result = strcmp( "ya", received->getParameter(yaHandle) );
  109. CPPUNIT_ASSERT_EQUAL( 0, result );
  110. result = strcmp( "yb", received->getParameter(ybHandle) );
  111. CPPUNIT_ASSERT_EQUAL( 0, result );
  112. result = strcmp( "yc", received->getParameter(ycHandle) );
  113. CPPUNIT_ASSERT_EQUAL( 0, result );
  114. // clean up
  115. delete received;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////////////////
  118. /////////////////////////////////// RO Test Send Methods ///////////////////////////////////
  119. ////////////////////////////////////////////////////////////////////////////////////////////
  120. ///////////////////////////////////////////
  121. // TEST: (valid) testSendROInteraction() //
  122. ///////////////////////////////////////////
  123. void SendInteractionTest::testSendROInteraction()
  124. {
  125. try
  126. {
  127. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, "NA" );
  128. }
  129. catch( RTI::Exception &e )
  130. {
  131. failTest( "Unexpected exception while sending RO interaction: %s", e._reason );
  132. }
  133. validateReceivedRO();
  134. }
  135. ///////////////////////////////////////////////////////////
  136. // TEST: (valid) testSendROInteractionWithNoParameters() //
  137. ///////////////////////////////////////////////////////////
  138. void SendInteractionTest::testSendROInteractionWithNoParameters()
  139. {
  140. RTI::ParameterHandleValuePairSet *emptySet = defaultFederate->createPHVPS(0);
  141. try
  142. {
  143. defaultFederate->rtiamb->sendInteraction( yHandle, *emptySet, "NA" );
  144. delete emptySet;
  145. }
  146. catch( RTI::Exception &e )
  147. {
  148. delete emptySet;
  149. failTest( "Unexpected exception sending RO interaction with empty params: %s", e._reason );
  150. }
  151. // wait for the interaciton and validate that it has no parameters
  152. TestNG6Interaction *interaction = listenerFederate->fedamb->waitForROInteraction( yHandle );
  153. int size = interaction->getSize();
  154. delete interaction;
  155. CPPUNIT_ASSERT_EQUAL( 0, size );
  156. }
  157. ///////////////////////////////////////////////////
  158. // TEST: testSendROInteractionWithInvalidClass() //
  159. ///////////////////////////////////////////////////
  160. void SendInteractionTest::testSendROInteractionWithInvalidClass()
  161. {
  162. try
  163. {
  164. defaultFederate->rtiamb->sendInteraction( 10000000, *phvps, "NA" );
  165. failTestMissingException( "InteractionClassNotDefined",
  166. "sending interaction with wrong class handle" );
  167. }
  168. catch( RTI::InteractionClassNotDefined &icnd )
  169. {
  170. // success!
  171. }
  172. catch( RTI::Exception &e )
  173. {
  174. failTestWrongException( "InteractionClassNotDefined", e,
  175. "sending interaction with wrong class handle" );
  176. }
  177. }
  178. ///////////////////////////////////////////////////////
  179. // TEST: testSendROInteractionWithInvalidParameter() //
  180. ///////////////////////////////////////////////////////
  181. void SendInteractionTest::testSendROInteractionWithInvalidParameter()
  182. {
  183. RTI::ParameterHandleValuePairSet *params = defaultFederate->createPHVPS( 2 );
  184. params->add( xaHandle, "xa", 3 );
  185. params->add( 10000000, "no", 3 );
  186. try
  187. {
  188. defaultFederate->rtiamb->sendInteraction( yHandle, *params, "NA" );
  189. delete params;
  190. failTestMissingException( "InteractionParameterNotDefined",
  191. "sending interaction with invalid param handle" );
  192. }
  193. catch( RTI::InteractionParameterNotDefined &ipnd )
  194. {
  195. // success!
  196. delete params;
  197. }
  198. catch( RTI::Exception &e )
  199. {
  200. delete params;
  201. failTestWrongException( "InteractionParameterNotDefined", e,
  202. "sending interaction with invalid param handle" );
  203. }
  204. }
  205. ///////////////////////////////////////////////////
  206. // TEST: testSendROInteractionWhenNotPublished() //
  207. ///////////////////////////////////////////////////
  208. void SendInteractionTest::testSendROInteractionWhenNotPublished()
  209. {
  210. try
  211. {
  212. defaultFederate->rtiamb->sendInteraction( xHandle, *phvps, "NA" );
  213. failTestMissingException( "InteractionClassNotPublished",
  214. "sending interaction of class we don't publish" );
  215. }
  216. catch( RTI::InteractionClassNotPublished &icnp )
  217. {
  218. // success!
  219. }
  220. catch( RTI::Exception &e )
  221. {
  222. failTestWrongException( "InteractionClassNotPublished", e,
  223. "sending interaction of class we don't publish" );
  224. }
  225. }
  226. ////////////////////////////////////////////////
  227. // TEST: testSendROInteractionWhenNotJoined() //
  228. ////////////////////////////////////////////////
  229. void SendInteractionTest::testSendROInteractionWhenNotJoined()
  230. {
  231. defaultFederate->quickResign();
  232. try
  233. {
  234. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, "NA" );
  235. failTestMissingException( "FederateNotExecutionMember",
  236. "sending interaction when not joined" );
  237. }
  238. catch( RTI::FederateNotExecutionMember &fnem )
  239. {
  240. // success!
  241. }
  242. catch( RTI::Exception &e )
  243. {
  244. failTestWrongException( "FederateNotExecutionMember", e,
  245. "sending interaction when not joined" );
  246. }
  247. }
  248. ////////////////////////////////////////////////////////////////////////////////////////////
  249. /////////////////////////////////// TSO Test Send Methods //////////////////////////////////
  250. ////////////////////////////////////////////////////////////////////////////////////////////
  251. ////////////////////////////////////////////
  252. // TEST: (valid) testSendTSOInteraction() //
  253. ////////////////////////////////////////////
  254. void SendInteractionTest::testSendTSOInteraction()
  255. {
  256. try
  257. {
  258. RTIfedTime theTime = 10.0;
  259. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, theTime, "NA" );
  260. }
  261. catch( RTI::Exception &e )
  262. {
  263. failTest( "Unexpected exception while sending TSO interaction: %s", e._reason );
  264. }
  265. // shouldn't receive the reflection yet
  266. listenerFederate->fedamb->waitForTSOInteractionTimeout( yHandle );
  267. defaultFederate->quickAdvanceAndWait( 10.0 );
  268. listenerFederate->quickAdvanceAndWait( 10.0 );
  269. validateReceivedTSO();
  270. }
  271. ////////////////////////////////////////////////////////////
  272. // TEST: (valid) testSendTSOInteractionWithNoParameters() //
  273. ////////////////////////////////////////////////////////////
  274. void SendInteractionTest::testSendTSOInteractionWithNoParameters()
  275. {
  276. RTI::ParameterHandleValuePairSet *emptySet = defaultFederate->createPHVPS(0);
  277. try
  278. {
  279. RTIfedTime theTime = 10.0;
  280. defaultFederate->rtiamb->sendInteraction( yHandle, *emptySet, theTime, "NA" );
  281. delete emptySet;
  282. }
  283. catch( RTI::Exception &e )
  284. {
  285. delete emptySet;
  286. failTest( "Unexpected exception sending TSO interaction with empty params: %s", e._reason );
  287. }
  288. // wait for the interaciton and validate that it has no parameters
  289. listenerFederate->fedamb->waitForTSOInteractionTimeout( yHandle );
  290. defaultFederate->quickAdvanceAndWait( 10.0 );
  291. listenerFederate->quickAdvanceAndWait( 10.0 );
  292. // should be able to get it now
  293. TestNG6Interaction *interaction = listenerFederate->fedamb->waitForTSOInteraction( yHandle );
  294. int size = interaction->getSize();
  295. delete interaction;
  296. CPPUNIT_ASSERT_EQUAL( 0, size );
  297. }
  298. ////////////////////////////////////////////////////
  299. // TEST: testSendTSOInteractionWithInvalidClass() //
  300. ////////////////////////////////////////////////////
  301. void SendInteractionTest::testSendTSOInteractionWithInvalidClass()
  302. {
  303. try
  304. {
  305. RTIfedTime theTime = 10.0;
  306. defaultFederate->rtiamb->sendInteraction( 10000000, *phvps, theTime, "NA" );
  307. failTestMissingException( "InteractionClassNotDefined",
  308. "sending interaction with wrong class handle" );
  309. }
  310. catch( RTI::InteractionClassNotDefined &icnd )
  311. {
  312. // success!
  313. }
  314. catch( RTI::Exception &e )
  315. {
  316. failTestWrongException( "InteractionClassNotDefined", e,
  317. "sending interaction with wrong class handle" );
  318. }
  319. }
  320. ////////////////////////////////////////////////////////
  321. // TEST: testSendTSOInteractionWithInvalidParameter() //
  322. ////////////////////////////////////////////////////////
  323. void SendInteractionTest::testSendTSOInteractionWithInvalidParameter()
  324. {
  325. RTI::ParameterHandleValuePairSet *params = defaultFederate->createPHVPS( 2 );
  326. params->add( xaHandle, "xa", 3 );
  327. params->add( 10000000, "no", 3 );
  328. try
  329. {
  330. RTIfedTime theTime = 10.0;
  331. defaultFederate->rtiamb->sendInteraction( yHandle, *params, theTime, "NA" );
  332. delete params;
  333. failTestMissingException( "InteractionParameterNotDefined",
  334. "sending interaction with invalid param handle" );
  335. }
  336. catch( RTI::InteractionParameterNotDefined &ipnd )
  337. {
  338. // success!
  339. delete params;
  340. }
  341. catch( RTI::Exception &e )
  342. {
  343. delete params;
  344. failTestWrongException( "InteractionParameterNotDefined", e,
  345. "sending interaction with invalid param handle" );
  346. }
  347. }
  348. ////////////////////////////////////////////////////
  349. // TEST: testSendTSOInteractionWhenNotPublished() //
  350. ////////////////////////////////////////////////////
  351. void SendInteractionTest::testSendTSOInteractionWhenNotPublished()
  352. {
  353. try
  354. {
  355. RTIfedTime theTime = 10.0;
  356. defaultFederate->rtiamb->sendInteraction( xHandle, *phvps, theTime, "NA" );
  357. failTestMissingException( "InteractionClassNotPublished",
  358. "sending interaction of class we don't publish" );
  359. }
  360. catch( RTI::InteractionClassNotPublished &icnp )
  361. {
  362. // success!
  363. }
  364. catch( RTI::Exception &e )
  365. {
  366. failTestWrongException( "InteractionClassNotPublished", e,
  367. "sending interaction of class we don't publish" );
  368. }
  369. }
  370. ///////////////////////////////////////////////////
  371. // TEST: testSendTSOInteractionWithInvalidTime() //
  372. ///////////////////////////////////////////////////
  373. void SendInteractionTest::testSendTSOInteractionWithInvalidTime()
  374. {
  375. try
  376. {
  377. // can't be -1.0 as that is the time used by Portico to represent no timestamp
  378. // thus, the LRC/RTI will process the request as an RO one, not a TSO with timestamp of -1
  379. RTIfedTime theTime = -11.0;
  380. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, theTime, "NA" );
  381. failTestMissingException( "InvalidFederationTime",
  382. "sending interaction with negative time" );
  383. }
  384. catch( RTI::InvalidFederationTime &fnem )
  385. {
  386. // success!
  387. }
  388. catch( RTI::Exception &e )
  389. {
  390. failTestWrongException( "InvalidFederationTime", e,
  391. "sending interaction with negative time" );
  392. }
  393. }
  394. //////////////////////////////////////////////////////////
  395. // TEST: testSendTSOInteractionWithTimeBelowLookahead() //
  396. //////////////////////////////////////////////////////////
  397. void SendInteractionTest::testSendTSOInteractionWithTimeBelowLookahead()
  398. {
  399. try
  400. {
  401. RTIfedTime theTime = 3.0;
  402. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, theTime, "NA" );
  403. failTestMissingException( "InvalidFederationTime",
  404. "sending interaction with time below lookahead" );
  405. }
  406. catch( RTI::InvalidFederationTime &fnem )
  407. {
  408. // success!
  409. }
  410. catch( RTI::Exception &e )
  411. {
  412. failTestWrongException( "InvalidFederationTime", e,
  413. "sending interaction with time below lookahead" );
  414. }
  415. }
  416. /////////////////////////////////////////////////
  417. // TEST: testSendTSOInteractionWhenNotJoined() //
  418. /////////////////////////////////////////////////
  419. void SendInteractionTest::testSendTSOInteractionWhenNotJoined()
  420. {
  421. defaultFederate->quickResign();
  422. try
  423. {
  424. RTIfedTime theTime = 10.0;
  425. defaultFederate->rtiamb->sendInteraction( yHandle, *phvps, theTime, "NA" );
  426. failTestMissingException( "FederateNotExecutionMember",
  427. "sending interaction when not joined" );
  428. }
  429. catch( RTI::FederateNotExecutionMember &fnem )
  430. {
  431. // success!
  432. }
  433. catch( RTI::Exception &e )
  434. {
  435. failTestWrongException( "FederateNotExecutionMember", e,
  436. "sending interaction when not joined" );
  437. }
  438. }