/codebase/src-cpp/test/ng6/ddm/SubscribeInteractionWithRegionTest.cpp

https://github.com/graeme-muller/portico · C++ · 452 lines · 269 code · 45 blank · 138 comment · 0 complexity · e38d4444695796c8b98a937a5d538e0a 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 "SubscribeInteractionWithRegionTest.h"
  16. // Register test suite with the global repository
  17. CPPUNIT_TEST_SUITE_REGISTRATION( SubscribeInteractionWithRegionTest );
  18. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SubscribeInteractionWithRegionTest,
  19. "SubscribeInteractionWithRegionTest" );
  20. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SubscribeInteractionWithRegionTest,
  21. "subscribeInteractionWithRegion" );
  22. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SubscribeInteractionWithRegionTest, "ddm" );
  23. /////////////////////////////////////////////////////////////////////////////////////////////
  24. ////////////////////////////////// Constructors/Destructors /////////////////////////////////
  25. /////////////////////////////////////////////////////////////////////////////////////////////
  26. SubscribeInteractionWithRegionTest::SubscribeInteractionWithRegionTest()
  27. {
  28. this->defaultFederate = new TestNG6Federate( "defaultFederate" );
  29. this->listenerFederate = new TestNG6Federate( "listenerFederate" );
  30. }
  31. SubscribeInteractionWithRegionTest::~SubscribeInteractionWithRegionTest()
  32. {
  33. delete this->listenerFederate;
  34. delete this->defaultFederate;
  35. }
  36. /////////////////////////////////////////////////////////////////////////////////////////////
  37. /////////////////////////////// Test Setup and Helper Methods ///////////////////////////////
  38. /////////////////////////////////////////////////////////////////////////////////////////////
  39. void SubscribeInteractionWithRegionTest::setUp()
  40. {
  41. this->defaultFederate->quickCreate();
  42. this->defaultFederate->quickJoin();
  43. this->listenerFederate->quickJoin();
  44. // get the class handles
  45. this->xHandle = defaultFederate->quickICHandle( "InteractionRoot.X" );
  46. this->xaHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xa" );
  47. this->xbHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xb" );
  48. this->xcHandle = defaultFederate->quickPCHandle( "InteractionRoot.X", "xc" );
  49. // do pub/sub
  50. this->defaultFederate->quickPublish( "InteractionRoot.X" );
  51. // create the regions
  52. this->senderRegion = defaultFederate->quickCreateTestRegion( 100, 200 );
  53. this->senderRegionOOB = defaultFederate->quickCreateTestRegion( 1000, 2000 );
  54. this->listenerRegion = listenerFederate->quickCreateTestRegion( 150, 250 );
  55. this->listenerRegionOOB = listenerFederate->quickCreateTestRegion( 1500, 2500 );
  56. this->otherRegion = listenerFederate->quickCreateOtherRegion( 150, 250 );
  57. }
  58. void SubscribeInteractionWithRegionTest::tearDown()
  59. {
  60. delete this->otherRegion;
  61. delete this->senderRegion;
  62. delete this->senderRegionOOB;
  63. delete this->listenerRegion;
  64. delete this->listenerRegionOOB;
  65. this->listenerFederate->quickResign();
  66. this->defaultFederate->quickResign();
  67. this->defaultFederate->quickDestroy();
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////
  70. ////////////////////////////////// Subscribe Test Methods //////////////////////////////////
  71. ////////////////////////////////////////////////////////////////////////////////////////////
  72. void SubscribeInteractionWithRegionTest::validateSubscribed( RTI::Region *regionToSend )
  73. {
  74. // send an interaction from the default federate
  75. defaultFederate->quickSendWithRegion( "InteractionRoot.X", regionToSend, 3, "xa", "xb", "xc" );
  76. // wait for it to be received in the listener
  77. TestNG6Interaction *received = listenerFederate->fedamb->waitForROInteraction( xHandle );
  78. // validate the parameters for the interaction
  79. int result = strcmp( "xa", received->getParameter(xaHandle) );
  80. CPPUNIT_ASSERT_EQUAL( 0, result );
  81. result = strcmp( "xb", received->getParameter(xbHandle) );
  82. CPPUNIT_ASSERT_EQUAL( 0, result );
  83. result = strcmp( "xc", received->getParameter(xcHandle) );
  84. CPPUNIT_ASSERT_EQUAL( 0, result );
  85. // clean up
  86. delete received;
  87. }
  88. void SubscribeInteractionWithRegionTest::validateNotSubscribed( RTI::Region *regionToSend )
  89. {
  90. // send an interaction from the default federate
  91. defaultFederate->quickSendWithRegion( "InteractionRoot.X", regionToSend, 3, "xa", "xb", "xc" );
  92. // make sure the received doesn't get anything
  93. listenerFederate->fedamb->waitForROInteractionTimeout( xHandle );
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////
  96. ////////////////////////////////// Subscribe Test Methods //////////////////////////////////
  97. ////////////////////////////////////////////////////////////////////////////////////////////
  98. // subscribeInteractionClassWithRegion( RTI::InteractionClassHandle theClass,
  99. // RTI::Region &theRegion,
  100. // RTI::Boolean active )
  101. // throw( RTI::InteractionClassNotDefined,
  102. // RTI::RegionNotKnown,
  103. // RTI::InvalidRegionContext,
  104. // RTI::FederateLoggingServiceCalls,
  105. // RTI::FederateNotExecutionMember,
  106. // RTI::ConcurrentAccessAttempted,
  107. // RTI::SaveInProgress,
  108. // RTI::RestoreInProgress,
  109. // RTI::RTIinternalError )
  110. /////////////////////////////////////
  111. // TEST: (valid) testICSubscribe() //
  112. /////////////////////////////////////
  113. /*
  114. * 1. Validate that the listener isn't receiving interactions
  115. * 2. Subscribe, but with the OOB region
  116. * 3. Validate that the listener still isn't receiving interactions
  117. * 4. Subscribe with the normal region
  118. * 5. Validate that the listener does receive the interaction
  119. */
  120. void SubscribeInteractionWithRegionTest::testICSubscribe()
  121. {
  122. // 1. validate that we're not yet subscribed
  123. validateNotSubscribed( senderRegion );
  124. // 2. subscribe with OOB region
  125. try
  126. {
  127. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( xHandle, *listenerRegionOOB );
  128. }
  129. catch( RTI::Exception &e )
  130. {
  131. failTest( "Unexpected exception while subscribing to interaction with region: %s",
  132. e._reason );
  133. }
  134. // 3. validate that we're still not receiving interactions as they're being sent
  135. // with a region that doesn't overlap the subscription region
  136. validateNotSubscribed( senderRegion );
  137. // 4. subscribe with the normal region, which should bring us into an overlap
  138. try
  139. {
  140. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( xHandle, *listenerRegion );
  141. }
  142. catch( RTI::Exception &e )
  143. {
  144. failTest( "Unexpected exception while subscribing to interaction with region: %s",
  145. e._reason );
  146. }
  147. // 5. validate that we do now receive the interaction
  148. validateSubscribed( senderRegion );
  149. }
  150. //////////////////////////////////////////////
  151. // TEST: testICSubscribeWithUnknownRegion() //
  152. //////////////////////////////////////////////
  153. void SubscribeInteractionWithRegionTest::testICSubscribeWithUnknownRegion()
  154. {
  155. try
  156. {
  157. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( xHandle, *senderRegion );
  158. failTestMissingException( "RegionNotKnown",
  159. "subscribing to interaction with region create by other federate" );
  160. }
  161. catch( RTI::RegionNotKnown &rnk )
  162. {
  163. // success!
  164. }
  165. catch( RTI::Exception &e )
  166. {
  167. failTestWrongException( "RegionNotKnown", e,
  168. "subscribing to interaction with region create by other federate" );
  169. }
  170. }
  171. //////////////////////////////////////////////
  172. // TEST: testICSubscribeWithInvalidRegion() //
  173. //////////////////////////////////////////////
  174. void SubscribeInteractionWithRegionTest::testICSubscribeWithInvalidRegion()
  175. {
  176. try
  177. {
  178. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( xHandle, *otherRegion );
  179. failTestMissingException( "InvalidRegionContext",
  180. "subscribing to interaction with region of wrong type for class" );
  181. }
  182. catch( RTI::InvalidRegionContext &irc )
  183. {
  184. // success!
  185. }
  186. catch( RTI::Exception &e )
  187. {
  188. failTestWrongException( "InvalidRegionContext", e,
  189. "subscribing to interaction with region of wrong type for class" );
  190. }
  191. }
  192. //////////////////////////////////////////////
  193. // TEST: testICSubscribeWithInvalidHandle() //
  194. //////////////////////////////////////////////
  195. void SubscribeInteractionWithRegionTest::testICSubscribeWithInvalidHandle()
  196. {
  197. try
  198. {
  199. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( 10000000, *listenerRegion );
  200. failTestMissingException( "InteractionClassNotDefined",
  201. "subscribing to interaction class with invalid handle" );
  202. }
  203. catch( RTI::InteractionClassNotDefined& icnd )
  204. {
  205. // success!
  206. }
  207. catch( RTI::Exception& e )
  208. {
  209. failTestWrongException( "InteractionClassNotDefined", e,
  210. "subscribing to interaction class with invalid handle" );
  211. }
  212. }
  213. //////////////////////////////////////////
  214. // TEST: testICSubscribeWhenNotJoined() //
  215. //////////////////////////////////////////
  216. void SubscribeInteractionWithRegionTest::testICSubscribeWhenNotJoined()
  217. {
  218. listenerFederate->quickResign();
  219. try
  220. {
  221. listenerFederate->rtiamb->subscribeInteractionClassWithRegion( xHandle, *listenerRegion );
  222. failTestMissingException( "FederateNotExecutionMember",
  223. "subscribing to interaction class when not joined" );
  224. }
  225. catch( RTI::FederateNotExecutionMember& fnem )
  226. {
  227. // success!
  228. }
  229. catch( RTI::Exception& e )
  230. {
  231. failTestWrongException( "FederateNotExecutionMember", e,
  232. "subscribing to interaction class when not joined" );
  233. }
  234. }
  235. ////////////////////////////////////////////////////////////////////////////////////////////
  236. ///////////////////////////////// Unsubscribe Test Methods /////////////////////////////////
  237. ////////////////////////////////////////////////////////////////////////////////////////////
  238. // unsubscribeInteractionClassWithRegion( RTI::InteractionClassHandle theClass,
  239. // RTI::Region &theRegion )
  240. // throw( RTI::InteractionClassNotDefined,
  241. // RTI::InteractionClassNotSubscribed,
  242. // RTI::RegionNotKnown,
  243. // RTI::FederateNotExecutionMember,
  244. // RTI::ConcurrentAccessAttempted,
  245. // RTI::SaveInProgress,
  246. // RTI::RestoreInProgress,
  247. // RTI::RTIinternalError )
  248. ///////////////////////////////////////
  249. // TEST: (valid) testICUnsubscribe() //
  250. ///////////////////////////////////////
  251. /*
  252. * 1. Subscribe with both regular and OOB region
  253. * 2. Validate interactions sent to both regions are received
  254. * 3. Unsubscribe from OOB region
  255. * 4. Validate interactions to regular region get through, but not those to OOB region
  256. * 5. Unsubscribe from normal region
  257. * 6. Validate no interactions get through
  258. */
  259. void SubscribeInteractionWithRegionTest::testICUnsubscribe()
  260. {
  261. // 1. subscribe to interaction in both regions
  262. listenerFederate->quickSubscribeWithRegion( "InteractionRoot.X", listenerRegion );
  263. listenerFederate->quickSubscribeWithRegion( "InteractionRoot.X", listenerRegionOOB );
  264. // 2. validate interactions sent to both regions are received
  265. validateSubscribed( senderRegion );
  266. validateSubscribed( senderRegionOOB );
  267. // 3. unsubscribe from OOB region
  268. try
  269. {
  270. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( xHandle,
  271. *listenerRegionOOB );
  272. }
  273. catch( RTI::Exception &e )
  274. {
  275. failTest( "Unexpected exception unsubscribing from interaction with region: %s", e._reason );
  276. }
  277. // 4. validate that interactions to OOB not received, while interactions to normal are
  278. validateSubscribed( senderRegion );
  279. validateNotSubscribed( senderRegionOOB );
  280. // 5. unsubscribe from the normal region
  281. try
  282. {
  283. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( xHandle, *listenerRegion );
  284. }
  285. catch( RTI::Exception &e )
  286. {
  287. failTest( "Unexpected exception unsubscribing from interaction with region: %s", e._reason );
  288. }
  289. // 6. validate no longer subscribed in either region
  290. validateNotSubscribed( senderRegion );
  291. validateNotSubscribed( senderRegionOOB );
  292. }
  293. ///////////////////////////////////////////////////////////////////////////
  294. // TEST: (valid) testICUnsubscribeWithoutRegionRemovesAllSubscriptions() //
  295. ///////////////////////////////////////////////////////////////////////////
  296. /*
  297. * If we are subscribed with regions and then unsubscribe without providing region data, the
  298. * entire subscription (for all regions) should be removed.
  299. */
  300. void SubscribeInteractionWithRegionTest::testICUnsubscribeWithoutRegionRemovesAllSubscriptions()
  301. {
  302. // subscribe to interaction in both regions and validate
  303. listenerFederate->quickSubscribeWithRegion( "InteractionRoot.X", listenerRegion );
  304. listenerFederate->quickSubscribeWithRegion( "InteractionRoot.X", listenerRegionOOB );
  305. validateSubscribed( senderRegion );
  306. validateSubscribed( senderRegionOOB );
  307. // unsubscribe without region and validate that we are no longer subscribed in any way
  308. try
  309. {
  310. listenerFederate->rtiamb->unsubscribeInteractionClass( xHandle );
  311. }
  312. catch( RTI::Exception &e )
  313. {
  314. failTest( "Unexpected exception unsubscribing from interaction: %s", e._reason );
  315. }
  316. // 6. validate no longer subscribed in either region
  317. validateNotSubscribed( senderRegion );
  318. validateNotSubscribed( senderRegionOOB );
  319. }
  320. ////////////////////////////////////////////////
  321. // TEST: testICUnsubscribeWithUnknownRegion() //
  322. ////////////////////////////////////////////////
  323. void SubscribeInteractionWithRegionTest::testICUnsubscribeWithUnknownRegion()
  324. {
  325. // subscribe so that this can't be a source of error below (we want to isolate a specific
  326. // error, so we have to remove the possibility of others)
  327. listenerFederate->quickSubscribeWithRegion( "InteractionRoot.X", listenerRegion );
  328. try
  329. {
  330. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( xHandle, *senderRegion );
  331. failTestMissingException( "RegionNotKnown",
  332. "unsubscribing interaction with region created in other federate" );
  333. }
  334. catch( RTI::RegionNotKnown &rnk )
  335. {
  336. // success!
  337. }
  338. catch( RTI::Exception &e )
  339. {
  340. failTestWrongException( "RegionNotKnown", e,
  341. "subscribing interaction with region created in other federate" );
  342. }
  343. }
  344. ////////////////////////////////////////////////
  345. // TEST: testICUnsubscribeWithInvalidHandle() //
  346. ////////////////////////////////////////////////
  347. void SubscribeInteractionWithRegionTest::testICUnsubscribeWithInvalidHandle()
  348. {
  349. try
  350. {
  351. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( 1000000, *listenerRegion );
  352. failTestMissingException( "InteractionClassNotDefined",
  353. "unsubscribing to interaction class with invalid handle" );
  354. }
  355. catch( RTI::InteractionClassNotDefined& icnd )
  356. {
  357. // success!
  358. }
  359. catch( RTI::Exception& e )
  360. {
  361. failTestWrongException( "InteractionClassNotDefined", e,
  362. "unsubscribing to interaction class with invalid handle" );
  363. }
  364. }
  365. ////////////////////////////////////////////////
  366. // TEST: testICUnsubscribeWhenNotSubscribed() //
  367. ////////////////////////////////////////////////
  368. void SubscribeInteractionWithRegionTest::testICUnsubscribeWhenNotSubscribed()
  369. {
  370. try
  371. {
  372. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( xHandle, *listenerRegion );
  373. failTestMissingException( "InteractionClassNotSubscribed",
  374. "unsubscribing interaction when not subscribed" );
  375. }
  376. catch( RTI::InteractionClassNotSubscribed &icns )
  377. {
  378. // success!
  379. }
  380. catch( RTI::Exception &e )
  381. {
  382. failTestWrongException( "InteractionClassNotSubscribed", e,
  383. "unsubscribing interaction when not subscribed" );
  384. }
  385. }
  386. ////////////////////////////////////////////
  387. // TEST: testICUnsubscribeWhenNotJoined() //
  388. ////////////////////////////////////////////
  389. void SubscribeInteractionWithRegionTest::testICUnsubscribeWhenNotJoined()
  390. {
  391. listenerFederate->quickResign();
  392. try
  393. {
  394. listenerFederate->rtiamb->unsubscribeInteractionClassWithRegion( xHandle, *listenerRegion );
  395. failTestMissingException( "FederateNotExecutionMember",
  396. "unsubscribing from interaction class when not joined" );
  397. }
  398. catch( RTI::FederateNotExecutionMember& fnem )
  399. {
  400. // success!
  401. }
  402. catch( RTI::Exception& e )
  403. {
  404. failTestWrongException( "FederateNotExecutionMember", e,
  405. "unsubscribing from interaction class when not joined" );
  406. }
  407. }