ctkCmdLineModuleFutureTest.cpp 10 KB

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