ctkCmdLineModuleFutureTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. // final progress report from the module
  147. << "module.progressValueChanged(1000)"
  148. // <filter-end> progress value and text
  149. << "module.progressValueChanged(1001)"
  150. << "module.progressTextChanged(Finished successfully.)"
  151. // the following signal is sent at the end to report completion
  152. << "module.progressValueChanged(1002)"
  153. << "module.finished";
  154. ctkCmdLineModuleSignalTester signalTester;
  155. ctkCmdLineModuleFuture future = manager.run(frontend);
  156. signalTester.setFuture(future);
  157. future.waitForFinished();
  158. QCoreApplication::processEvents();
  159. QVERIFY(signalTester.checkSignals(expectedSignals));
  160. QList<ctkCmdLineModuleResult> results;
  161. results << ctkCmdLineModuleResult("imageOutput", "/tmp/out.nrrd");
  162. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  163. QCOMPARE(signalTester.results(), results);
  164. }
  165. //-----------------------------------------------------------------------------
  166. void ctkCmdLineModuleFutureTester::testProgress()
  167. {
  168. QList<QString> expectedSignals;
  169. expectedSignals << "module.started"
  170. // the following signals are send when connecting a QFutureWatcher to
  171. // an already started QFuture
  172. << "module.progressRangeChanged(0,0)"
  173. << "module.progressValueChanged(0)"
  174. << "module.progressRangeChanged(0,1002)"
  175. // the test module always reports error data when starting
  176. << "module.errorReady"
  177. // the following two signals are send when the module reports "filter start"
  178. << "module.progressValueChanged(1)"
  179. << "module.progressTextChanged(Does nothing useful)"
  180. // the output data on the standard output channel
  181. << "module.outputReady"
  182. // this signal is send when the module reports progress for "output1"
  183. << "module.progressValueChanged(1000)"
  184. << "module.progressTextChanged(Calculating output 2...)"
  185. // first resultNumberOutput result
  186. << "module.resultReadyAt(0,1)"
  187. << "module.resultReadyAt(0)"
  188. // imageOutput result
  189. << "module.resultReadyAt(1,2)"
  190. << "module.resultReadyAt(1)"
  191. // exitStatusOutput result
  192. << "module.resultReadyAt(2,3)"
  193. << "module.resultReadyAt(2)"
  194. // <filter-end> progress value and text
  195. << "module.progressValueChanged(1001)"
  196. << "module.progressTextChanged(Finished successfully.)"
  197. // the following signal is sent at the end to report completion
  198. << "module.progressValueChanged(1002)"
  199. << "module.finished";
  200. ctkCmdLineModuleSignalTester signalTester;
  201. frontend->setValue("numOutputsVar", 1);
  202. ctkCmdLineModuleFuture future = manager.run(frontend);
  203. signalTester.setFuture(future);
  204. future.waitForFinished();
  205. // process pending events
  206. QCoreApplication::processEvents();
  207. QVERIFY(signalTester.checkSignals(expectedSignals));
  208. QList<ctkCmdLineModuleResult> results;
  209. results << ctkCmdLineModuleResult("resultNumberOutput", 1);
  210. results << ctkCmdLineModuleResult("imageOutput", "/tmp/out.nrrd");
  211. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  212. QCOMPARE(signalTester.results(), results);
  213. }
  214. //-----------------------------------------------------------------------------
  215. void ctkCmdLineModuleFutureTester::testPauseAndCancel()
  216. {
  217. ctkCmdLineModuleSignalTester signalTester;
  218. frontend->setValue("runtimeVar", 60);
  219. ctkCmdLineModuleFuture future = manager.run(frontend);
  220. signalTester.setFuture(future);
  221. QList<QString> expectedSignals;
  222. expectedSignals << "module.started"
  223. << "module.progressRangeChanged(0,0)"
  224. << "module.progressValueChanged(0)"
  225. << "module.progressRangeChanged(0,1002)"
  226. << "module.errorReady";
  227. if (future.canPause())
  228. {
  229. // Due to Qt bug 12152, these two signals are reversed
  230. expectedSignals.push_back("module.resumed");
  231. expectedSignals.push_back("module.paused");
  232. }
  233. // the following two signals are send when the module reports "filter start"
  234. expectedSignals << "module.progressValueChanged(1)"
  235. << "module.progressTextChanged(Does nothing useful)";
  236. if (future.canCancel())
  237. {
  238. expectedSignals.push_back("module.canceled");
  239. }
  240. expectedSignals.push_back("module.finished");
  241. QTest::qWait(100);
  242. if (future.canPause())
  243. {
  244. future.pause();
  245. QTest::qWait(100);
  246. QVERIFY(future.isPaused());
  247. }
  248. QVERIFY(future.isRunning());
  249. if (future.canPause())
  250. {
  251. future.togglePaused();
  252. QTest::qWait(100);
  253. }
  254. QVERIFY(!future.isPaused());
  255. QVERIFY(future.isRunning());
  256. if (future.canCancel())
  257. {
  258. // give event processing a chance before killing the process
  259. QTest::qWait(500);
  260. future.cancel();
  261. }
  262. future.waitForFinished();
  263. // process pending events
  264. QCoreApplication::processEvents();
  265. QVERIFY(future.isCanceled());
  266. QVERIFY(future.isFinished());
  267. QVERIFY(signalTester.checkSignals(expectedSignals));
  268. }
  269. //-----------------------------------------------------------------------------
  270. void ctkCmdLineModuleFutureTester::testOutput()
  271. {
  272. ctkCmdLineModuleSignalTester signalTester;
  273. connect(signalTester.watcher(), SIGNAL(outputDataReady()), SLOT(ouputDataReady()));
  274. connect(signalTester.watcher(), SIGNAL(errorDataReady()), SLOT(errorDataReady()));
  275. this->currentWatcher = signalTester.watcher();
  276. frontend->setValue("numOutputsVar", 2);
  277. frontend->setValue("errorTextVar", "Final error msg.");
  278. ctkCmdLineModuleFuture future = manager.run(frontend);
  279. signalTester.setFuture(future);
  280. future.waitForFinished();
  281. // process pending events
  282. QCoreApplication::processEvents();
  283. QVERIFY(future.isFinished());
  284. QList<QString> expectedSignals;
  285. expectedSignals << "module.started"
  286. // the following signals are send when connecting a QFutureWatcher to
  287. // an already started QFuture
  288. << "module.progressRangeChanged(0,0)"
  289. << "module.progressValueChanged(0)"
  290. << "module.progressRangeChanged(0,1002)"
  291. // the test module always reports error data when starting
  292. << "module.errorReady"
  293. // the following two signals are send when the module reports "filter start"
  294. << "module.progressValueChanged(1)"
  295. << "module.progressTextChanged(Does nothing useful)"
  296. // the output data on the standard output channel "Output 1"
  297. << "module.outputReady"
  298. // this signal is send when the module reports progress for "output1"
  299. << "module.progressValueChanged(500)"
  300. << "module.progressTextChanged(Calculating output 2...)"
  301. // first resultNumberOutput result
  302. << "module.resultReadyAt(0,1)"
  303. << "module.resultReadyAt(0)"
  304. // the output data on the standard output channel "Output 2"
  305. << "module.outputReady"
  306. // this signal is send when the module reports progress for "output2"
  307. << "module.progressValueChanged(1000)"
  308. << "module.progressTextChanged(Calculating output 3...)"
  309. // second resultNumberOutput result
  310. << "module.resultReadyAt(1,2)"
  311. << "module.resultReadyAt(1)"
  312. // imageOutput result
  313. << "module.resultReadyAt(2,3)"
  314. << "module.resultReadyAt(2)"
  315. // exitStatusOutput result
  316. << "module.resultReadyAt(3,4)"
  317. << "module.resultReadyAt(3)"
  318. // <filter-end> progress value and text
  319. << "module.progressValueChanged(1001)"
  320. << "module.progressTextChanged(Finished successfully.)"
  321. // final error message
  322. << "module.errorReady"
  323. // the following signal is sent at the end to report completion
  324. << "module.progressValueChanged(1002)"
  325. << "module.finished";
  326. QVERIFY(signalTester.checkSignals(expectedSignals));
  327. const char* expectedOutput = "Output 1\nOutput 2\n";
  328. const char* expectedError = "A superficial error message.\nFinal error msg.";
  329. QCOMPARE(this->outputData.data(), expectedOutput);
  330. QCOMPARE(this->errorData.data(), expectedError);
  331. QCOMPARE(future.readAllOutputData().data(), expectedOutput);
  332. QCOMPARE(future.readAllErrorData().data(), expectedError);
  333. QList<ctkCmdLineModuleResult> results;
  334. results << ctkCmdLineModuleResult("resultNumberOutput", 1);
  335. results << ctkCmdLineModuleResult("resultNumberOutput", 2);
  336. results << ctkCmdLineModuleResult("errorMsgOutput", "Final error msg.");
  337. results << ctkCmdLineModuleResult("exitStatusOutput", "Normal exit");
  338. QCOMPARE(signalTester.results(), results);
  339. }
  340. //-----------------------------------------------------------------------------
  341. void ctkCmdLineModuleFutureTester::testError()
  342. {
  343. frontend->setValue("fileVar", "output1");
  344. frontend->setValue("exitCodeVar", 24);
  345. frontend->setValue("errorTextVar", "Some error occured\n");
  346. ctkCmdLineModuleFuture future = manager.run(frontend);
  347. try
  348. {
  349. future.waitForFinished();
  350. QFAIL("Expected exception not thrown.");
  351. }
  352. catch (const ctkCmdLineModuleRunException& e)
  353. {
  354. QVERIFY2(e.errorCode() == 24, "Test matching error code");
  355. QCOMPARE(future.readAllErrorData().data(), "A superficial error message.\nSome error occured\n");
  356. }
  357. }
  358. // ----------------------------------------------------------------------------
  359. CTK_TEST_MAIN(ctkCmdLineModuleFutureTest)
  360. #include "moc_ctkCmdLineModuleFutureTest.cpp"