ctkEAScenario1TestSuite.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkEAScenario1TestSuite_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <service/event/ctkEventConstants.h>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <QTest>
  20. //----------------------------------------------------------------------------
  21. ctkEAScenario1EventConsumer::ctkEAScenario1EventConsumer(
  22. ctkPluginContext* pluginContext, const QStringList& topics,
  23. int messagesSent, bool useSignalSlot)
  24. : context(pluginContext), topicsToConsume(topics),
  25. numOfasynchMessages(0), numOfsynchMessages(0),
  26. synchMessageExpectedNumber(0), asynchMessageExpectedNumber(0),
  27. messagesSent(messagesSent), error(false), exc(""),
  28. useSignalSlot(useSignalSlot)
  29. {
  30. }
  31. //----------------------------------------------------------------------------
  32. void ctkEAScenario1EventConsumer::runTest()
  33. {
  34. numOfasynchMessages = 0;
  35. numOfsynchMessages = 0;
  36. synchMessageExpectedNumber = 0;
  37. asynchMessageExpectedNumber = 0;
  38. /* create the hashtable to put properties in */
  39. ctkDictionary props;
  40. /* put service.pid property in hashtable */
  41. props.insert(ctkEventConstants::EVENT_TOPIC, topicsToConsume);
  42. if (useSignalSlot)
  43. {
  44. /* Claims the reference of the EventAdmin Service */
  45. eventAdminRef = context->getServiceReference<ctkEventAdmin>();
  46. /* assert that a reference is aquired */
  47. QVERIFY2(eventAdminRef, "Should be able to get reference to ctkEventAdmin service");
  48. /* get the service */
  49. eventAdmin = context->getService<ctkEventAdmin>(eventAdminRef);
  50. /* assert that service is available */
  51. QVERIFY2(eventAdmin, "Should be able to get instance to ctkEventAdmin object");
  52. slotId = eventAdmin->subscribeSlot(this, SLOT(handleEvent(ctkEvent)), props);
  53. }
  54. else
  55. {
  56. /* register the service */
  57. serviceRegistration = context->registerService<ctkEventHandler>(this, props);
  58. QVERIFY2(serviceRegistration, "service registration should not be invalid");
  59. }
  60. }
  61. //----------------------------------------------------------------------------
  62. void ctkEAScenario1EventConsumer::cleanup()
  63. {
  64. try
  65. {
  66. if (useSignalSlot)
  67. {
  68. eventAdmin->unsubscribeSlot(slotId);
  69. context->ungetService(eventAdminRef);
  70. }
  71. else
  72. {
  73. serviceRegistration.unregister();
  74. }
  75. }
  76. catch (const ctkException&)
  77. {}
  78. if (error)
  79. {
  80. throw exc;
  81. }
  82. QCOMPARE(messagesSent, synchMessageExpectedNumber); //"Not all synch messages recieved"
  83. QCOMPARE(messagesSent, asynchMessageExpectedNumber); //"Not all asynch messages recieved"
  84. }
  85. //----------------------------------------------------------------------------
  86. void ctkEAScenario1EventConsumer::reset()
  87. {
  88. numOfasynchMessages = 0;
  89. numOfsynchMessages = 0;
  90. synchMessageExpectedNumber = 0;
  91. asynchMessageExpectedNumber = 0;
  92. }
  93. //----------------------------------------------------------------------------
  94. void ctkEAScenario1EventConsumer::handleEvent(const ctkEvent& event)
  95. {
  96. try
  97. {
  98. //TODO security topic permission
  99. // TopicPermission permissionAquired
  100. // = new TopicPermission((String)event.getProperty
  101. // (EventConstants.EVENT_TOPIC),"subscribe");
  102. // TopicPermission actualPermission
  103. // = new TopicPermission("com/acme/*","subscribe");
  104. // assertTrue(getName() +"Should not recevice this topic:"
  105. // +(String)event.getProperty(EventConstants.EVENT_TOPIC),
  106. // actualPermission.implies(permissionAquired));
  107. /* try to get the message */
  108. QString message = event.getProperty("Synchronous message").toString();
  109. /* check if message is null */
  110. if(!message.isNull())
  111. {
  112. /* its a syncronous message */
  113. numOfsynchMessages++;
  114. /* print that a message is received */
  115. qDebug() << "received a synchronous event with message:" << message;
  116. /* get the message number */
  117. int aquiredNumber= message.toInt();
  118. /* assert that the message is the expected one */
  119. QVERIFY2(synchMessageExpectedNumber == aquiredNumber,
  120. qPrintable(QString("Expected syncronous message number: %1 got: %2 - order NOT conserved")
  121. .arg(synchMessageExpectedNumber).arg(aquiredNumber)));
  122. /* the next messages of this type should be +1 */
  123. synchMessageExpectedNumber++;
  124. }
  125. else
  126. {
  127. message = event.getProperty("Asynchronous message").toString();
  128. if(!message.isNull())
  129. {
  130. numOfasynchMessages++;
  131. qDebug() << "received an asynchronous event with message:" << message;
  132. /* get the message number */
  133. int aquiredNumber= message.toInt();
  134. /* assert that the message is the expected one */
  135. QVERIFY2(asynchMessageExpectedNumber==aquiredNumber,
  136. qPrintable(QString("Expected asyncronous message number: %1 got: %2 - order NOT conserved")
  137. .arg(asynchMessageExpectedNumber).arg(aquiredNumber)));
  138. /* the next messages of this type should be +1 */
  139. asynchMessageExpectedNumber++;
  140. }
  141. }
  142. /* assert that the messages property is not null */
  143. QVERIFY2(!message.isNull(), "Message should not be null in ");
  144. /* assert that the messages of syncronous type are not too many */
  145. QVERIFY2(numOfsynchMessages < messagesSent+1,
  146. "to many synchronous messages in handleEvent()");
  147. /* assert that the messsage of the asyncronous type are not too many */
  148. QVERIFY2(numOfasynchMessages < messagesSent+1,
  149. "to many asynchronous messages in handleEvent()");
  150. }
  151. catch (const ctkRuntimeException& e)
  152. {
  153. error = true;
  154. exc = e;
  155. throw;
  156. }
  157. catch (...)
  158. {
  159. error = true;
  160. }
  161. }
  162. //----------------------------------------------------------------------------
  163. ctkEAScenario1EventPublisher::ctkEAScenario1EventPublisher(
  164. ctkPluginContext* context, const QString& name,
  165. int id, int numOfMessage, bool useSignalSlot)
  166. : eventAdmin(0), context(context), messageTosend(numOfMessage), useSignalSlot(useSignalSlot)
  167. {
  168. thread.setObjectName(QString("%1-%2").arg(name).arg(id));
  169. moveToThread(&thread);
  170. }
  171. //----------------------------------------------------------------------------
  172. void ctkEAScenario1EventPublisher::runTest()
  173. {
  174. /* Claims the reference of the EventAdmin Service */
  175. serviceReference = context->getServiceReference<ctkEventAdmin>();
  176. /* assert that a reference is aquired */
  177. QVERIFY2(serviceReference, "Should be able to get reference to ctkEventAdmin service");
  178. /* get the service */
  179. eventAdmin = context->getService<ctkEventAdmin>(serviceReference);
  180. /* assert that service is available */
  181. QVERIFY2(eventAdmin, "Should be able to get instance to ctkEventAdmin object");
  182. if (useSignalSlot)
  183. {
  184. eventAdmin->publishSignal(this, SIGNAL(syncSignalEvent(ctkDictionary)), "com/acme/timer", Qt::DirectConnection);
  185. eventAdmin->publishSignal(this, SIGNAL(asyncSignalEvent(ctkDictionary)), "com/acme/timer", Qt::QueuedConnection);
  186. }
  187. /* start the delivery thread */
  188. connect(&thread, SIGNAL(started()), SLOT(sendEvents()));
  189. /* print that the test has started */
  190. qDebug() << "Testing synchronus delivery";
  191. thread.start();
  192. /* wait until thread is dead */
  193. thread.wait();
  194. disconnect(&thread, SIGNAL(started()), this, SLOT(sendEvents()));
  195. connect(&thread, SIGNAL(started()), SLOT(postEvents()));
  196. /* print that the test has started */
  197. qDebug() << "Testing asynchronous delivery";
  198. /* start the test */
  199. thread.start();
  200. /* wait until thread is dead */
  201. thread.wait();
  202. context->ungetService(serviceReference);
  203. QTest::qWait(500); // allow for delivery
  204. }
  205. //----------------------------------------------------------------------------
  206. void ctkEAScenario1EventPublisher::sendEvents()
  207. {
  208. qDebug() << "Starting to send events";
  209. /* deliver the messages */
  210. for (int i = 0; i < messageTosend; i++)
  211. {
  212. /* a Hash table to store message in */
  213. ctkDictionary message;
  214. /* put some properties into the messages */
  215. message.insert("Synchronous message", i);
  216. /* send the message */
  217. ctkEvent event("com/acme/timer", message);
  218. if (useSignalSlot)
  219. {
  220. emit syncSignalEvent(message);
  221. }
  222. else
  223. {
  224. eventAdmin->sendEvent(event);
  225. }
  226. }
  227. thread.quit();
  228. }
  229. //----------------------------------------------------------------------------
  230. void ctkEAScenario1EventPublisher::postEvents()
  231. {
  232. qDebug() << "Starting to post events";
  233. for (int i = 0; i < messageTosend; i++)
  234. {
  235. /* create the hasht table */
  236. ctkDictionary message;
  237. /* create the message */
  238. message.insert("Asynchronous message", i);
  239. /* Sends an asynchronous event to the admin */
  240. ctkEvent event("com/acme/timer", message);
  241. if (useSignalSlot)
  242. {
  243. emit asyncSignalEvent(message);
  244. }
  245. else
  246. {
  247. eventAdmin->postEvent(event);
  248. }
  249. }
  250. thread.quit();
  251. }
  252. //----------------------------------------------------------------------------
  253. ctkEAScenario1TestSuite::ctkEAScenario1TestSuite(ctkPluginContext* context, long eventPluginId, bool useSignalSlot)
  254. : pluginContext(context), MESSAGES_SENT(10), eventPluginId(eventPluginId),
  255. eventConsumer(0), eventPublisher(0), useSignalSlot(useSignalSlot)
  256. {
  257. }
  258. //----------------------------------------------------------------------------
  259. void ctkEAScenario1TestSuite::initTestCase()
  260. {
  261. qDebug() << "Using" << (useSignalSlot ? "" : "no") << "Qt signal/slot mechanism";
  262. pluginContext->getPlugin(eventPluginId)->start();
  263. QStringList scenario1_topics("com/acme/*");
  264. /* add the event consumer to the test suite */
  265. eventConsumer = new ctkEAScenario1EventConsumer(pluginContext,
  266. scenario1_topics, MESSAGES_SENT,
  267. useSignalSlot);
  268. eventPublisher = new ctkEAScenario1EventPublisher(pluginContext,
  269. "Scenario 1 EventPublisher",
  270. 1, MESSAGES_SENT,
  271. useSignalSlot);
  272. }
  273. //----------------------------------------------------------------------------
  274. void ctkEAScenario1TestSuite::cleanupTestCase()
  275. {
  276. eventConsumer->cleanup();
  277. delete eventPublisher;
  278. delete eventConsumer;
  279. pluginContext->getPlugin(eventPluginId)->stop();
  280. }
  281. //----------------------------------------------------------------------------
  282. void ctkEAScenario1TestSuite::testRegisterConsumer()
  283. {
  284. eventConsumer->reset();
  285. eventConsumer->runTest();
  286. }
  287. //----------------------------------------------------------------------------
  288. void ctkEAScenario1TestSuite::testPublishEvents()
  289. {
  290. eventPublisher->runTest();
  291. }