ctkEAScenario2TestSuite.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "ctkEAScenario2TestSuite_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <service/event/ctkEventConstants.h>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <QTest>
  20. //----------------------------------------------------------------------------
  21. ctkEAScenario2EventConsumer::ctkEAScenario2EventConsumer(
  22. ctkPluginContext* pluginContext, const QStringList& topics,
  23. int numSyncMsg, int numAsyncMsg)
  24. : context(pluginContext),topicsToConsume(topics),
  25. asynchMessages(0), synchMessages(0), numSyncMessages(numSyncMsg),
  26. numAsyncMessages(numAsyncMsg), error(false), exc("")
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. void ctkEAScenario2EventConsumer::runTest()
  31. {
  32. asynchMessages = 0;
  33. synchMessages = 0;
  34. /* create the hashtable to put properties in */
  35. ctkDictionary props;
  36. /* put service.pid property in hashtable */
  37. props.insert(ctkEventConstants::EVENT_TOPIC, topicsToConsume);
  38. /* register the service */
  39. serviceRegistration = context->registerService<ctkEventHandler>(this, props);
  40. QVERIFY2(serviceRegistration, "service registration should not be invalid");
  41. }
  42. //----------------------------------------------------------------------------
  43. void ctkEAScenario2EventConsumer::cleanup()
  44. {
  45. QMutexLocker l(&mutex);
  46. try
  47. {
  48. serviceRegistration.unregister();
  49. }
  50. catch (const ctkIllegalStateException&)
  51. {}
  52. if (error)
  53. {
  54. throw exc;
  55. }
  56. QCOMPARE(synchMessages, numSyncMessages); // "Not all synch messages recieved"
  57. QCOMPARE(asynchMessages, numAsyncMessages); // "Not all asynch messages recieved"
  58. }
  59. //----------------------------------------------------------------------------
  60. void ctkEAScenario2EventConsumer::handleEvent(const ctkEvent& event)
  61. {
  62. QMutexLocker l(&mutex);
  63. try
  64. {
  65. /* get the topic from the event*/
  66. // QString eventTopic = event.getTopic();
  67. //TODO security topic permission
  68. // /* make a topic permission from the received topic in order to check it*/
  69. // TopicPermission permissionAccuired = new TopicPermission(eventTopic, "SUBSCRIBE");
  70. // /* make a topic permission from the topic to consume in order to check it*/
  71. // TopicPermission actualPermission = new TopicPermission(topicsToConsume[0], "SUBSCRIBE");
  72. // /* assert if the topic in the event is the same as the topic to listen fore including wildcard */
  73. // assertTrue("The topics was not equal", actualPermission.implies(permissionAccuired));
  74. /* try to get the message */
  75. QString message = event.getProperty("Synchronous message").toString();
  76. if(!message.isNull())
  77. {
  78. /* its a syncronous message */
  79. qDebug() << "received a synchronous event with message:" << message;
  80. /* assert that the messages of syncronous type are not too many */
  81. QVERIFY2(synchMessages < numSyncMessages, "too many synchronous messages");
  82. synchMessages++;
  83. qDebug() << "Max number of Sync messages is:" << numSyncMessages << "and number of received Sync messages is: " << synchMessages;
  84. }
  85. else
  86. {
  87. message = event.getProperty("Asynchronous message").toString();
  88. if(!message.isNull())
  89. {
  90. qDebug() << "received an asynchronous event with message:" << message;
  91. /* assert that the messsage of the asyncronous type are not too many */
  92. QVERIFY2(asynchMessages < numAsyncMessages, "too many asynchronous messages");
  93. asynchMessages++;
  94. qDebug() << "Max number of Async messages is:" << numAsyncMessages << "and number of received Async messages is:" << asynchMessages;
  95. }
  96. }
  97. }
  98. catch (const ctkRuntimeException& e)
  99. {
  100. error = true;
  101. exc = e;
  102. throw e;
  103. }
  104. catch (...)
  105. {
  106. error = true;
  107. }
  108. }
  109. //----------------------------------------------------------------------------
  110. ctkEAScenario2EventPublisher::ctkEAScenario2EventPublisher(ctkPluginContext* context,
  111. const QString& name, const QStringList topics,
  112. int id, int numOfMessage)
  113. : eventAdmin(0), context(context),
  114. messageTosend(numOfMessage), topicsToSend(topics)
  115. {
  116. thread.setObjectName(QString("%1-%2").arg(name).arg(id));
  117. moveToThread(&thread);
  118. }
  119. //----------------------------------------------------------------------------
  120. void ctkEAScenario2EventPublisher::runTest()
  121. {
  122. /* Claims the reference of the EventAdmin Service */
  123. serviceReference = context->getServiceReference<ctkEventAdmin>();
  124. /* assert that a reference is aquired */
  125. QVERIFY2(serviceReference, "Should be able to get reference to ctkEventAdmin service");
  126. eventAdmin = context->getService<ctkEventAdmin>(serviceReference);
  127. QVERIFY2(eventAdmin, "Should be able to get instance to ctkEventAdmin object");
  128. connect(&thread, SIGNAL(started()), SLOT(sendEvents()));
  129. thread.start();
  130. /* wait until thread is dead */
  131. thread.wait();
  132. disconnect(&thread, SIGNAL(started()), this, SLOT(sendEvents()));
  133. connect(&thread, SIGNAL(started()), SLOT(postEvents()));
  134. thread.start();
  135. /* wait until thread is dead */
  136. thread.wait();
  137. context->ungetService(serviceReference);
  138. QTest::qWait(1000); // allow for delivery
  139. }
  140. //----------------------------------------------------------------------------
  141. void ctkEAScenario2EventPublisher::sendEvents()
  142. {
  143. for (int i = 0; i < messageTosend; i++)
  144. {
  145. try
  146. {
  147. /* a Hash table to store message in */
  148. ctkDictionary message;
  149. /* put some properties into the messages */
  150. message.insert("Synchronous message",i);
  151. /* send the message */
  152. qDebug() << " sending a synchronous event with message:"
  153. << message << "and the topic:" << topicsToSend[i];
  154. eventAdmin->sendEvent(ctkEvent(topicsToSend[i], message));
  155. }
  156. catch(const std::exception& e)
  157. {
  158. qDebug() << "Error when publishing synhronous:" << e.what();
  159. }
  160. }
  161. thread.quit();
  162. }
  163. //----------------------------------------------------------------------------
  164. void ctkEAScenario2EventPublisher::postEvents()
  165. {
  166. for (int i = 0; i < messageTosend; i++)
  167. {
  168. try
  169. {
  170. /* create the hasht table */
  171. ctkDictionary message;
  172. /* create the message */
  173. message.insert("Asynchronous message",i);
  174. /* Sends an asynchronous event to the admin */
  175. qDebug() << "sending an Asynchronous event with message:"
  176. << message << "and the topic:" << topicsToSend[i];
  177. eventAdmin->postEvent(ctkEvent(topicsToSend[i], message));
  178. }
  179. catch(const std::exception& e)
  180. {
  181. qDebug() << "Error when publishing asynhronous:" << e.what();
  182. }
  183. }
  184. thread.quit();
  185. }
  186. //----------------------------------------------------------------------------
  187. ctkEAScenario2TestSuite::ctkEAScenario2TestSuite(ctkPluginContext* context, long eventPluginId)
  188. : pluginContext(context), eventPluginId(eventPluginId)
  189. {
  190. }
  191. //----------------------------------------------------------------------------
  192. void ctkEAScenario2TestSuite::initTestCase()
  193. {
  194. pluginContext->getPlugin(eventPluginId)->start();
  195. /* create a topic string */
  196. QStringList scenario2_topics1("com/acme/timer");
  197. QStringList scenario2_topics2("com/*");
  198. QStringList scenario2_topicsToPublish;
  199. scenario2_topicsToPublish << "";
  200. scenario2_topicsToPublish << "com/AcMe/TiMeR";
  201. scenario2_topicsToPublish << "com.acme.timer";
  202. scenario2_topicsToPublish << "com/acme/timer";
  203. eventConsumers.push_back(new ctkEAScenario2EventConsumer(
  204. pluginContext, scenario2_topics1,
  205. 1,1));
  206. eventConsumers.push_back(new ctkEAScenario2EventConsumer(
  207. pluginContext, scenario2_topics2,
  208. 2,2));
  209. eventPublisher = new ctkEAScenario2EventPublisher(
  210. pluginContext, "Scenario 2 EventPublisher",
  211. scenario2_topicsToPublish, 2, 4);
  212. }
  213. //----------------------------------------------------------------------------
  214. void ctkEAScenario2TestSuite::cleanupTestCase()
  215. {
  216. foreach (ctkEAScenario2EventConsumer* consumer, eventConsumers)
  217. {
  218. consumer->cleanup();
  219. }
  220. delete eventPublisher;
  221. qDeleteAll(eventConsumers);
  222. pluginContext->getPlugin(eventPluginId)->stop();
  223. }
  224. //----------------------------------------------------------------------------
  225. void ctkEAScenario2TestSuite::testRegisterConsumer()
  226. {
  227. foreach(ctkEAScenario2EventConsumer* consumer, eventConsumers)
  228. {
  229. consumer->runTest();
  230. }
  231. }
  232. //----------------------------------------------------------------------------
  233. void ctkEAScenario2TestSuite::testPublishEvents()
  234. {
  235. eventPublisher->runTest();
  236. }