ctkEAScenario3TestSuite.cpp 7.9 KB

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