ctkCmdLineModuleFutureTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <ctkCmdLineModuleManager.h>
  16. #include <ctkCmdLineModuleFactory.h>
  17. #include <ctkCmdLineModule.h>
  18. #include <ctkCmdLineModuleReference.h>
  19. #include <ctkCmdLineModuleDescription.h>
  20. #include <ctkCmdLineModuleParameter.h>
  21. #include <ctkCmdLineModuleRunException.h>
  22. #include <ctkCmdLineModuleFuture.h>
  23. #include "ctkCmdLineModuleSignalTester.h"
  24. #include <QVariant>
  25. #include <QCoreApplication>
  26. #include <QDebug>
  27. #include <QFutureWatcher>
  28. #include <cstdlib>
  29. class ctkCmdLineModuleTestInstanceFactory : public ctkCmdLineModuleFactory
  30. {
  31. public:
  32. virtual ctkCmdLineModule* create(const ctkCmdLineModuleReference& moduleRef)
  33. {
  34. struct ModuleTestInstance : public ctkCmdLineModule
  35. {
  36. ModuleTestInstance(const ctkCmdLineModuleReference& moduleRef) : ctkCmdLineModule(moduleRef) {}
  37. virtual QObject* guiHandle() const { return NULL; }
  38. virtual QVariant value(const QString& parameter) const
  39. {
  40. QVariant value = currentValues[parameter];
  41. if (!value.isValid())
  42. return this->moduleReference().description().parameter(parameter).defaultValue();
  43. return value;
  44. }
  45. virtual void setValue(const QString& parameter, const QVariant& value)
  46. {
  47. currentValues[parameter] = value;
  48. }
  49. private:
  50. QHash<QString, QVariant> currentValues;
  51. };
  52. return new ModuleTestInstance(moduleRef);
  53. }
  54. };
  55. bool futureTestStartFinish(ctkCmdLineModule* module)
  56. {
  57. qDebug() << "Testing ctkCmdLineModuleFuture start/finish signals.";
  58. QList<QString> expectedSignals;
  59. expectedSignals.push_back("module.started");
  60. expectedSignals.push_back("module.finished");
  61. ctkCmdLineModuleSignalTester signalTester;
  62. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  63. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  64. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  65. qDebug() << module->commandLineArguments();
  66. ctkCmdLineModuleFuture future = module->run();
  67. watcher.setFuture(future);
  68. try
  69. {
  70. future.waitForFinished();
  71. }
  72. catch (const ctkCmdLineModuleRunException& e)
  73. {
  74. qDebug() << e;
  75. return false;
  76. }
  77. // process pending events
  78. QCoreApplication::processEvents();
  79. return signalTester.checkSignals(expectedSignals);
  80. }
  81. bool futureTestProgress(ctkCmdLineModule* module)
  82. {
  83. qDebug() << "Testing ctkCmdLineModuleFuture progress signals.";
  84. QList<QString> expectedSignals;
  85. expectedSignals.push_back("module.started");
  86. // this signal is send when connecting a QFutureWatcher to
  87. // an already started QFuture
  88. expectedSignals.push_back("module.progressValueChanged");
  89. // the following two signals are send when the module reports "filter start"
  90. expectedSignals.push_back("module.progressValueChanged");
  91. expectedSignals.push_back("module.progressTextChanged");
  92. // this signal is send when the module reports progress for "output1"
  93. expectedSignals.push_back("module.progressValueChanged");
  94. // the following two signal are sent at the end to report
  95. // completion and the full standard output text.
  96. expectedSignals.push_back("module.progressValueChanged");
  97. expectedSignals.push_back("module.progressTextChanged");
  98. expectedSignals.push_back("module.finished");
  99. ctkCmdLineModuleSignalTester signalTester;
  100. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  101. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  102. QObject::connect(&watcher, SIGNAL(progressValueChanged(int)), &signalTester, SLOT(moduleProgressValueChanged(int)));
  103. QObject::connect(&watcher, SIGNAL(progressTextChanged(QString)), &signalTester, SLOT(moduleProgressTextChanged(QString)));
  104. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  105. module->setValue("numOutputsVar", 1);
  106. ctkCmdLineModuleFuture future = module->run();
  107. watcher.setFuture(future);
  108. try
  109. {
  110. future.waitForFinished();
  111. }
  112. catch (const ctkCmdLineModuleRunException& e)
  113. {
  114. qDebug() << e;
  115. return false;
  116. }
  117. // process pending events
  118. QCoreApplication::processEvents();
  119. return signalTester.checkSignals(expectedSignals);
  120. }
  121. bool futureTestPauseAndCancel(ctkCmdLineModule* module)
  122. {
  123. qDebug() << "Testing ctkCmdLineModuleFuture pause and cancel capabilities";
  124. QList<QString> expectedSignals;
  125. expectedSignals.push_back("module.started");
  126. expectedSignals.push_back("module.finished");
  127. ctkCmdLineModuleSignalTester signalTester;
  128. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  129. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  130. QObject::connect(&watcher, SIGNAL(progressValueChanged(int)), &signalTester, SLOT(moduleProgressValueChanged(int)));
  131. QObject::connect(&watcher, SIGNAL(progressTextChanged(QString)), &signalTester, SLOT(moduleProgressTextChanged(QString)));
  132. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  133. module->setValue("runtimeVar", 60);
  134. ctkCmdLineModuleFuture future = module->run();
  135. watcher.setFuture(future);
  136. try
  137. {
  138. future.cancel();
  139. future.waitForFinished();
  140. }
  141. catch (const ctkCmdLineModuleRunException& e)
  142. {
  143. qDebug() << e;
  144. return false;
  145. }
  146. // process pending events
  147. QCoreApplication::processEvents();
  148. if (!signalTester.checkSignals(expectedSignals))
  149. {
  150. return false;
  151. }
  152. if (!(future.isCanceled() && future.isFinished()))
  153. {
  154. qDebug() << "Cancel state wrong";
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool futureTestError(ctkCmdLineModule* module)
  160. {
  161. qDebug() << "Testing ctkCmdLineModuleFuture error reporting.";
  162. module->setValue("fileVar", "output1");
  163. module->setValue("exitCodeVar", 24);
  164. module->setValue("errorTextVar", "Some error occured\n");
  165. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  166. ctkCmdLineModuleFuture future = module->run();
  167. watcher.setFuture(future);
  168. try
  169. {
  170. future.waitForFinished();
  171. return EXIT_FAILURE;
  172. }
  173. catch (const ctkCmdLineModuleRunException& e)
  174. {
  175. Q_ASSERT_X(e.errorCode() == 24, __FUNCTION__, "Error code mismatch");
  176. Q_ASSERT_X(e.errorString() == "Some error occured\n", __FUNCTION__, "Error text mismatch");
  177. }
  178. // process pending events
  179. QCoreApplication::processEvents();
  180. return true;
  181. }
  182. int ctkCmdLineModuleFutureTest(int argc, char* argv[])
  183. {
  184. QCoreApplication app(argc, argv);
  185. ctkCmdLineModuleTestInstanceFactory factory;
  186. ctkCmdLineModuleManager manager(&factory);
  187. QString moduleFilename = app.applicationDirPath() + "/ctkCmdLineModuleTestBed";
  188. ctkCmdLineModuleReference moduleRef = manager.registerModule(moduleFilename);
  189. if (!moduleRef)
  190. {
  191. qCritical() << "Module at" << moduleFilename << "could not be registered";
  192. }
  193. {
  194. QScopedPointer<ctkCmdLineModule> module(manager.createModule(moduleRef));
  195. if (!futureTestStartFinish(module.data()))
  196. {
  197. return EXIT_FAILURE;
  198. }
  199. }
  200. {
  201. QScopedPointer<ctkCmdLineModule> module(manager.createModule(moduleRef));
  202. if (!futureTestProgress(module.data()))
  203. {
  204. return EXIT_FAILURE;
  205. }
  206. }
  207. {
  208. QScopedPointer<ctkCmdLineModule> module(manager.createModule(moduleRef));
  209. if (!futureTestError(module.data()))
  210. {
  211. return EXIT_FAILURE;
  212. }
  213. }
  214. {
  215. QScopedPointer<ctkCmdLineModule> module(manager.createModule(moduleRef));
  216. if (!futureTestPauseAndCancel(module.data()))
  217. {
  218. return EXIT_FAILURE;
  219. }
  220. }
  221. // if (!futureTestResultReady(module.data()))
  222. // {
  223. // return EXIT_FAILURE;
  224. // }
  225. return EXIT_SUCCESS;
  226. }