ctkEAScenario4TestSuite.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "ctkEAScenario4TestSuite_p.h"
  16. #include <ctkPluginContext.h>
  17. #include <service/event/ctkEventConstants.h>
  18. #include <service/event/ctkEventAdmin.h>
  19. #include <QTest>
  20. //----------------------------------------------------------------------------
  21. ctkEAScenario4EventConsumer::ctkEAScenario4EventConsumer(
  22. ctkPluginContext* pluginContext, const QStringList topics,
  23. int numSyncMsg, int numAsyncMsg, const QString& filter)
  24. : context(pluginContext), topicsToConsume(topics), filterToConsume(filter),
  25. asynchMessages(0), synchMessages(0), numSyncMessages(numSyncMsg),
  26. numAsyncMessages(numAsyncMsg), error(false), exc("")
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. void ctkEAScenario4EventConsumer::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. if (!filterToConsume.isEmpty())
  39. {
  40. props.insert(ctkEventConstants::EVENT_FILTER, filterToConsume);
  41. }
  42. /* register the service */
  43. serviceRegistration = context->registerService<ctkEventHandler>(this, props);
  44. QVERIFY2(serviceRegistration, "service registration should not be null");
  45. }
  46. //----------------------------------------------------------------------------
  47. void ctkEAScenario4EventConsumer::cleanup()
  48. {
  49. QMutexLocker l(&mutex);
  50. try
  51. {
  52. serviceRegistration.unregister();
  53. }
  54. catch (const ctkIllegalStateException&) {}
  55. if (error)
  56. {
  57. throw exc;
  58. }
  59. QCOMPARE(synchMessages, numSyncMessages); // "Not all synch messages recieved"
  60. QCOMPARE(asynchMessages, numAsyncMessages); // "Not all asynch messages recieved"
  61. }
  62. //----------------------------------------------------------------------------
  63. void ctkEAScenario4EventConsumer::handleEvent(const ctkEvent& event)
  64. {
  65. QMutexLocker l(&mutex);
  66. try
  67. {
  68. /* try to get the message */
  69. QString message = event.getProperty("Synchronous message").toString();
  70. if (!message.isNull())
  71. {
  72. /* its an asyncronous message */
  73. synchMessages++;
  74. qDebug() << "received a Synchronous event with message:" << message;
  75. }
  76. else
  77. {
  78. message = event.getProperty("Asynchronous message").toString();
  79. if (!message.isNull())
  80. {
  81. asynchMessages++;
  82. qDebug() << "received an Asynchronus event with message:" << message;
  83. }
  84. }
  85. /* assert that the messages property is not null */
  86. QVERIFY2(!message.isNull(), "Message should not be null in handleEvent()");
  87. /* assert that the messages of syncronous type are not too many */
  88. QVERIFY2(synchMessages < numSyncMessages + 1, "too many synchronous messages");
  89. /* assert that the messsage of the asyncronous type are not too many */
  90. QVERIFY2(asynchMessages < numAsyncMessages + 1, "too many asynchronous messages");
  91. }
  92. catch (const ctkRuntimeException& e)
  93. {
  94. error = true;
  95. exc = e;
  96. throw e;
  97. }
  98. catch (...)
  99. {
  100. error = true;
  101. }
  102. }
  103. //----------------------------------------------------------------------------
  104. void ctkEAScenario4EventPublisher::sendEvents()
  105. {
  106. /* a Hash table to store message in */
  107. ctkDictionary message(propertiesToSend);
  108. for (int i = 0; i < messageTosend; i++)
  109. {
  110. /* put some properties into the messages */
  111. message.insert("Synchronous message", i);
  112. /* send the message */
  113. qDebug() << "sending a synchronous event with message:"
  114. << message << "and the topic:" << topicToSend;
  115. eventAdmin->sendEvent(ctkEvent(topicToSend, message));
  116. }
  117. thread.quit();
  118. }
  119. //----------------------------------------------------------------------------
  120. void ctkEAScenario4EventPublisher::postEvents()
  121. {
  122. /* create the hash table */
  123. ctkDictionary message(propertiesToSend);
  124. for (int i = 0; i < messageTosend; i++)
  125. {
  126. /* create the message */
  127. message.insert("Asynchronous message", i);
  128. /* Sends a asynchronous event to the admin */
  129. qDebug() << "sending an Asynchronous event with message:"
  130. << message << "and the topic:" << topicToSend;
  131. eventAdmin->postEvent(ctkEvent(topicToSend, message));
  132. }
  133. thread.quit();
  134. }
  135. //----------------------------------------------------------------------------
  136. ctkEAScenario4EventPublisher::ctkEAScenario4EventPublisher(ctkPluginContext* context, const QString& name,
  137. const QString& topic, const ctkDictionary& props,
  138. int id, int numOfMessage)
  139. : eventAdmin(0), context(context), messageTosend(numOfMessage), topicToSend(topic), propertiesToSend(props)
  140. {
  141. thread.setObjectName(QString("%1-%2").arg(name).arg(id));
  142. moveToThread(&thread);
  143. }
  144. //----------------------------------------------------------------------------
  145. void ctkEAScenario4EventPublisher::runTest()
  146. {
  147. /* Claims the reference of the EventAdmin Service */
  148. serviceReference = context->getServiceReference<ctkEventAdmin>();
  149. /* assert that a reference is aquired */
  150. QVERIFY2(serviceReference, "Should be able to get reference to ctkEventAdmin service");
  151. eventAdmin = context->getService<ctkEventAdmin>(serviceReference);
  152. QVERIFY2(eventAdmin, "Should be able to get instance to ctkEventAdmin object");
  153. connect(&thread, SIGNAL(started()), SLOT(sendEvents()));
  154. thread.start();
  155. /* wait until thread is dead */
  156. thread.wait();
  157. disconnect(&thread, SIGNAL(started()), this, SLOT(sendEvents()));
  158. connect(&thread, SIGNAL(started()), SLOT(postEvents()));
  159. thread.start();
  160. /* wait until thread is dead */
  161. thread.wait();
  162. QTest::qWait(1000); // allow for delivery
  163. }
  164. //----------------------------------------------------------------------------
  165. ctkEAScenario4TestSuite::ctkEAScenario4TestSuite(ctkPluginContext* context, long eventPluginId)
  166. : pluginContext(context), eventPluginId(eventPluginId)
  167. {
  168. }
  169. //----------------------------------------------------------------------------
  170. void ctkEAScenario4TestSuite::initTestCase()
  171. {
  172. pluginContext->getPlugin(eventPluginId)->start();
  173. /* keys and properties to be used in the EventProducers*/
  174. ctkDictionary keysAndProps1;
  175. keysAndProps1.insert("year", "2004");
  176. keysAndProps1.insert("month", "12");
  177. ctkDictionary keysAndProps2;
  178. keysAndProps2.insert("year", "2005");
  179. keysAndProps2.insert("month", "12");
  180. ctkDictionary keysAndProps3;
  181. keysAndProps3.insert("YEAR", "2005");
  182. keysAndProps3.insert("month", "11"); // Won't year filters match because year is not present?
  183. /*Topics to be used in the EventConsumers*/
  184. QStringList scenario4_topics1("com/acme/timer");
  185. /*Filters to be used in the EventConsumers*/
  186. QString scenario4_filter1 = "(year=2004)";
  187. QString scenario4_filter2 = "(year=2005)";
  188. QString scenario4_filter3 = "(year:2004)";
  189. QString scenario4_filter4;
  190. QString scenario4_filter5 = "(month=12)";
  191. /* add the event consumer with the correct topics to the test suite */
  192. eventConsumers.push_back(new ctkEAScenario4EventConsumer(pluginContext, scenario4_topics1,
  193. 1, 1, scenario4_filter1));
  194. eventConsumers.push_back(new ctkEAScenario4EventConsumer(pluginContext, scenario4_topics1,
  195. 2, 2, scenario4_filter2));
  196. eventConsumers.push_back(new ctkEAScenario4EventConsumer(pluginContext, scenario4_topics1,
  197. 0, 0, scenario4_filter3));
  198. eventConsumers.push_back(new ctkEAScenario4EventConsumer(pluginContext, scenario4_topics1,
  199. 3, 3, scenario4_filter4));
  200. eventConsumers.push_back(new ctkEAScenario4EventConsumer(pluginContext, scenario4_topics1,
  201. 2, 2, scenario4_filter5));
  202. /* add the event publisher to the test suite */
  203. eventPublishers.push_back(new ctkEAScenario4EventPublisher(pluginContext, "Scenario 4 EventPublisher1",
  204. "com/acme/timer", keysAndProps1, 4, 1));
  205. eventPublishers.push_back(new ctkEAScenario4EventPublisher(pluginContext, "Scenario 4 EventPublisher2",
  206. "com/acme/timer", keysAndProps2, 4, 1));
  207. eventPublishers.push_back(new ctkEAScenario4EventPublisher(pluginContext, "Scenario 4 EventPublisher3",
  208. "com/acme/timer", keysAndProps3, 4, 1));
  209. }
  210. //----------------------------------------------------------------------------
  211. void ctkEAScenario4TestSuite::cleanupTestCase()
  212. {
  213. foreach(ctkEAScenario4EventConsumer* eventConsumer, eventConsumers)
  214. {
  215. eventConsumer->cleanup();
  216. }
  217. qDeleteAll(eventPublishers);
  218. qDeleteAll(eventConsumers);
  219. pluginContext->getPlugin(eventPluginId)->stop();
  220. }
  221. //----------------------------------------------------------------------------
  222. void ctkEAScenario4TestSuite::testRegisterConsumer()
  223. {
  224. foreach(ctkEAScenario4EventConsumer* consumer, eventConsumers)
  225. {
  226. consumer->runTest();
  227. }
  228. }
  229. //----------------------------------------------------------------------------
  230. void ctkEAScenario4TestSuite::testPublishEvents()
  231. {
  232. foreach(ctkEAScenario4EventPublisher* publisher, eventPublishers)
  233. {
  234. publisher->runTest();
  235. }
  236. }