Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <random>
  7. #include "CodeDweller/child.hpp"
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Configuration ///////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /// Child program name.
  12. const std::string childName("./childProgram");
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // End of configuration ////////////////////////////////////////////////////////
  15. ////////////////////////////////////////////////////////////////////////////////
  16. int nTotalTests = 0;
  17. int nPass = 0;
  18. int nFail = 0;
  19. bool result;
  20. #define NO_EXCEPTION_TERM(msg) \
  21. std::cout \
  22. << msg << " failed to throw exception at line " \
  23. << __LINE__ << "." << std::endl
  24. #define EXCEPTION_TERM(msg) \
  25. std::cout \
  26. << msg << " threw unexpected exception: " << e.what() << std::endl
  27. #define RETURN_FALSE(msg) \
  28. std::cout \
  29. << msg << " at line " << __LINE__ << std::endl; \
  30. return false;
  31. #define RUN_TEST(test) \
  32. std::cout << " " #test ": "; \
  33. std::cout.flush(); \
  34. result = test(); \
  35. std::cout << (result ? "ok" : "fail") << std::endl; \
  36. nTotalTests++; \
  37. if (result) nPass++; else nFail++;
  38. #define SUMMARY \
  39. std::cout \
  40. << "\nPass: " << nPass \
  41. << ", Fail: " << nFail \
  42. << ", Total: " << nTotalTests << std::endl
  43. bool doReadWrite(size_t bufSize,
  44. size_t nominalAboveMin_ms,
  45. size_t delta_ms,
  46. size_t maxChar) {
  47. try {
  48. std::vector<std::string> cmd;
  49. cmd.push_back(childName);
  50. CodeDweller::Child child(cmd,
  51. bufSize,
  52. nominalAboveMin_ms,
  53. delta_ms);
  54. // Characters for testing.
  55. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  56. // Calculate how often to output a "." to indicate progress.
  57. int maxChunks = maxChar / bufSize;
  58. int nChunks = 0;
  59. int nDots = 50;
  60. int outputEveryChunk = maxChunks / nDots / 2;
  61. if (outputEveryChunk == 0) {
  62. outputEveryChunk = 2;
  63. }
  64. // Send and receive random-sized chunks of data.
  65. size_t nChar = 0;
  66. while (nChar < maxChar) {
  67. std::uniform_int_distribution<int> randomChunk(1, bufSize * 4);
  68. std::default_random_engine generator(4828943);
  69. nChunks++;
  70. if (nChunks % outputEveryChunk == 0) {
  71. std::cout << ".";
  72. std::cout.flush();
  73. }
  74. int chunkSize;
  75. std::string sentChunk, expectedChunk;
  76. // Get a random chunk size for sending.
  77. chunkSize = randomChunk(generator);
  78. if (chunkSize + nChar > maxChar) {
  79. chunkSize = maxChar - nChar;
  80. }
  81. // Generate output to child.
  82. sentChunk.clear();
  83. for (size_t i = 0; i < chunkSize; i++) {
  84. sentChunk.push_back(randomLetter[std::rand() % 26]);
  85. }
  86. // Generated expected input from child.
  87. expectedChunk = sentChunk;
  88. for (int i = 0; i < expectedChunk.size(); i++) {
  89. expectedChunk[i] = std::toupper(expectedChunk[i]);
  90. }
  91. // Send the data.
  92. while (!sentChunk.empty()) {
  93. if (child.writeAndShrink(sentChunk) == 0) {
  94. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  95. }
  96. // Read and compare while sending data.
  97. std::string chunkFromChild;
  98. size_t chunkFromChildSize;
  99. (void) child.read(chunkFromChild);
  100. chunkFromChildSize = chunkFromChild.size();
  101. if (!chunkFromChild.empty()) {
  102. if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
  103. expectedChunk.erase(0, chunkFromChildSize);
  104. } else {
  105. std::cout << "Expected: \""
  106. << expectedChunk.substr(0, chunkFromChildSize)
  107. << "\", received \"" << chunkFromChild << "\"."
  108. << std::endl;
  109. std::cout << "Remaining chunk: \"" << expectedChunk << "\""
  110. << std::endl;
  111. child.write("q");
  112. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  113. RETURN_FALSE(" comparison failure");
  114. }
  115. }
  116. }
  117. // Read and compare after all data was sent.
  118. while (!expectedChunk.empty()) {
  119. std::string chunkFromChild;
  120. size_t chunkFromChildSize;
  121. (void) child.read(chunkFromChild);
  122. chunkFromChildSize = chunkFromChild.size();
  123. if (0 == chunkFromChildSize) {
  124. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  125. } else {
  126. if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
  127. expectedChunk.erase(0, chunkFromChildSize);
  128. } else {
  129. std::cout << "Expected: \""
  130. << expectedChunk.substr(0, chunkFromChildSize)
  131. << "\", received \"" << chunkFromChild << "\"."
  132. << std::endl;
  133. std::cout << "Remaining chunk: \"" << expectedChunk << "\""
  134. << std::endl;
  135. child.write("q");
  136. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  137. RETURN_FALSE(" comparison failure");
  138. }
  139. }
  140. }
  141. nChar += chunkSize;
  142. }
  143. // Send exit message.
  144. if (!child.write("q")) {
  145. RETURN_FALSE(" write() failure");
  146. }
  147. // Verify exit.
  148. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  149. if (!child.isDone()) {
  150. std::cout << " Failure in testChildNonblockingReadWrite2: "
  151. << "Child program did not exit." << std::endl;
  152. return false;
  153. }
  154. std::string errorDescription;
  155. if (child.errorOccurred(errorDescription)) {
  156. std::ostringstream temp;
  157. temp << " Failure in: testChildNonBlockingReadWrite2: "
  158. << errorDescription;
  159. RETURN_FALSE(temp.str());
  160. }
  161. child.close();
  162. } catch (std::exception &e) {
  163. EXCEPTION_TERM("Non-blocking reader test");
  164. return false;
  165. }
  166. return true;
  167. }
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // ChildStream Tests ///////////////////////////////////////////////////////////
  170. ////////////////////////////////////////////////////////////////////////////////
  171. bool testChildStreamIsDone() {
  172. try {
  173. CodeDweller::ChildStream child;
  174. // Test exception if called out-of-order.
  175. try {
  176. child.isDone();
  177. NO_EXCEPTION_TERM("isDone() called without run()");
  178. return false;
  179. } catch (std::exception &e) {
  180. }
  181. // Start the child.
  182. child.open(childName);
  183. if (child.isDone()) {
  184. std::cout << "isDone() failure; returned true." << std::endl;
  185. return false;
  186. }
  187. // Command the child to exit.
  188. child << 'q';
  189. child.flush();
  190. // Sleep to let the child exit.
  191. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  192. if (!child.isDone()) {
  193. std::cout << "isDone() failure; returned false." << std::endl;
  194. return false;
  195. }
  196. } catch (std::exception &e) {
  197. EXCEPTION_TERM("isDone()");
  198. return false;
  199. }
  200. return true;
  201. }
  202. bool testChildStreamIsRunning() {
  203. try {
  204. CodeDweller::ChildStream child;
  205. if (child.isRunning()) {
  206. std::cout << "isRunning() failure; returned true before starting."
  207. << std::endl;
  208. return false;
  209. }
  210. // Start the child.
  211. child.open(childName);
  212. if (!child.isRunning()) {
  213. std::cout << "isRunning() failure; returned false." << std::endl;
  214. return false;
  215. }
  216. // Command the child to exit.
  217. child << 'q';
  218. child.flush();
  219. // Sleep to let the child exit.
  220. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  221. child.close();
  222. if (child.isRunning()) {
  223. std::cout << "isRunning() failure; returned true after stopping."
  224. << std::endl;
  225. return false;
  226. }
  227. } catch (std::exception &e) {
  228. EXCEPTION_TERM("testChildStreamIsRunning");
  229. return false;
  230. }
  231. return true;
  232. }
  233. bool testChildStreamResult() {
  234. try {
  235. std::vector<std::string> cmd;
  236. cmd.push_back(childName);
  237. cmd.push_back("quit"); // Child sleeps for 50 ms and then exits.
  238. CodeDweller::ChildStream child(cmd);
  239. // Test exception if called out-of-order.
  240. try {
  241. (void) child.result();
  242. NO_EXCEPTION_TERM(" result() called without run()");
  243. return false;
  244. } catch (std::exception &e) {
  245. }
  246. // Test exception if called while child is running.
  247. try {
  248. (void) child.result();
  249. NO_EXCEPTION_TERM(" result() called before child exited");
  250. return false;
  251. } catch (std::exception &e) {
  252. }
  253. // Wait for the child to exit.
  254. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  255. int32_t result = child.result();
  256. if (25 != result) {
  257. std::cout << "result() failure; returned " << result
  258. << " instead of 25." << std::endl;
  259. return false;
  260. }
  261. } catch (std::exception &e) {
  262. EXCEPTION_TERM("result()");
  263. return false;
  264. }
  265. return true;
  266. }
  267. bool testChildStreamClose() {
  268. // Test with no waiting.
  269. try {
  270. CodeDweller::ChildStream child(childName);
  271. child.close();
  272. } catch (std::exception &e) {
  273. EXCEPTION_TERM("close() with no waiting");
  274. return false;
  275. }
  276. // Test with waiting.
  277. try {
  278. CodeDweller::ChildStream child(childName);
  279. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  280. child.close();
  281. } catch (std::exception &e) {
  282. EXCEPTION_TERM("close() with 100 ms waiting");
  283. return false;
  284. }
  285. // Test after the child exits.
  286. std::vector<std::string> cmd;
  287. cmd.push_back(childName);
  288. cmd.push_back("quit");
  289. try {
  290. CodeDweller::ChildStream child(cmd);
  291. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  292. child.close();
  293. } catch (std::exception &e) {
  294. EXCEPTION_TERM("close() after child exits");
  295. return false;
  296. }
  297. // Test exception thrown for out-of-order calling.
  298. try {
  299. CodeDweller::ChildStream child;
  300. child.close();
  301. NO_EXCEPTION_TERM("close() called without run()");
  302. return false;
  303. } catch (std::exception &e) {
  304. }
  305. return true;
  306. }
  307. bool testChildStreamReaderWriter() {
  308. try {
  309. size_t bufSize = 15;
  310. CodeDweller::ChildStream child(bufSize);
  311. std::ostringstream childOutput;
  312. std::vector<std::string> expectedChildOutput;
  313. char readChar;
  314. // Generate input.
  315. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  316. int nChar = bufSize - 1;
  317. int nLines = 2;
  318. for (int iLine = 0; iLine < nLines; iLine++) {
  319. std::string line;
  320. line.erase();
  321. for (int iChar = 0; iChar < nChar; iChar++) {
  322. line.push_back(randomLetter[std::rand() % 26]);
  323. }
  324. expectedChildOutput.push_back(line);
  325. }
  326. // Test exception.
  327. try {
  328. child.exceptions(std::ostream::failbit | std::ostream::badbit);
  329. child << bufSize;
  330. child.flush();
  331. NO_EXCEPTION_TERM(" writer called without run()");
  332. return false;
  333. } catch (std::exception &e) {
  334. }
  335. // Clear the writer stream.
  336. try {
  337. child.clear();
  338. } catch (std::exception &e) {
  339. }
  340. child.open(childName);
  341. // Write, read, put back, and reread each character.
  342. for (std::string line : expectedChildOutput) {
  343. // Write one line.
  344. const char *ptr;
  345. ptr = line.data();
  346. for (std::string::size_type i = 0; i < line.length(); i++) {
  347. child << ptr[i];
  348. if (!child) {
  349. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  350. }
  351. }
  352. child.flush();
  353. if (!child) {
  354. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  355. }
  356. // Read one line.
  357. std::string readLine;
  358. readLine.erase();
  359. for (std::string::size_type i = 0; i < line.length(); i++) {
  360. child >> readChar;
  361. if (!child) {
  362. RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
  363. }
  364. readLine.push_back(readChar);
  365. }
  366. // Convert to upper case.
  367. std::string expectedLine;
  368. expectedLine = line;
  369. for (auto &c : expectedLine) {
  370. c = toupper(c);
  371. }
  372. // Compare.
  373. if (expectedLine != readLine) {
  374. std::cout << " Failure in testReaderWriter." << std::endl;
  375. std::cout << " Expected: '" << expectedLine
  376. << "'\n Received: '" << readLine << "'" << std::endl;
  377. return false;
  378. }
  379. }
  380. // Send exit message.
  381. child << 'q';
  382. child.flush();
  383. if (!child) {
  384. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  385. }
  386. // Verify exit.
  387. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  388. if (!child.isDone()) {
  389. std::cout << " Failure in testReaderWriter: "
  390. << "Child program did not exit." << std::endl;
  391. return false;
  392. }
  393. } catch (std::exception &e) {
  394. EXCEPTION_TERM("reader()/writer()");
  395. return false;
  396. }
  397. return true;
  398. }
  399. bool testChildStreamReader() {
  400. try {
  401. size_t bufSize = 32;
  402. CodeDweller::ChildStream child(bufSize);
  403. // Test exception.
  404. try {
  405. int temp;
  406. child.exceptions(std::istream::failbit | std::istream::badbit);
  407. child >> temp;
  408. NO_EXCEPTION_TERM(" reader called without run()");
  409. return false;
  410. } catch (std::exception &e) {
  411. }
  412. child.clear();
  413. child.clear();
  414. std::ostringstream childOutput;
  415. std::string expectedChildOutput("This is a test");
  416. char readChar;
  417. std::vector<std::string> cmd;
  418. cmd.push_back(childName);
  419. cmd.push_back("write");
  420. child.open(cmd);
  421. child.exceptions(std::istream::badbit);
  422. child >> std::noskipws;
  423. if (!child) {
  424. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  425. }
  426. while (child >> readChar) {
  427. if (!child) {
  428. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  429. }
  430. child.putback(readChar);
  431. if (!child) {
  432. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  433. }
  434. child >> readChar;
  435. if (!child) {
  436. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  437. }
  438. childOutput << readChar;
  439. }
  440. if (!child.eof()) {
  441. RETURN_FALSE(" Failure in testReader: Error occured before "
  442. "EOF was reached while reading");
  443. }
  444. // Check.
  445. if (childOutput.str() != expectedChildOutput) {
  446. std::cout << " reader() failure in testReader." << std::endl;
  447. std::cout << " Expected: '" << expectedChildOutput
  448. << "'\n Received: '" << childOutput.str() << "'"
  449. << std::endl;
  450. return false;
  451. }
  452. // Sleep to let the child exit.
  453. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  454. if (!child.isDone()) {
  455. std::cout << "first isDone() failure in testReader." << std::endl;
  456. return false;
  457. }
  458. if (!child.isDone()) {
  459. std::cout << "second isDone() failure in testReader." << std::endl;
  460. return false;
  461. }
  462. } catch (std::exception &e) {
  463. EXCEPTION_TERM("reader()");
  464. return false;
  465. }
  466. return true;
  467. }
  468. bool testChildStreamBinaryRead() {
  469. try {
  470. std::vector<std::string> cmd;
  471. cmd.push_back(childName);
  472. size_t bufSize = 164;
  473. CodeDweller::ChildStream child(bufSize);
  474. child.open(cmd);
  475. // Write.
  476. std::string childInput("abc");
  477. child << childInput;
  478. child.flush();
  479. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  480. // Read one character.
  481. char ch;
  482. child.read(&ch, 1);
  483. if (ch != 'A') {
  484. RETURN_FALSE(" reader.read() returned incorrect value");
  485. }
  486. // Read.
  487. char buf[bufSize * 2];
  488. child.read(buf, 2);
  489. buf[2] = '\0';
  490. std::string input(buf);
  491. if (input != "BC") {
  492. RETURN_FALSE(" reader.read() returned incorrect value");
  493. }
  494. // Fill input buffer.
  495. std::string output("abcdefghijklmnopprstuvwxyz");
  496. std::string expectedInput(output);
  497. for (int i = 0; i < output.size(); i++) {
  498. expectedInput[i] = std::toupper(output[i]);
  499. }
  500. child << output;
  501. child.flush();
  502. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  503. child.read(&ch, 1);
  504. int index = 0;
  505. if (ch != expectedInput[index++]) {
  506. RETURN_FALSE(" reader.read() returned incorrect value");
  507. }
  508. size_t nBytesRead = expectedInput.size() - 1;
  509. child.read(buf, nBytesRead);
  510. buf[nBytesRead] = '\0';
  511. if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
  512. RETURN_FALSE(" reader.read() failure");
  513. }
  514. // Send exit message.
  515. child << 'q';
  516. child.flush();
  517. if (!child) {
  518. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  519. }
  520. // Verify exit.
  521. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  522. if (!child.isDone()) {
  523. std::cout << " Failure in testNonblockingReader: "
  524. << "Child program did not exit." << std::endl;
  525. return false;
  526. }
  527. } catch (std::exception &e) {
  528. EXCEPTION_TERM("Binary read test");
  529. return false;
  530. }
  531. return true;
  532. }
  533. bool testChildStreamNonBlockingRead() {
  534. try {
  535. std::vector<std::string> cmd;
  536. cmd.push_back(childName);
  537. size_t bufSize = 16;
  538. CodeDweller::ChildStream child(cmd, bufSize);
  539. // Check for available input with no input.
  540. if (child.numBytesAvailable() != 0) {
  541. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  542. }
  543. // Check for available input with input.
  544. std::string childInput("abc");
  545. child << childInput;
  546. child.flush();
  547. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  548. // Check that input is available.
  549. if (child.numBytesAvailable() != 1) {
  550. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  551. }
  552. // Read one character.
  553. char ch;
  554. child.read(&ch, 1);
  555. if (ch != 'A') {
  556. RETURN_FALSE(" reader.read() returned incorrect value");
  557. }
  558. // Check that input is available.
  559. if (child.numBytesAvailable() != 2) {
  560. RETURN_FALSE(" numBytesAvailable() did not return expected 2");
  561. }
  562. // Read.
  563. char buf[bufSize * 2];
  564. child.read(buf, 2);
  565. buf[2] = '\0';
  566. std::string input(buf);
  567. if (input != "BC") {
  568. RETURN_FALSE(" reader.read() returned incorrect value");
  569. }
  570. // Check that no input is available.
  571. if (child.numBytesAvailable() != 0) {
  572. RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
  573. "after reading");
  574. }
  575. // Fill input buffer.
  576. std::string output("abcdefghijklmnopprstuvwxyz");
  577. std::string expectedInput(output);
  578. for (int i = 0; i < output.size(); i++) {
  579. expectedInput[i] = std::toupper(output[i]);
  580. }
  581. child << output;
  582. child.flush();
  583. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  584. if (child.numBytesAvailable() != 1) {
  585. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  586. }
  587. child.read(&ch, 1);
  588. int index = 0;
  589. if (ch != expectedInput[index++]) {
  590. RETURN_FALSE(" reader.read() returned incorrect value");
  591. }
  592. size_t nBytesAvailable;
  593. nBytesAvailable = child.numBytesAvailable();
  594. if (nBytesAvailable != bufSize) {
  595. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  596. }
  597. std::fill_n(buf, sizeof(buf), 0);
  598. child.read(buf, nBytesAvailable);
  599. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  600. RETURN_FALSE(" reader.read() failure");
  601. }
  602. index += nBytesAvailable;
  603. nBytesAvailable = child.numBytesAvailable();
  604. if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
  605. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  606. }
  607. std::fill_n(buf, sizeof(buf), 0);
  608. child.read(buf, nBytesAvailable);
  609. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  610. RETURN_FALSE(" reader.read() failure");
  611. }
  612. index += nBytesAvailable;
  613. if (expectedInput.size() != index) {
  614. RETURN_FALSE(" not all data was read by reader.read()");
  615. }
  616. if (child.numBytesAvailable() != 0) {
  617. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  618. }
  619. // Send exit message.
  620. child << 'q';
  621. child.flush();
  622. if (!child) {
  623. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  624. }
  625. // Verify exit.
  626. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  627. if (!child.isDone()) {
  628. std::cout << " Failure in testNonblockingReader: "
  629. << "Child program did not exit." << std::endl;
  630. return false;
  631. }
  632. } catch (std::exception &e) {
  633. EXCEPTION_TERM("Non-blocking reader test");
  634. return false;
  635. }
  636. return true;
  637. }
  638. ////////////////////////////////////////////////////////////////////////////////
  639. // Child Tests ///////////////////////////////////////////////////////////
  640. ////////////////////////////////////////////////////////////////////////////////
  641. bool testChildIsDone() {
  642. try {
  643. CodeDweller::Child child;
  644. // Test exception if called out-of-order.
  645. try {
  646. child.isDone();
  647. NO_EXCEPTION_TERM("isDone() called without run()");
  648. return false;
  649. } catch (std::exception &e) {
  650. }
  651. // Start the child.
  652. child.open(childName);
  653. if (child.isDone()) {
  654. std::cout << "isDone() failure; returned true." << std::endl;
  655. return false;
  656. }
  657. // Command the child to exit.
  658. if (!child.write("q")) {
  659. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  660. }
  661. // Sleep to let the child exit.
  662. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  663. if (!child.isDone()) {
  664. std::cout << "isDone() failure; returned false." << std::endl;
  665. return false;
  666. }
  667. child.close();
  668. } catch (std::exception &e) {
  669. EXCEPTION_TERM("isDone()");
  670. return false;
  671. }
  672. return true;
  673. }
  674. bool testChildIsRunning() {
  675. try {
  676. CodeDweller::Child child;
  677. if (child.isRunning()) {
  678. std::cout << "isRunning() failure; returned true before starting."
  679. << std::endl;
  680. return false;
  681. }
  682. // Start the child.
  683. child.open(childName);
  684. if (!child.isRunning()) {
  685. std::cout << "isRunning() failure; returned false." << std::endl;
  686. return false;
  687. }
  688. // Command the child to exit.
  689. if (!child.write("q")) {
  690. RETURN_FALSE(" write() failure");
  691. }
  692. // Sleep to let the child exit.
  693. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  694. child.close();
  695. if (child.isRunning()) {
  696. std::cout << "isRunning() failure; returned true after stopping."
  697. << std::endl;
  698. return false;
  699. }
  700. } catch (std::exception &e) {
  701. EXCEPTION_TERM("isRunning()");
  702. return false;
  703. }
  704. return true;
  705. }
  706. bool testChildResult() {
  707. try {
  708. std::vector<std::string> cmd;
  709. cmd.push_back(childName);
  710. cmd.push_back("quit");
  711. CodeDweller::Child child(cmd);
  712. // Test exception if called out-of-order.
  713. try {
  714. (void) child.result();
  715. NO_EXCEPTION_TERM(" result() called without run()");
  716. return false;
  717. } catch (std::exception &e) {
  718. }
  719. // Test exception if called while child is running.
  720. try {
  721. (void) child.result();
  722. NO_EXCEPTION_TERM(" result() called before child exited");
  723. return false;
  724. } catch (std::exception &e) {
  725. }
  726. // Wait for the child to exit.
  727. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  728. int32_t result = child.result();
  729. if (25 != result) {
  730. std::cout << "result() failure; returned " << result
  731. << " instead of 25." << std::endl;
  732. return false;
  733. }
  734. } catch (std::exception &e) {
  735. EXCEPTION_TERM("result()");
  736. return false;
  737. }
  738. return true;
  739. }
  740. bool testChildClose() {
  741. // Test with no child process.
  742. try {
  743. CodeDweller::Child child;
  744. child.close();
  745. NO_EXCEPTION_TERM("close() with no child process");
  746. return false;
  747. } catch (std::exception &e) {
  748. }
  749. // Test with no waiting.
  750. try {
  751. CodeDweller::Child child(childName);
  752. child.close();
  753. } catch (std::exception &e) {
  754. EXCEPTION_TERM("close() with no waiting");
  755. return false;
  756. }
  757. // Test with waiting.
  758. try {
  759. CodeDweller::Child child(childName);
  760. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  761. child.close();
  762. } catch (std::exception &e) {
  763. EXCEPTION_TERM("close() with 100 ms waiting");
  764. return false;
  765. }
  766. // Test after the child exits.
  767. std::vector<std::string> cmd;
  768. cmd.push_back(childName);
  769. cmd.push_back("quit");
  770. try {
  771. CodeDweller::Child child(cmd);
  772. if (!child.write("q")) {
  773. RETURN_FALSE(" write() failure");
  774. }
  775. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  776. child.close();
  777. } catch (std::exception &e) {
  778. EXCEPTION_TERM("close() after child exits");
  779. return false;
  780. }
  781. // Test exception thrown for out-of-order calling.
  782. try {
  783. CodeDweller::Child child;
  784. child.close();
  785. NO_EXCEPTION_TERM("close() called without open()");
  786. return false;
  787. } catch (std::exception &e) {
  788. }
  789. return true;
  790. }
  791. bool testChildStdInClose() {
  792. std::string errorDescription;
  793. // Test with no child process.
  794. try {
  795. CodeDweller::Child child;
  796. child.closeStdIn();
  797. NO_EXCEPTION_TERM("closeStdIn() with no child process");
  798. return false;
  799. } catch (std::exception &e) {
  800. }
  801. // Test with no waiting.
  802. try {
  803. CodeDweller::Child child(childName);
  804. child.closeStdIn();
  805. child.close();
  806. } catch (std::exception &e) {
  807. EXCEPTION_TERM("closeStdIn() with no waiting");
  808. return false;
  809. }
  810. // Run child that waits for stdin to close.
  811. std::vector<std::string> cmd;
  812. cmd.push_back(childName);
  813. cmd.push_back("checkStdinEOF");
  814. try {
  815. CodeDweller::Child child(cmd);
  816. child.closeStdIn();
  817. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  818. int32_t result = child.result();
  819. if (15 != result) {
  820. std::cout << "closeStdIn() failure; returned " << result
  821. << " instead of 15." << std::endl;
  822. return false;
  823. }
  824. child.close();
  825. if (child.errorOccurred(errorDescription)) {
  826. std::ostringstream temp;
  827. temp << " Failure in: testChildStdInClose: " << errorDescription;
  828. RETURN_FALSE(temp.str());
  829. }
  830. } catch (std::exception &e) {
  831. EXCEPTION_TERM("closeStdIn() or close()");
  832. return false;
  833. }
  834. // Test after the child exits.
  835. cmd.clear();
  836. cmd.push_back(childName);
  837. cmd.push_back("quit");
  838. try {
  839. CodeDweller::Child child(cmd);
  840. if (!child.write("q")) {
  841. RETURN_FALSE(" write() failure");
  842. }
  843. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  844. child.closeStdIn();
  845. } catch (std::exception &e) {
  846. EXCEPTION_TERM("closeStdIn() after child exits");
  847. return false;
  848. }
  849. // Test exception thrown for out-of-order calling.
  850. try {
  851. CodeDweller::Child child;
  852. child.closeStdIn();
  853. NO_EXCEPTION_TERM("closeStdIn() called without open()");
  854. return false;
  855. } catch (std::exception &e) {
  856. }
  857. return true;
  858. }
  859. bool testChildrenStdInClose() {
  860. std::string errorDescription;
  861. int const nChildren = 5;
  862. // Test with no child process.
  863. try {
  864. CodeDweller::Child child;
  865. child.closeStdIn();
  866. NO_EXCEPTION_TERM("closeStdIn() with no child process");
  867. return false;
  868. } catch (std::exception &e) {
  869. }
  870. // Test with no waiting.
  871. try {
  872. CodeDweller::Child child[nChildren];
  873. for (int i = 0; i < nChildren; i++) {
  874. child[i].open(childName);
  875. }
  876. for (int i = 0; i < nChildren; i++) {
  877. child[i].closeStdIn();
  878. }
  879. for (int i = 0; i < nChildren; i++) {
  880. child[i].close();
  881. }
  882. } catch (std::exception &e) {
  883. EXCEPTION_TERM("closeStdIn() with no waiting");
  884. return false;
  885. }
  886. // Run child that waits for stdin to close.
  887. std::vector<std::string> cmd;
  888. cmd.push_back(childName);
  889. cmd.push_back("checkStdinEOF");
  890. try {
  891. CodeDweller::Child child[nChildren];
  892. for (int i = 0; i < nChildren; i++ ) {
  893. child[i].open(cmd);
  894. }
  895. for (int i = 0; i < nChildren; i++) {
  896. child[i].closeStdIn();
  897. }
  898. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  899. for (int i = 0; i < nChildren; i++) {
  900. int32_t result = child[i].result();
  901. if (15 != result) {
  902. std::cout << "closeStdIn() failure for child " << i << "; returned "
  903. << result << " instead of 15." << std::endl;
  904. return false;
  905. }
  906. }
  907. for (int i = 0; i < nChildren; i++) {
  908. child[i].close();
  909. }
  910. for (int i = 0; i < nChildren; i++) {
  911. if (child[i].errorOccurred(errorDescription)) {
  912. std::ostringstream temp;
  913. temp << " Failure in: testChildStdInClose for child " << i
  914. << ": " << errorDescription;
  915. RETURN_FALSE(temp.str());
  916. }
  917. }
  918. } catch (std::exception &e) {
  919. EXCEPTION_TERM("closeStdIn() or close()");
  920. return false;
  921. }
  922. // Test after the child exits.
  923. cmd.clear();
  924. cmd.push_back(childName);
  925. cmd.push_back("quit");
  926. try {
  927. CodeDweller::Child child[nChildren];
  928. for (int i = 0; i < nChildren; i++) {
  929. child[i].open(cmd);
  930. }
  931. for (int i = 0; i < nChildren; i++) {
  932. if (!child[i].write("q")) {
  933. RETURN_FALSE(" write() failure");
  934. }
  935. }
  936. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  937. for (int i = 0; i < nChildren; i++) {
  938. child[i].closeStdIn();
  939. }
  940. } catch (std::exception &e) {
  941. EXCEPTION_TERM("closeStdIn() after child exits");
  942. return false;
  943. }
  944. return true;
  945. }
  946. bool testChildOpen() {
  947. // Test with no waiting.
  948. try {
  949. CodeDweller::Child child(childName);
  950. child.close();
  951. child.open(childName);
  952. child.close();
  953. } catch (std::exception &e) {
  954. EXCEPTION_TERM("close()/open() with no waiting");
  955. return false;
  956. }
  957. // Test with waiting.
  958. try {
  959. CodeDweller::Child child(childName);
  960. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  961. child.close();
  962. child.open(childName);
  963. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  964. child.close();
  965. } catch (std::exception &e) {
  966. EXCEPTION_TERM("close()/open() with 100 ms waiting");
  967. return false;
  968. }
  969. // Test exception thrown for out-of-order calling.
  970. try {
  971. CodeDweller::Child child;
  972. child.close();
  973. NO_EXCEPTION_TERM("close() called without open()");
  974. return false;
  975. } catch (std::exception &e) {
  976. }
  977. return true;
  978. }
  979. bool testChildReadWrite() {
  980. size_t bufSize = 15;
  981. int nChar = bufSize - 1; // Size of each packet.
  982. try {
  983. CodeDweller::Child child(bufSize);
  984. std::ostringstream childOutput;
  985. std::vector<std::string> expectedChildOutput;
  986. char readChar;
  987. // Generate input.
  988. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  989. int nChar = bufSize - 1;
  990. int nLines = 2;
  991. for (int iLine = 0; iLine < nLines; iLine++) {
  992. std::string line;
  993. line.erase();
  994. for (int iChar = 0; iChar < nChar; iChar++) {
  995. line.push_back(randomLetter[std::rand() % 26]);
  996. }
  997. expectedChildOutput.push_back(line);
  998. }
  999. // Test exception.
  1000. try {
  1001. child.write("Should Throw");
  1002. NO_EXCEPTION_TERM(" writer called without run()");
  1003. return false;
  1004. } catch (std::exception &e) {
  1005. }
  1006. child.open(childName);
  1007. // Write, read test.
  1008. int nLine = 0;
  1009. for (std::string &line : expectedChildOutput) {
  1010. nLine++;
  1011. // Try to queue.
  1012. bool done;
  1013. int nTries = 100;
  1014. for (int i = 0; i < nTries; i++) {
  1015. done = child.write(line);
  1016. if (done) {
  1017. break;
  1018. }
  1019. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1020. }
  1021. if (!done) {
  1022. std::ostringstream temp;
  1023. temp << " Failure from write(std::string const &) on line " << nLine;
  1024. RETURN_FALSE(temp.str());
  1025. }
  1026. // Wait for transmission.
  1027. for (int i = 0; i < nTries; i++) {
  1028. done = child.isFinishedWriting();
  1029. if (done) {
  1030. break;
  1031. }
  1032. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1033. }
  1034. if (!done) {
  1035. std::ostringstream temp;
  1036. temp << " Failure from isFinishedWriting() on line " << nLine;
  1037. RETURN_FALSE(temp.str());
  1038. }
  1039. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1040. // Read one line.
  1041. std::string readLine, temp;
  1042. int nCharToRead, nCharRead;
  1043. readLine.clear();
  1044. nCharToRead = line.size();
  1045. for (int i = 0; i < nTries; i++) {
  1046. nCharRead = child.read(temp, nCharToRead);
  1047. readLine += temp;
  1048. nCharToRead -= nCharRead;
  1049. if (nCharToRead <= 0) {
  1050. break;
  1051. }
  1052. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1053. }
  1054. if (0 != nCharToRead) {
  1055. std::ostringstream temp;
  1056. temp << " Failure from () on line " << nLine
  1057. << ": nCharRead: " << nCharRead;
  1058. RETURN_FALSE(temp.str());
  1059. }
  1060. // Convert to upper case.
  1061. std::string expectedLine;
  1062. expectedLine = line;
  1063. for (auto &c : expectedLine) {
  1064. c = toupper(c);
  1065. }
  1066. // Compare.
  1067. if (expectedLine != readLine) {
  1068. std::cout << " Failure in testReadWrite." << std::endl;
  1069. std::cout << " Expected: '" << expectedLine
  1070. << "'\n Received: '" << readLine << "'" << std::endl;
  1071. return false;
  1072. }
  1073. }
  1074. // Send exit message.
  1075. if (!child.write("q")) {
  1076. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  1077. }
  1078. // Verify exit.
  1079. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  1080. if (!child.isDone()) {
  1081. std::cout << " Failure in testReadWrite: Child program did not exit."
  1082. << std::endl;
  1083. return false;
  1084. }
  1085. } catch (std::exception &e) {
  1086. EXCEPTION_TERM("read()/write()/isFinishedWriting()");
  1087. return false;
  1088. }
  1089. return true;
  1090. }
  1091. bool testChildIsFinishedWriting() {
  1092. std::vector<std::string> cmd;
  1093. cmd.push_back(childName);
  1094. size_t bufSize = 16;
  1095. std::uint16_t
  1096. nominalAboveMin_ms = 100,
  1097. delta_ms = 0;
  1098. try {
  1099. CodeDweller::Child child(cmd,
  1100. bufSize,
  1101. nominalAboveMin_ms,
  1102. delta_ms);
  1103. if (!child.isFinishedWriting()) {
  1104. RETURN_FALSE(" isFinishedWriting() failure");
  1105. }
  1106. if (!child.write("0123456789")) {
  1107. RETURN_FALSE(" write() failure");
  1108. }
  1109. // Busy wait until the data is written.
  1110. while (!child.isFinishedWriting())
  1111. ;
  1112. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1113. // Writer thread should be pausing now.
  1114. if (!child.write("0123456789")) {
  1115. RETURN_FALSE(" write() failure");
  1116. }
  1117. if (child.isFinishedWriting()) {
  1118. RETURN_FALSE(" isFinishedWriting() failure");
  1119. }
  1120. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  1121. if (child.isFinishedWriting()) {
  1122. RETURN_FALSE(" isFinishedWriting() failure");
  1123. }
  1124. std::this_thread::sleep_for(std::chrono::milliseconds(80));
  1125. if (!child.isFinishedWriting()) {
  1126. RETURN_FALSE(" isFinishedWriting() failure");
  1127. }
  1128. // Send exit message.
  1129. if (!child.write("q")) {
  1130. RETURN_FALSE(" write() failure");
  1131. }
  1132. // Verify exit.
  1133. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1134. if (!child.isDone()) {
  1135. std::cout << " Failure in testIsFinishedWriting: "
  1136. << "Child program did not exit."
  1137. << std::endl;
  1138. return false;
  1139. }
  1140. } catch (std::exception &e) {
  1141. EXCEPTION_TERM("write()/isFinishedWriting()");
  1142. return false;
  1143. }
  1144. return true;
  1145. }
  1146. bool testChildRead() {
  1147. try {
  1148. size_t bufSize = 32;
  1149. CodeDweller::Child child(bufSize);
  1150. // Test exception.
  1151. try {
  1152. std::string temp;
  1153. (void) child.read(temp);
  1154. NO_EXCEPTION_TERM(" read() called without open()");
  1155. return false;
  1156. } catch (std::exception &e) {
  1157. }
  1158. std::ostringstream childOutput;
  1159. std::string expectedChildOutput("This is a test");
  1160. std::string readBuf;
  1161. std::vector<std::string> cmd;
  1162. cmd.push_back(childName);
  1163. cmd.push_back("write");
  1164. child.open(cmd);
  1165. int nTries = 100;
  1166. size_t nRead = 0;
  1167. for (int i = 0; i < nTries; i++) {
  1168. nRead += child.read(readBuf);
  1169. childOutput << readBuf;
  1170. if (expectedChildOutput.size() == nRead) {
  1171. break;
  1172. }
  1173. if (child.errorOccurred(readBuf)) {
  1174. std::ostringstream temp;
  1175. temp << " Failure in testReader on try " << i << ": " << readBuf;
  1176. RETURN_FALSE(temp.str());
  1177. }
  1178. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1179. }
  1180. // Check.
  1181. if (childOutput.str() != expectedChildOutput) {
  1182. std::cout << " read() failure in testChildRead." << std::endl;
  1183. std::cout << " Expected: '" << expectedChildOutput
  1184. << "'\n Received: '" << childOutput.str() << "'"
  1185. << std::endl;
  1186. return false;
  1187. }
  1188. // Sleep to let the child exit.
  1189. for (int i = 0; i < 100; i++) {
  1190. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1191. if (child.isDone())
  1192. break;
  1193. }
  1194. if (!child.isDone()) {
  1195. std::cout << "first isDone() failure in testReader." << std::endl;
  1196. return false;
  1197. }
  1198. if (!child.isDone()) {
  1199. std::cout << "second isDone() failure in testReader." << std::endl;
  1200. return false;
  1201. }
  1202. child.close();
  1203. } catch (std::exception &e) {
  1204. EXCEPTION_TERM("reader()");
  1205. return false;
  1206. }
  1207. return true;
  1208. }
  1209. bool testChildNonBlockingReadWrite1() {
  1210. try {
  1211. std::vector<std::string> cmd;
  1212. cmd.push_back(childName);
  1213. size_t bufSize = 16;
  1214. std::uint16_t
  1215. nominalAboveMin_ms = 15,
  1216. delta_ms = 0;
  1217. CodeDweller::Child child(cmd,
  1218. bufSize,
  1219. nominalAboveMin_ms,
  1220. delta_ms);
  1221. // Check for available input with input.
  1222. std::string childInput("abc");
  1223. if (!child.write(childInput)) {
  1224. RETURN_FALSE(" first write() failure");
  1225. }
  1226. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1227. // Read one character.
  1228. std::string readBuf;
  1229. if (child.read(readBuf, 1) != 1) {
  1230. RETURN_FALSE(" read() failure");
  1231. }
  1232. if ("A" != readBuf) {
  1233. RETURN_FALSE(" read() failure");
  1234. }
  1235. // Read.
  1236. if (child.read(readBuf, 2) != 2) {
  1237. RETURN_FALSE(" read() failure");
  1238. }
  1239. if ("BC" != readBuf) {
  1240. RETURN_FALSE(" read() failure");
  1241. }
  1242. // Check that no input is available.
  1243. if (child.read(readBuf) != 0) {
  1244. RETURN_FALSE(" read() failure");
  1245. }
  1246. if (!readBuf.empty()) {
  1247. RETURN_FALSE(" read() failure");
  1248. }
  1249. // Fill input buffer.
  1250. std::string output("abcdefghijklmnopprstuvwxyz");
  1251. std::string expectedInput(output);
  1252. std::string expectedOutputAfter1(output.substr(bufSize));
  1253. for (int i = 0; i < output.size(); i++) {
  1254. expectedInput[i] = std::toupper(output[i]);
  1255. }
  1256. if (child.writeAndShrink(output) != bufSize) {
  1257. RETURN_FALSE(" writeAndShrink() failure");
  1258. }
  1259. if (output != expectedOutputAfter1) {
  1260. RETURN_FALSE(" writeAndShrink() failure");
  1261. }
  1262. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1263. int expectedLeftOver = output.size();
  1264. if (child.writeAndShrink(output) != expectedLeftOver) {
  1265. RETURN_FALSE(" writeAndShrink() failure");
  1266. }
  1267. if (!output.empty()) {
  1268. RETURN_FALSE(" writeAndShrink() failure");
  1269. }
  1270. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1271. size_t nRead = child.read(readBuf);
  1272. if (nRead != bufSize) {
  1273. RETURN_FALSE(" read() failure");
  1274. }
  1275. if (readBuf != expectedInput.substr(0, bufSize)) {
  1276. RETURN_FALSE(" read() failure)");
  1277. }
  1278. // Wait for reader thread to fill input buffer.
  1279. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  1280. if (child.read(readBuf) != expectedLeftOver) {
  1281. RETURN_FALSE(" read() failure");
  1282. }
  1283. if (expectedInput.substr(bufSize) != readBuf) {
  1284. RETURN_FALSE(" read() failure");
  1285. }
  1286. // Send exit message.
  1287. if (!child.write("q")) {
  1288. RETURN_FALSE(" write() failure");
  1289. }
  1290. // Verify exit.
  1291. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1292. if (!child.isDone()) {
  1293. std::cout << " Failure in testChildNonblockingReadWrite1: "
  1294. << "Child program did not exit." << std::endl;
  1295. return false;
  1296. }
  1297. std::string errorDescription;
  1298. if (child.errorOccurred(errorDescription)) {
  1299. std::ostringstream temp;
  1300. temp << " Failure in: testChildNonBlockingReadWrite1: "
  1301. << errorDescription;
  1302. RETURN_FALSE(temp.str());
  1303. }
  1304. child.close();
  1305. } catch (std::exception &e) {
  1306. EXCEPTION_TERM("Non-blocking reader test");
  1307. return false;
  1308. }
  1309. return true;
  1310. }
  1311. bool testChildNonBlockingReadWrite2() {
  1312. size_t bufSize, maxChar;
  1313. std::uint16_t
  1314. nominalAboveMin_ms = 0,
  1315. delta_ms = 0;
  1316. bufSize = 16;
  1317. maxChar = 1000 * 100;
  1318. std::cout << "\n many small buffers";
  1319. std::cout.flush();
  1320. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1321. RETURN_FALSE(" read()/write() failure");
  1322. }
  1323. bufSize = 128 * 1024;
  1324. maxChar = 1000 * 10;
  1325. std::cout << "\n one big buffer";
  1326. std::cout.flush();
  1327. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1328. RETURN_FALSE(" read()/write() failure");
  1329. }
  1330. bufSize = 128 * 1024;
  1331. maxChar = 1000 * 1000 * 10;
  1332. std::cout << "\n many big buffers";
  1333. std::cout.flush();
  1334. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1335. RETURN_FALSE(" read()/write() failure");
  1336. }
  1337. return true;
  1338. }
  1339. bool testChildVectorReadWrite() {
  1340. try {
  1341. std::vector<std::string> cmd;
  1342. cmd.push_back(childName);
  1343. size_t bufSize = 16;
  1344. std::uint16_t
  1345. nominalAboveMin_ms = 15,
  1346. delta_ms = 0;
  1347. CodeDweller::Child child(cmd,
  1348. bufSize,
  1349. nominalAboveMin_ms,
  1350. delta_ms);
  1351. // Check that write fails.
  1352. std::vector<char> badVector(bufSize + 1);
  1353. if (child.write(badVector)) {
  1354. RETURN_FALSE(" write() failure");
  1355. }
  1356. // Check write().
  1357. std::vector<char> childInput = {'a', 'b', 'c'};
  1358. if (!child.write(childInput)) {
  1359. RETURN_FALSE(" write() failure");
  1360. }
  1361. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1362. // Read one character.
  1363. std::vector<char> readBuf;
  1364. if (child.read(readBuf, 1) != 1) {
  1365. RETURN_FALSE(" read() failure");
  1366. }
  1367. if ('A' != readBuf[0]) {
  1368. RETURN_FALSE(" read() failure");
  1369. }
  1370. // Read.
  1371. if (child.read(readBuf, 2) != 2) {
  1372. RETURN_FALSE(" read() failure");
  1373. }
  1374. if (std::vector<char>({'B', 'C'}) != readBuf) {
  1375. RETURN_FALSE(" read() failure");
  1376. }
  1377. // Check that no input is available.
  1378. if (child.read(readBuf) != 0) {
  1379. RETURN_FALSE(" read() failure");
  1380. }
  1381. if (!readBuf.empty()) {
  1382. RETURN_FALSE(" read() failure");
  1383. }
  1384. // Fill input buffer.
  1385. std::vector<char> output = {
  1386. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  1387. 'n', 'o', 'p', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  1388. };
  1389. std::vector<char> expectedInput(output);
  1390. for (int i = 0; i < output.size(); i++) {
  1391. expectedInput[i] = std::toupper(output[i]);
  1392. }
  1393. if (child.write(output, bufSize) != bufSize) {
  1394. RETURN_FALSE(" write() failure");
  1395. }
  1396. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1397. size_t nRequested = bufSize / 2;
  1398. // Check partial read.
  1399. size_t nRead = child.read(readBuf, nRequested);
  1400. if (nRead != nRequested) {
  1401. RETURN_FALSE(" read() failure");
  1402. }
  1403. if (readBuf != std::vector<char>(expectedInput.begin(),
  1404. expectedInput.begin() + nRead)) {
  1405. RETURN_FALSE(" read() failure)");
  1406. }
  1407. // Check remainder.
  1408. readBuf.resize(bufSize * 2);
  1409. nRead = child.read(readBuf);
  1410. if ( (bufSize - nRequested) != nRead) {
  1411. RETURN_FALSE(" read() failure");
  1412. }
  1413. if (readBuf != std::vector<char>(expectedInput.begin() + nRequested,
  1414. expectedInput.begin() + bufSize)) {
  1415. RETURN_FALSE(" read() failure)");
  1416. }
  1417. // Write remainder.
  1418. output.erase(output.begin(),
  1419. output.begin() + bufSize);
  1420. expectedInput.erase(expectedInput.begin(),
  1421. expectedInput.begin() + bufSize);
  1422. size_t nExpectedBytes = output.size();
  1423. if (child.writeAndShrink(output) != nExpectedBytes) {
  1424. RETURN_FALSE(" writeAndShrink() failure");
  1425. }
  1426. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1427. if (child.read(readBuf) != nExpectedBytes) {
  1428. RETURN_FALSE(" read() failure");
  1429. }
  1430. if (readBuf != expectedInput) {
  1431. RETURN_FALSE(" read() failure");
  1432. }
  1433. // Send exit message.
  1434. if (!child.write("q")) {
  1435. RETURN_FALSE(" write() failure");
  1436. }
  1437. // Verify exit.
  1438. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1439. if (!child.isDone()) {
  1440. std::cout << " Failure in testChildVectorReadWrite: "
  1441. << "Child program did not exit." << std::endl;
  1442. return false;
  1443. }
  1444. std::string errorDescription;
  1445. if (child.errorOccurred(errorDescription)) {
  1446. std::ostringstream temp;
  1447. temp << " Failure in: testChildVectorReadWrite: "
  1448. << errorDescription;
  1449. RETURN_FALSE(temp.str());
  1450. }
  1451. child.close();
  1452. } catch (std::exception &e) {
  1453. EXCEPTION_TERM("testChildVectorReadWrite");
  1454. return false;
  1455. }
  1456. return true;
  1457. }
  1458. bool testChildReadDelimited() {
  1459. try {
  1460. std::vector<std::string> cmd;
  1461. cmd.push_back(childName);
  1462. size_t bufSize = 16;
  1463. std::uint16_t
  1464. nominalAboveMin_ms = 15,
  1465. delta_ms = 0;
  1466. CodeDweller::Child child(cmd,
  1467. bufSize,
  1468. nominalAboveMin_ms,
  1469. delta_ms);
  1470. // Check delimiter at beginning of buffer.
  1471. std::vector<char> childInput = {'a', 'b', 'c', 'd', 'e'};
  1472. if (!child.write(childInput)) {
  1473. RETURN_FALSE(" write() failure");
  1474. }
  1475. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1476. // Check
  1477. std::vector<char> readVec;
  1478. if (!child.readDelimited(readVec, "AB")) {
  1479. RETURN_FALSE(" readDelimited() failure");
  1480. }
  1481. if (!readVec.empty()) {
  1482. RETURN_FALSE(" readDelimited() failure");
  1483. }
  1484. if (child.readDelimited(readVec, "n")) {
  1485. RETURN_FALSE(" readDelimited() failure");
  1486. }
  1487. if (child.readDelimited(readVec, "nlelk;lqm;foim")) {
  1488. RETURN_FALSE(" readDelimited() failure");
  1489. }
  1490. // Empty buffer.
  1491. std::string temp;
  1492. (void) child.read(temp);
  1493. if ("CDE" != temp) {
  1494. RETURN_FALSE(" readDelimited() failure");
  1495. }
  1496. // Check delimiter at middle of buffer.
  1497. if (!child.write(childInput)) {
  1498. RETURN_FALSE(" write() failure");
  1499. }
  1500. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1501. // Check
  1502. std::string readStr;
  1503. std::string delim = "CD";
  1504. if (!child.readDelimited(readStr, delim)) {
  1505. RETURN_FALSE(" readDelimited() failure");
  1506. }
  1507. if ("AB" != readStr) {
  1508. RETURN_FALSE(" readDelimited() failure");
  1509. }
  1510. delim = "n";
  1511. if (child.readDelimited(readStr, delim)) {
  1512. RETURN_FALSE(" readDelimited() failure");
  1513. }
  1514. delim = "fimsoim3m4";
  1515. if (child.readDelimited(readStr, delim)) {
  1516. RETURN_FALSE(" readDelimited() failure");
  1517. }
  1518. readStr = "orirw";
  1519. delim = "";
  1520. if (!child.readDelimited(readStr, delim)) {
  1521. RETURN_FALSE(" readDelimited() failure");
  1522. }
  1523. if (!readStr.empty()) {
  1524. RETURN_FALSE(" readDelimited() failure");
  1525. }
  1526. // Empty buffer.
  1527. (void) child.read(temp);
  1528. if ("E" != temp) {
  1529. RETURN_FALSE(" readDelimited() failure");
  1530. }
  1531. // Check delimiter at end of buffer.
  1532. if (!child.write(childInput)) {
  1533. RETURN_FALSE(" write() failure");
  1534. }
  1535. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1536. // Check
  1537. std::vector<char> delimVec = {'E'};
  1538. if (!child.readDelimited(readStr, delimVec)) {
  1539. RETURN_FALSE(" readDelimited() failure");
  1540. }
  1541. if ("ABCD" != readStr) {
  1542. RETURN_FALSE(" readDelimited() failure");
  1543. }
  1544. delimVec[0] = 'n';
  1545. if (child.readDelimited(readStr, delimVec)) {
  1546. RETURN_FALSE(" readDelimited() failure");
  1547. }
  1548. delimVec.push_back('4');
  1549. delimVec.push_back(',');
  1550. delimVec.push_back('m');
  1551. delimVec.push_back('y');
  1552. delimVec.push_back('t');
  1553. delimVec.push_back('2');
  1554. delimVec.push_back('e');
  1555. delimVec.push_back('t');
  1556. if (child.readDelimited(readStr, delimVec)) {
  1557. RETURN_FALSE(" readDelimited() failure");
  1558. }
  1559. // Empty buffer.
  1560. (void) child.read(temp);
  1561. if ("" != temp) {
  1562. RETURN_FALSE(" readDelimited() failure");
  1563. }
  1564. // Send exit message.
  1565. if (!child.write("q")) {
  1566. RETURN_FALSE(" write() failure");
  1567. }
  1568. // Verify exit.
  1569. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  1570. if (!child.isDone()) {
  1571. std::cout << " Failure in testChildReadDelimited: "
  1572. << "Child program did not exit." << std::endl;
  1573. return false;
  1574. }
  1575. std::string errorDescription;
  1576. if (child.errorOccurred(errorDescription)) {
  1577. std::ostringstream temp;
  1578. temp << " Failure in: testChildReadDelimited: "
  1579. << errorDescription;
  1580. RETURN_FALSE(temp.str());
  1581. }
  1582. child.close();
  1583. } catch (std::exception &e) {
  1584. EXCEPTION_TERM("testChildReadDelimited");
  1585. return false;
  1586. }
  1587. return true;
  1588. }
  1589. ////////////////////////////////////////////////////////////////////////////////
  1590. // End of tests ////////////////////////////////////////////////////////////////
  1591. ////////////////////////////////////////////////////////////////////////////////
  1592. int main()
  1593. {
  1594. // ChildStream is not supported.
  1595. #if 0
  1596. std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
  1597. RUN_TEST(testChildStreamIsDone);
  1598. RUN_TEST(testChildStreamIsRunning);
  1599. RUN_TEST(testChildStreamResult);
  1600. RUN_TEST(testChildStreamClose);
  1601. RUN_TEST(testChildStreamReader);
  1602. RUN_TEST(testChildStreamReaderWriter);
  1603. RUN_TEST(testChildStreamBinaryRead);
  1604. RUN_TEST(testChildStreamNonBlockingRead);
  1605. #endif
  1606. std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
  1607. RUN_TEST(testChildIsDone);
  1608. RUN_TEST(testChildIsRunning);
  1609. RUN_TEST(testChildResult);
  1610. RUN_TEST(testChildClose);
  1611. RUN_TEST(testChildStdInClose);
  1612. RUN_TEST(testChildrenStdInClose);
  1613. RUN_TEST(testChildOpen);
  1614. RUN_TEST(testChildIsFinishedWriting);
  1615. RUN_TEST(testChildRead);
  1616. RUN_TEST(testChildReadWrite);
  1617. RUN_TEST(testChildNonBlockingReadWrite1);
  1618. RUN_TEST(testChildNonBlockingReadWrite2);
  1619. RUN_TEST(testChildVectorReadWrite);
  1620. RUN_TEST(testChildReadDelimited);
  1621. SUMMARY;
  1622. return 0;
  1623. }