ctkCmdLineModuleFutureTest.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <ctkCmdLineModuleFrontendFactory.h>
  17. #include <ctkCmdLineModuleFrontend.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 "ctkCmdLineModuleBackendLocalProcess.h"
  25. #include <QVariant>
  26. #include <QCoreApplication>
  27. #include <QDebug>
  28. #include <QFutureWatcher>
  29. #include <cstdlib>
  30. class ctkCmdLineModuleFrontendMockupFactory : public ctkCmdLineModuleFrontendFactory
  31. {
  32. public:
  33. virtual ctkCmdLineModuleFrontend* create(const ctkCmdLineModuleReference& moduleRef)
  34. {
  35. struct ModuleFrontendMockup : public ctkCmdLineModuleFrontend
  36. {
  37. ModuleFrontendMockup(const ctkCmdLineModuleReference& moduleRef)
  38. : ctkCmdLineModuleFrontend(moduleRef) {}
  39. virtual QObject* guiHandle() const { return NULL; }
  40. virtual QVariant value(const QString& parameter, int role) const
  41. {
  42. Q_UNUSED(role)
  43. QVariant value = currentValues[parameter];
  44. if (!value.isValid())
  45. return this->moduleReference().description().parameter(parameter).defaultValue();
  46. return value;
  47. }
  48. virtual void setValue(const QString& parameter, const QVariant& value)
  49. {
  50. currentValues[parameter] = value;
  51. }
  52. private:
  53. QHash<QString, QVariant> currentValues;
  54. };
  55. return new ModuleFrontendMockup(moduleRef);
  56. }
  57. virtual QString name() const { return "Mock-up"; }
  58. virtual QString description() const { return "A mock-up factory for testing."; }
  59. };
  60. bool futureTestStartFinish(ctkCmdLineModuleManager* manager, ctkCmdLineModuleFrontend* frontend)
  61. {
  62. qDebug() << "Testing ctkCmdLineModuleFuture start/finish signals.";
  63. QList<QString> expectedSignals;
  64. expectedSignals.push_back("module.started");
  65. expectedSignals.push_back("module.finished");
  66. ctkCmdLineModuleSignalTester signalTester;
  67. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  68. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  69. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  70. ctkCmdLineModuleFuture future = manager->run(frontend);
  71. watcher.setFuture(future);
  72. try
  73. {
  74. future.waitForFinished();
  75. }
  76. catch (const ctkCmdLineModuleRunException& e)
  77. {
  78. qDebug() << e;
  79. return false;
  80. }
  81. // process pending events
  82. QCoreApplication::processEvents();
  83. return signalTester.checkSignals(expectedSignals);
  84. }
  85. bool futureTestProgress(ctkCmdLineModuleManager* manager, ctkCmdLineModuleFrontend* frontend)
  86. {
  87. qDebug() << "Testing ctkCmdLineModuleFuture progress signals.";
  88. QList<QString> expectedSignals;
  89. expectedSignals.push_back("module.started");
  90. // this signal is send when connecting a QFutureWatcher to
  91. // an already started QFuture
  92. expectedSignals.push_back("module.progressValueChanged");
  93. // the following two signals are send when the module reports "filter start"
  94. expectedSignals.push_back("module.progressValueChanged");
  95. expectedSignals.push_back("module.progressTextChanged");
  96. // this signal is send when the module reports progress for "output1"
  97. expectedSignals.push_back("module.progressValueChanged");
  98. // the following two signal are sent at the end to report
  99. // completion and the full standard output text.
  100. expectedSignals.push_back("module.progressValueChanged");
  101. expectedSignals.push_back("module.progressTextChanged");
  102. expectedSignals.push_back("module.finished");
  103. ctkCmdLineModuleSignalTester signalTester;
  104. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  105. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  106. QObject::connect(&watcher, SIGNAL(progressValueChanged(int)), &signalTester, SLOT(moduleProgressValueChanged(int)));
  107. QObject::connect(&watcher, SIGNAL(progressTextChanged(QString)), &signalTester, SLOT(moduleProgressTextChanged(QString)));
  108. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  109. frontend->setValue("numOutputsVar", 1);
  110. ctkCmdLineModuleFuture future = manager->run(frontend);
  111. watcher.setFuture(future);
  112. try
  113. {
  114. future.waitForFinished();
  115. }
  116. catch (const ctkCmdLineModuleRunException& e)
  117. {
  118. qDebug() << e;
  119. return false;
  120. }
  121. // process pending events
  122. QCoreApplication::processEvents();
  123. return signalTester.checkSignals(expectedSignals);
  124. }
  125. bool futureTestPauseAndCancel(ctkCmdLineModuleManager* manager, ctkCmdLineModuleFrontend* frontend)
  126. {
  127. qDebug() << "Testing ctkCmdLineModuleFuture pause and cancel capabilities";
  128. ctkCmdLineModuleSignalTester signalTester;
  129. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  130. QObject::connect(&watcher, SIGNAL(started()), &signalTester, SLOT(moduleStarted()));
  131. QObject::connect(&watcher, SIGNAL(paused()), &signalTester, SLOT(modulePaused()));
  132. QObject::connect(&watcher, SIGNAL(resumed()), &signalTester, SLOT(moduleResumed()));
  133. QObject::connect(&watcher, SIGNAL(canceled()), &signalTester, SLOT(moduleCanceled()));
  134. QObject::connect(&watcher, SIGNAL(finished()), &signalTester, SLOT(moduleFinished()));
  135. frontend->setValue("runtimeVar", 60);
  136. ctkCmdLineModuleFuture future = manager->run(frontend);
  137. watcher.setFuture(future);
  138. QList<QString> expectedSignals;
  139. expectedSignals.push_back("module.started");
  140. if (future.canPause())
  141. {
  142. // Due to Qt bug 12152, these two signals are reversed
  143. expectedSignals.push_back("module.resumed");
  144. expectedSignals.push_back("module.paused");
  145. }
  146. if (future.canCancel())
  147. {
  148. expectedSignals.push_back("module.canceled");
  149. }
  150. expectedSignals.push_back("module.finished");
  151. sleep(1);
  152. QCoreApplication::processEvents();
  153. future.pause();
  154. sleep(1);
  155. QCoreApplication::processEvents();
  156. if (future.canPause())
  157. {
  158. if (!(future.isPaused() && future.isRunning()))
  159. {
  160. qDebug() << "Pause state wrong";
  161. future.setPaused(false);
  162. future.cancel();
  163. QCoreApplication::processEvents();
  164. future.waitForFinished();
  165. return false;
  166. }
  167. }
  168. future.togglePaused();
  169. QCoreApplication::processEvents();
  170. sleep(1);
  171. if (future.isPaused() && future.isRunning())
  172. {
  173. qDebug() << "Pause state wrong (module is paused, but it shouldn't be)";
  174. future.cancel();
  175. QCoreApplication::processEvents();
  176. future.waitForFinished();
  177. return false;
  178. }
  179. try
  180. {
  181. future.cancel();
  182. QCoreApplication::processEvents();
  183. future.waitForFinished();
  184. }
  185. catch (const ctkCmdLineModuleRunException& e)
  186. {
  187. qDebug() << e;
  188. return false;
  189. }
  190. // process pending events
  191. QCoreApplication::processEvents();
  192. if (!signalTester.checkSignals(expectedSignals))
  193. {
  194. return false;
  195. }
  196. if (!(future.isCanceled() && future.isFinished()))
  197. {
  198. qDebug() << "Cancel state wrong";
  199. return false;
  200. }
  201. return true;
  202. }
  203. bool futureTestError(ctkCmdLineModuleManager* manager, ctkCmdLineModuleFrontend* frontend)
  204. {
  205. qDebug() << "Testing ctkCmdLineModuleFuture error reporting.";
  206. frontend->setValue("fileVar", "output1");
  207. frontend->setValue("exitCodeVar", 24);
  208. frontend->setValue("errorTextVar", "Some error occured\n");
  209. QFutureWatcher<ctkCmdLineModuleResult> watcher;
  210. ctkCmdLineModuleFuture future = manager->run(frontend);
  211. watcher.setFuture(future);
  212. try
  213. {
  214. future.waitForFinished();
  215. return EXIT_FAILURE;
  216. }
  217. catch (const ctkCmdLineModuleRunException& e)
  218. {
  219. Q_ASSERT_X(e.errorCode() == 24, __FUNCTION__, "Error code mismatch");
  220. Q_ASSERT_X(e.errorString() == "Some error occured\n", __FUNCTION__, "Error text mismatch");
  221. }
  222. // process pending events
  223. QCoreApplication::processEvents();
  224. return true;
  225. }
  226. int ctkCmdLineModuleFutureTest(int argc, char* argv[])
  227. {
  228. QCoreApplication app(argc, argv);
  229. ctkCmdLineModuleFrontendMockupFactory factory;
  230. ctkCmdLineModuleBackendLocalProcess backend;
  231. ctkCmdLineModuleManager manager;
  232. manager.registerBackend(&backend);
  233. QUrl moduleUrl = QUrl::fromLocalFile(app.applicationDirPath() + "/ctkCmdLineModuleTestBed");
  234. ctkCmdLineModuleReference moduleRef;
  235. try
  236. {
  237. moduleRef = manager.registerModule(moduleUrl);
  238. }
  239. catch (const ctkException& e)
  240. {
  241. qCritical() << "Module at" << moduleUrl << "could not be registered: " << e;
  242. }
  243. {
  244. QScopedPointer<ctkCmdLineModuleFrontend> frontend(factory.create(moduleRef));
  245. if (!futureTestStartFinish(&manager, frontend.data()))
  246. {
  247. return EXIT_FAILURE;
  248. }
  249. }
  250. {
  251. QScopedPointer<ctkCmdLineModuleFrontend> frontend(factory.create(moduleRef));
  252. if (!futureTestProgress(&manager, frontend.data()))
  253. {
  254. return EXIT_FAILURE;
  255. }
  256. }
  257. {
  258. QScopedPointer<ctkCmdLineModuleFrontend> frontend(factory.create(moduleRef));
  259. if (!futureTestError(&manager, frontend.data()))
  260. {
  261. return EXIT_FAILURE;
  262. }
  263. }
  264. {
  265. QScopedPointer<ctkCmdLineModuleFrontend> frontend(factory.create(moduleRef));
  266. if (!futureTestPauseAndCancel(&manager, frontend.data()))
  267. {
  268. return EXIT_FAILURE;
  269. }
  270. }
  271. // if (!futureTestResultReady(frontend.data()))
  272. // {
  273. // return EXIT_FAILURE;
  274. // }
  275. return EXIT_SUCCESS;
  276. }