ctkCmdLineModuleFutureTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <ctkCmdLineModuleFutureWatcher.h>
  24. #include "ctkCmdLineModuleSignalTester.h"
  25. #include "ctkCmdLineModuleBackendLocalProcess.h"
  26. #include "ctkTest.h"
  27. #include <QVariant>
  28. #include <QCoreApplication>
  29. #include <QDebug>
  30. #include <QFutureWatcher>
  31. //-----------------------------------------------------------------------------
  32. class ctkCmdLineModuleFrontendMockupFactory : public ctkCmdLineModuleFrontendFactory
  33. {
  34. public:
  35. virtual ctkCmdLineModuleFrontend* create(const ctkCmdLineModuleReference& moduleRef)
  36. {
  37. struct ModuleFrontendMockup : public ctkCmdLineModuleFrontend
  38. {
  39. ModuleFrontendMockup(const ctkCmdLineModuleReference& moduleRef)
  40. : ctkCmdLineModuleFrontend(moduleRef) {}
  41. virtual QObject* guiHandle() const { return NULL; }
  42. virtual QVariant value(const QString& parameter, int role) const
  43. {
  44. Q_UNUSED(role)
  45. QVariant value = currentValues[parameter];
  46. if (!value.isValid())
  47. return this->moduleReference().description().parameter(parameter).defaultValue();
  48. return value;
  49. }
  50. virtual void setValue(const QString& parameter, const QVariant& value, int role = DisplayRole)
  51. {
  52. Q_UNUSED(role)
  53. currentValues[parameter] = value;
  54. }
  55. private:
  56. QHash<QString, QVariant> currentValues;
  57. };
  58. return new ModuleFrontendMockup(moduleRef);
  59. }
  60. virtual QString name() const { return "Mock-up"; }
  61. virtual QString description() const { return "A mock-up factory for testing."; }
  62. };
  63. //-----------------------------------------------------------------------------
  64. class ctkCmdLineModuleFutureTester : public QObject
  65. {
  66. Q_OBJECT
  67. public Q_SLOTS:
  68. void ouputDataReady();
  69. void errorDataReady();
  70. private Q_SLOTS:
  71. void initTestCase();
  72. void init();
  73. void cleanup();
  74. void testStartFinish();
  75. void testProgress();
  76. void testPauseAndCancel();
  77. void testOutput();
  78. void testError();
  79. private:
  80. QByteArray outputData;
  81. QByteArray errorData;
  82. ctkCmdLineModuleFutureWatcher* currentWatcher;
  83. ctkCmdLineModuleFrontendMockupFactory factory;
  84. ctkCmdLineModuleBackendLocalProcess backend;
  85. ctkCmdLineModuleManager manager;
  86. ctkCmdLineModuleReference moduleRef;
  87. ctkCmdLineModuleFrontend* frontend;
  88. };
  89. //-----------------------------------------------------------------------------
  90. void ctkCmdLineModuleFutureTester::ouputDataReady()
  91. {
  92. if (this->currentWatcher)
  93. {
  94. outputData.append(currentWatcher->readPendingOutputData());
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. void ctkCmdLineModuleFutureTester::errorDataReady()
  99. {
  100. if (this->currentWatcher)
  101. {
  102. errorData.append(currentWatcher->readPendingErrorData());
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void ctkCmdLineModuleFutureTester::initTestCase()
  107. {
  108. manager.registerBackend(&backend);
  109. QUrl moduleUrl = QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + "/ctkCmdLineModuleTestBed");
  110. moduleRef = manager.registerModule(moduleUrl);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void ctkCmdLineModuleFutureTester::init()
  114. {
  115. currentWatcher = 0;
  116. frontend = factory.create(moduleRef);
  117. }
  118. //-----------------------------------------------------------------------------
  119. void ctkCmdLineModuleFutureTester::cleanup()
  120. {
  121. delete frontend;
  122. outputData.clear();
  123. errorData.clear();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkCmdLineModuleFutureTester::testStartFinish()
  127. {
  128. QList<QString> expectedSignals;
  129. expectedSignals << "module.started"
  130. // the following signals are send when connecting a QFutureWatcher to
  131. // an already started QFuture
  132. << "module.progressRangeChanged(0,0)"
  133. << "module.progressValueChanged(0)"
  134. << "module.progressRangeChanged(0,1002)"
  135. // the test module always reports error data when starting
  136. << "module.errorReady"
  137. // the following two signals are send when the module reports "filter start"
  138. << "module.progressValueChanged(1)"
  139. << "module.progressTextChanged(Does nothing useful)"
  140. // imageOutput result
  141. << "module.resultReadyAt(0,1)"
  142. << "module.resultReadyAt(0)"
  143. // exitStatusOutput result
  144. << "module.resultReadyAt(1,2)"
  145. << "module.resultReadyAt(1)"
  146. // <filter-end> progress value and text
  147. << "module.progressValueChanged(1001)"
  148. << "module.progressTextChanged(Finished successfully.)"
  149. // the following signal is sent at the end to report completion
  150. << "module.progressValueChanged(1002)"
  151. << "module.finished";
  152. ctkCmdLineModuleSignalTester signalTester;
  153. ctkCmdLineModuleFuture future = manager.run(frontend);
  154. signalTester.setFuture(future);
  155. future.waitForFinished();
  156. QCoreApplication::processEvents();
  157. QVERIFY(signalTester.checkSignals(expectedSignals));
  158. QList<ctkCmdLineModuleResult> results;
  159. results << ctkCmdLineModuleResult("imageOutput", "/tmp/out.nrrd");
  160. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  161. QCOMPARE(signalTester.results(), results);
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkCmdLineModuleFutureTester::testProgress()
  165. {
  166. QList<QString> expectedSignals;
  167. expectedSignals << "module.started"
  168. // the following signals are send when connecting a QFutureWatcher to
  169. // an already started QFuture
  170. << "module.progressRangeChanged(0,0)"
  171. << "module.progressValueChanged(0)"
  172. << "module.progressRangeChanged(0,1002)"
  173. // the test module always reports error data when starting
  174. << "module.errorReady"
  175. // the following two signals are send when the module reports "filter start"
  176. << "module.progressValueChanged(1)"
  177. << "module.progressTextChanged(Does nothing useful)"
  178. // the output data on the standard output channel
  179. << "module.outputReady"
  180. // this signal is send when the module reports progress for "output1"
  181. << "module.progressValueChanged(1000)"
  182. << "module.progressTextChanged(Calculating output 2...)"
  183. // first resultNumberOutput result
  184. << "module.resultReadyAt(0,1)"
  185. << "module.resultReadyAt(0)"
  186. // imageOutput result
  187. << "module.resultReadyAt(1,2)"
  188. << "module.resultReadyAt(1)"
  189. // exitStatusOutput result
  190. << "module.resultReadyAt(2,3)"
  191. << "module.resultReadyAt(2)"
  192. // <filter-end> progress value and text
  193. << "module.progressValueChanged(1001)"
  194. << "module.progressTextChanged(Finished successfully.)"
  195. // the following signal is sent at the end to report completion
  196. << "module.progressValueChanged(1002)"
  197. << "module.finished";
  198. ctkCmdLineModuleSignalTester signalTester;
  199. frontend->setValue("numOutputsVar", 1);
  200. ctkCmdLineModuleFuture future = manager.run(frontend);
  201. signalTester.setFuture(future);
  202. future.waitForFinished();
  203. // process pending events
  204. QCoreApplication::processEvents();
  205. QVERIFY(signalTester.checkSignals(expectedSignals));
  206. QList<ctkCmdLineModuleResult> results;
  207. results << ctkCmdLineModuleResult("resultNumberOutput", 1);
  208. results << ctkCmdLineModuleResult("imageOutput", "/tmp/out.nrrd");
  209. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  210. QCOMPARE(signalTester.results(), results);
  211. }
  212. //-----------------------------------------------------------------------------
  213. void ctkCmdLineModuleFutureTester::testPauseAndCancel()
  214. {
  215. ctkCmdLineModuleSignalTester signalTester;
  216. frontend->setValue("runtimeVar", 60);
  217. ctkCmdLineModuleFuture future = manager.run(frontend);
  218. signalTester.setFuture(future);
  219. QList<QString> expectedSignals;
  220. expectedSignals << "module.started"
  221. << "module.progressRangeChanged(0,0)"
  222. << "module.progressValueChanged(0)"
  223. << "module.progressRangeChanged(0,1002)"
  224. << "module.errorReady";
  225. if (future.canPause())
  226. {
  227. // Due to Qt bug 12152, these two signals are reversed
  228. expectedSignals.push_back("module.resumed");
  229. expectedSignals.push_back("module.paused");
  230. }
  231. if (future.canCancel())
  232. {
  233. expectedSignals.push_back("module.canceled");
  234. }
  235. expectedSignals.push_back("module.finished");
  236. QTest::qWait(100);
  237. if (future.canPause())
  238. {
  239. future.pause();
  240. QTest::qWait(100);
  241. QVERIFY(future.isPaused());
  242. }
  243. QVERIFY(future.isRunning());
  244. if (future.canPause())
  245. {
  246. future.togglePaused();
  247. QTest::qWait(100);
  248. }
  249. QVERIFY(!future.isPaused());
  250. QVERIFY(future.isRunning());
  251. if (future.canCancel())
  252. {
  253. // give event processing a chance before killing the process
  254. QTest::qWait(200);
  255. future.cancel();
  256. }
  257. future.waitForFinished();
  258. // process pending events
  259. QCoreApplication::processEvents();
  260. QVERIFY(future.isCanceled());
  261. QVERIFY(future.isFinished());
  262. QVERIFY(signalTester.checkSignals(expectedSignals));
  263. }
  264. //-----------------------------------------------------------------------------
  265. void ctkCmdLineModuleFutureTester::testOutput()
  266. {
  267. ctkCmdLineModuleSignalTester signalTester;
  268. connect(signalTester.watcher(), SIGNAL(outputDataReady()), SLOT(ouputDataReady()));
  269. connect(signalTester.watcher(), SIGNAL(errorDataReady()), SLOT(errorDataReady()));
  270. this->currentWatcher = signalTester.watcher();
  271. frontend->setValue("numOutputsVar", 2);
  272. frontend->setValue("errorTextVar", "Final error msg.");
  273. ctkCmdLineModuleFuture future = manager.run(frontend);
  274. signalTester.setFuture(future);
  275. future.waitForFinished();
  276. // process pending events
  277. QCoreApplication::processEvents();
  278. QVERIFY(future.isFinished());
  279. QList<QString> expectedSignals;
  280. expectedSignals << "module.started"
  281. // the following signals are send when connecting a QFutureWatcher to
  282. // an already started QFuture
  283. << "module.progressRangeChanged(0,0)"
  284. << "module.progressValueChanged(0)"
  285. << "module.progressRangeChanged(0,1002)"
  286. // the test module always reports error data when starting
  287. << "module.errorReady"
  288. // the following two signals are send when the module reports "filter start"
  289. << "module.progressValueChanged(1)"
  290. << "module.progressTextChanged(Does nothing useful)"
  291. // the output data on the standard output channel "Output 1"
  292. << "module.outputReady"
  293. // this signal is send when the module reports progress for "output1"
  294. << "module.progressValueChanged(500)"
  295. << "module.progressTextChanged(Calculating output 2...)"
  296. // first resultNumberOutput result
  297. << "module.resultReadyAt(0,1)"
  298. << "module.resultReadyAt(0)"
  299. // the output data on the standard output channel "Output 2"
  300. << "module.outputReady"
  301. // this signal is send when the module reports progress for "output2"
  302. << "module.progressValueChanged(1000)"
  303. << "module.progressTextChanged(Calculating output 3...)"
  304. // second resultNumberOutput result
  305. << "module.resultReadyAt(1,2)"
  306. << "module.resultReadyAt(1)"
  307. // imageOutput result
  308. << "module.resultReadyAt(2,3)"
  309. << "module.resultReadyAt(2)"
  310. // exitStatusOutput result
  311. << "module.resultReadyAt(3,4)"
  312. << "module.resultReadyAt(3)"
  313. // <filter-end> progress value and text
  314. << "module.progressValueChanged(1001)"
  315. << "module.progressTextChanged(Finished successfully.)"
  316. // final error message
  317. << "module.errorReady"
  318. // the following signal is sent at the end to report completion
  319. << "module.progressValueChanged(1002)"
  320. << "module.finished";
  321. QVERIFY(signalTester.checkSignals(expectedSignals));
  322. const char* expectedOutput = "Output 1\nOutput 2\n";
  323. const char* expectedError = "A superficial error message.\nFinal error msg.";
  324. QCOMPARE(this->outputData.data(), expectedOutput);
  325. QCOMPARE(this->errorData.data(), expectedError);
  326. QCOMPARE(future.readAllOutputData().data(), expectedOutput);
  327. QCOMPARE(future.readAllErrorData().data(), expectedError);
  328. QList<ctkCmdLineModuleResult> results;
  329. results << ctkCmdLineModuleResult("resultNumberOutput", 1);
  330. results << ctkCmdLineModuleResult("resultNumberOutput", 2);
  331. results << ctkCmdLineModuleResult("errorMsgOutput", "Final error msg.");
  332. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  333. QCOMPARE(signalTester.results(), results);
  334. }
  335. //-----------------------------------------------------------------------------
  336. void ctkCmdLineModuleFutureTester::testError()
  337. {
  338. frontend->setValue("fileVar", "output1");
  339. frontend->setValue("exitCodeVar", 24);
  340. frontend->setValue("errorTextVar", "Some error occured\n");
  341. ctkCmdLineModuleFuture future = manager.run(frontend);
  342. try
  343. {
  344. future.waitForFinished();
  345. QFAIL("Expected exception not thrown.");
  346. }
  347. catch (const ctkCmdLineModuleRunException& e)
  348. {
  349. QVERIFY2(e.errorCode() == 24, "Test matching error code");
  350. QCOMPARE(future.readAllErrorData().data(), "A superficial error message.\nSome error occured\n");
  351. }
  352. }
  353. // ----------------------------------------------------------------------------
  354. CTK_TEST_MAIN(ctkCmdLineModuleFutureTest)
  355. #include "moc_ctkCmdLineModuleFutureTest.cpp"