You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testChild.cpp 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  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(100));
  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(100));
  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(100));
  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(100));
  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(100));
  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(100));
  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(100));
  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 testChildOpen() {
  792. // Test with no waiting.
  793. try {
  794. CodeDweller::Child child(childName);
  795. child.close();
  796. child.open(childName);
  797. child.close();
  798. } catch (std::exception &e) {
  799. EXCEPTION_TERM("close()/open() with no waiting");
  800. return false;
  801. }
  802. // Test with waiting.
  803. try {
  804. CodeDweller::Child child(childName);
  805. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  806. child.close();
  807. child.open(childName);
  808. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  809. child.close();
  810. } catch (std::exception &e) {
  811. EXCEPTION_TERM("close()/open() with 100 ms waiting");
  812. return false;
  813. }
  814. // Test exception thrown for out-of-order calling.
  815. try {
  816. CodeDweller::Child child;
  817. child.close();
  818. NO_EXCEPTION_TERM("close() called without open()");
  819. return false;
  820. } catch (std::exception &e) {
  821. }
  822. return true;
  823. }
  824. bool testChildReadWrite() {
  825. size_t bufSize = 15;
  826. int nChar = bufSize - 1; // Size of each packet.
  827. try {
  828. CodeDweller::Child child(bufSize);
  829. std::ostringstream childOutput;
  830. std::vector<std::string> expectedChildOutput;
  831. char readChar;
  832. // Generate input.
  833. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  834. int nChar = bufSize - 1;
  835. int nLines = 2;
  836. for (int iLine = 0; iLine < nLines; iLine++) {
  837. std::string line;
  838. line.erase();
  839. for (int iChar = 0; iChar < nChar; iChar++) {
  840. line.push_back(randomLetter[std::rand() % 26]);
  841. }
  842. expectedChildOutput.push_back(line);
  843. }
  844. // Test exception.
  845. try {
  846. child.write("Should Throw");
  847. NO_EXCEPTION_TERM(" writer called without run()");
  848. return false;
  849. } catch (std::exception &e) {
  850. }
  851. child.open(childName);
  852. // Write, read test.
  853. int nLine = 0;
  854. for (std::string &line : expectedChildOutput) {
  855. nLine++;
  856. // Try to queue.
  857. bool done;
  858. int nTries = 100;
  859. for (int i = 0; i < nTries; i++) {
  860. done = child.write(line);
  861. if (done) {
  862. break;
  863. }
  864. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  865. }
  866. if (!done) {
  867. std::ostringstream temp;
  868. temp << " Failure from write(std::string const &) on line " << nLine;
  869. RETURN_FALSE(temp.str());
  870. }
  871. // Wait for transmission.
  872. for (int i = 0; i < nTries; i++) {
  873. done = child.isFinishedWriting();
  874. if (done) {
  875. break;
  876. }
  877. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  878. }
  879. if (!done) {
  880. std::ostringstream temp;
  881. temp << " Failure from isFinishedWriting() on line " << nLine;
  882. RETURN_FALSE(temp.str());
  883. }
  884. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  885. // Read one line.
  886. std::string readLine, temp;
  887. int nCharToRead, nCharRead;
  888. readLine.clear();
  889. nCharToRead = line.size();
  890. for (int i = 0; i < nTries; i++) {
  891. nCharRead = child.read(temp, nCharToRead);
  892. readLine += temp;
  893. nCharToRead -= nCharRead;
  894. if (nCharToRead <= 0) {
  895. break;
  896. }
  897. }
  898. if (0 != nCharToRead) {
  899. std::ostringstream temp;
  900. temp << " Failure from () on line " << nLine
  901. << ": nCharRead: " << nCharRead;
  902. RETURN_FALSE(temp.str());
  903. }
  904. // Convert to upper case.
  905. std::string expectedLine;
  906. expectedLine = line;
  907. for (auto &c : expectedLine) {
  908. c = toupper(c);
  909. }
  910. // Compare.
  911. if (expectedLine != readLine) {
  912. std::cout << " Failure in testReadWrite." << std::endl;
  913. std::cout << " Expected: '" << expectedLine
  914. << "'\n Received: '" << readLine << "'" << std::endl;
  915. return false;
  916. }
  917. }
  918. // Send exit message.
  919. if (!child.write("q")) {
  920. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  921. }
  922. // Verify exit.
  923. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  924. if (!child.isDone()) {
  925. std::cout << " Failure in testReadWrite: Child program did not exit."
  926. << std::endl;
  927. return false;
  928. }
  929. } catch (std::exception &e) {
  930. EXCEPTION_TERM("read()/write()/isFinishedWriting()");
  931. return false;
  932. }
  933. return true;
  934. }
  935. bool testChildIsFinishedWriting() {
  936. std::vector<std::string> cmd;
  937. cmd.push_back(childName);
  938. size_t bufSize = 16;
  939. std::uint16_t
  940. nominalAboveMin_ms = 100,
  941. delta_ms = 0;
  942. try {
  943. CodeDweller::Child child(cmd,
  944. bufSize,
  945. nominalAboveMin_ms,
  946. delta_ms);
  947. if (!child.isFinishedWriting()) {
  948. RETURN_FALSE(" isFinishedWriting() failure");
  949. }
  950. if (!child.write("0123456789")) {
  951. RETURN_FALSE(" write() failure");
  952. }
  953. // Busy wait until the data is written.
  954. while (!child.isFinishedWriting())
  955. ;
  956. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  957. // Writer thread should be pausing now.
  958. if (!child.write("0123456789")) {
  959. RETURN_FALSE(" write() failure");
  960. }
  961. if (child.isFinishedWriting()) {
  962. RETURN_FALSE(" isFinishedWriting() failure");
  963. }
  964. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  965. if (child.isFinishedWriting()) {
  966. RETURN_FALSE(" isFinishedWriting() failure");
  967. }
  968. std::this_thread::sleep_for(std::chrono::milliseconds(80));
  969. if (!child.isFinishedWriting()) {
  970. RETURN_FALSE(" isFinishedWriting() failure");
  971. }
  972. // Send exit message.
  973. if (!child.write("q")) {
  974. RETURN_FALSE(" write() failure");
  975. }
  976. // Verify exit.
  977. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  978. if (!child.isDone()) {
  979. std::cout << " Failure in testIsFinishedWriting: "
  980. << "Child program did not exit."
  981. << std::endl;
  982. return false;
  983. }
  984. } catch (std::exception &e) {
  985. EXCEPTION_TERM("write()/isFinishedWriting()");
  986. return false;
  987. }
  988. return true;
  989. }
  990. bool testChildRead() {
  991. try {
  992. size_t bufSize = 32;
  993. CodeDweller::Child child(bufSize);
  994. // Test exception.
  995. try {
  996. std::string temp;
  997. (void) child.read(temp);
  998. NO_EXCEPTION_TERM(" read() called without open()");
  999. return false;
  1000. } catch (std::exception &e) {
  1001. }
  1002. std::ostringstream childOutput;
  1003. std::string expectedChildOutput("This is a test");
  1004. std::string readBuf;
  1005. std::vector<std::string> cmd;
  1006. cmd.push_back(childName);
  1007. cmd.push_back("write");
  1008. child.open(cmd);
  1009. int nTries = 100;
  1010. size_t nRead = 0;
  1011. for (int i = 0; i < nTries; i++) {
  1012. nRead += child.read(readBuf);
  1013. childOutput << readBuf;
  1014. if (expectedChildOutput.size() == nRead) {
  1015. break;
  1016. }
  1017. if (child.errorOccurred(readBuf)) {
  1018. std::ostringstream temp;
  1019. temp << " Failure in testReader on try " << i << ": " << readBuf;
  1020. RETURN_FALSE(temp.str());
  1021. }
  1022. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1023. }
  1024. // Check.
  1025. if (childOutput.str() != expectedChildOutput) {
  1026. std::cout << " read() failure in testChildRead." << std::endl;
  1027. std::cout << " Expected: '" << expectedChildOutput
  1028. << "'\n Received: '" << childOutput.str() << "'"
  1029. << std::endl;
  1030. return false;
  1031. }
  1032. // Sleep to let the child exit.
  1033. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1034. if (!child.isDone()) {
  1035. std::cout << "first isDone() failure in testReader." << std::endl;
  1036. return false;
  1037. }
  1038. if (!child.isDone()) {
  1039. std::cout << "second isDone() failure in testReader." << std::endl;
  1040. return false;
  1041. }
  1042. child.close();
  1043. } catch (std::exception &e) {
  1044. EXCEPTION_TERM("reader()");
  1045. return false;
  1046. }
  1047. return true;
  1048. }
  1049. bool testChildNonBlockingReadWrite1() {
  1050. try {
  1051. std::vector<std::string> cmd;
  1052. cmd.push_back(childName);
  1053. size_t bufSize = 16;
  1054. std::uint16_t
  1055. nominalAboveMin_ms = 15,
  1056. delta_ms = 0;
  1057. CodeDweller::Child child(cmd,
  1058. bufSize,
  1059. nominalAboveMin_ms,
  1060. delta_ms);
  1061. // Check for available input with input.
  1062. std::string childInput("abc");
  1063. if (!child.write(childInput)) {
  1064. RETURN_FALSE(" first write() failure");
  1065. }
  1066. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1067. // Read one character.
  1068. std::string readBuf;
  1069. if (child.read(readBuf, 1) != 1) {
  1070. RETURN_FALSE(" read() failure");
  1071. }
  1072. if ("A" != readBuf) {
  1073. RETURN_FALSE(" read() failure");
  1074. }
  1075. // Read.
  1076. if (child.read(readBuf, 2) != 2) {
  1077. RETURN_FALSE(" read() failure");
  1078. }
  1079. if ("BC" != readBuf) {
  1080. RETURN_FALSE(" read() failure");
  1081. }
  1082. // Check that no input is available.
  1083. if (child.read(readBuf) != 0) {
  1084. RETURN_FALSE(" read() failure");
  1085. }
  1086. if (!readBuf.empty()) {
  1087. RETURN_FALSE(" read() failure");
  1088. }
  1089. // Fill input buffer.
  1090. std::string output("abcdefghijklmnopprstuvwxyz");
  1091. std::string expectedInput(output);
  1092. std::string expectedOutputAfter1(output.substr(bufSize));
  1093. for (int i = 0; i < output.size(); i++) {
  1094. expectedInput[i] = std::toupper(output[i]);
  1095. }
  1096. if (child.writeAndShrink(output) != bufSize) {
  1097. RETURN_FALSE(" writeAndShrink() failure");
  1098. }
  1099. if (output != expectedOutputAfter1) {
  1100. RETURN_FALSE(" writeAndShrink() failure");
  1101. }
  1102. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1103. int expectedLeftOver = output.size();
  1104. if (child.writeAndShrink(output) != expectedLeftOver) {
  1105. RETURN_FALSE(" writeAndShrink() failure");
  1106. }
  1107. if (!output.empty()) {
  1108. RETURN_FALSE(" writeAndShrink() failure");
  1109. }
  1110. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1111. size_t nRead = child.read(readBuf);
  1112. if (nRead != bufSize) {
  1113. RETURN_FALSE(" read() failure");
  1114. }
  1115. if (readBuf != expectedInput.substr(0, bufSize)) {
  1116. RETURN_FALSE(" read() failure)");
  1117. }
  1118. // Wait for reader thread to fill input buffer.
  1119. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1120. if (child.read(readBuf) != expectedLeftOver) {
  1121. RETURN_FALSE(" read() failure");
  1122. }
  1123. if (expectedInput.substr(bufSize) != readBuf) {
  1124. RETURN_FALSE(" read() failure");
  1125. }
  1126. // Send exit message.
  1127. if (!child.write("q")) {
  1128. RETURN_FALSE(" write() failure");
  1129. }
  1130. // Verify exit.
  1131. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1132. if (!child.isDone()) {
  1133. std::cout << " Failure in testChildNonblockingReadWrite1: "
  1134. << "Child program did not exit." << std::endl;
  1135. return false;
  1136. }
  1137. std::string errorDescription;
  1138. if (child.errorOccurred(errorDescription)) {
  1139. std::ostringstream temp;
  1140. temp << " Failure in: testChildNonBlockingReadWrite1: "
  1141. << errorDescription;
  1142. RETURN_FALSE(temp.str());
  1143. }
  1144. child.close();
  1145. } catch (std::exception &e) {
  1146. EXCEPTION_TERM("Non-blocking reader test");
  1147. return false;
  1148. }
  1149. return true;
  1150. }
  1151. bool testChildNonBlockingReadWrite2() {
  1152. size_t bufSize, maxChar;
  1153. std::uint16_t
  1154. nominalAboveMin_ms = 0,
  1155. delta_ms = 0;
  1156. bufSize = 16;
  1157. maxChar = 1000 * 100;
  1158. std::cout << "\n many small buffers";
  1159. std::cout.flush();
  1160. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1161. RETURN_FALSE(" read()/write() failure");
  1162. }
  1163. bufSize = 128 * 1024;
  1164. maxChar = 1000 * 10;
  1165. std::cout << "\n one big buffer";
  1166. std::cout.flush();
  1167. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1168. RETURN_FALSE(" read()/write() failure");
  1169. }
  1170. bufSize = 128 * 1024;
  1171. maxChar = 1000 * 1000 * 10;
  1172. std::cout << "\n many big buffers";
  1173. std::cout.flush();
  1174. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1175. RETURN_FALSE(" read()/write() failure");
  1176. }
  1177. return true;
  1178. }
  1179. bool testChildVectorReadWrite() {
  1180. try {
  1181. std::vector<std::string> cmd;
  1182. cmd.push_back(childName);
  1183. size_t bufSize = 16;
  1184. std::uint16_t
  1185. nominalAboveMin_ms = 15,
  1186. delta_ms = 0;
  1187. CodeDweller::Child child(cmd,
  1188. bufSize,
  1189. nominalAboveMin_ms,
  1190. delta_ms);
  1191. // Check that write fails.
  1192. std::vector<char> badVector(bufSize + 1);
  1193. if (child.write(badVector)) {
  1194. RETURN_FALSE(" write() failure");
  1195. }
  1196. // Check write().
  1197. std::vector<char> childInput = {'a', 'b', 'c'};
  1198. if (!child.write(childInput)) {
  1199. RETURN_FALSE(" write() failure");
  1200. }
  1201. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1202. // Read one character.
  1203. std::vector<char> readBuf;
  1204. if (child.read(readBuf, 1) != 1) {
  1205. RETURN_FALSE(" read() failure");
  1206. }
  1207. if ('A' != readBuf[0]) {
  1208. RETURN_FALSE(" read() failure");
  1209. }
  1210. // Read.
  1211. if (child.read(readBuf, 2) != 2) {
  1212. RETURN_FALSE(" read() failure");
  1213. }
  1214. if (std::vector<char>({'B', 'C'}) != readBuf) {
  1215. RETURN_FALSE(" read() failure");
  1216. }
  1217. // Check that no input is available.
  1218. if (child.read(readBuf) != 0) {
  1219. RETURN_FALSE(" read() failure");
  1220. }
  1221. if (!readBuf.empty()) {
  1222. RETURN_FALSE(" read() failure");
  1223. }
  1224. // Fill input buffer.
  1225. std::vector<char> output = {
  1226. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  1227. 'n', 'o', 'p', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  1228. };
  1229. std::vector<char> expectedInput(output);
  1230. for (int i = 0; i < output.size(); i++) {
  1231. expectedInput[i] = std::toupper(output[i]);
  1232. }
  1233. if (child.write(output, bufSize) != bufSize) {
  1234. RETURN_FALSE(" write() failure");
  1235. }
  1236. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1237. size_t nRequested = bufSize / 2;
  1238. // Check partial read.
  1239. size_t nRead = child.read(readBuf, nRequested);
  1240. if (nRead != nRequested) {
  1241. RETURN_FALSE(" read() failure");
  1242. }
  1243. if (readBuf != std::vector<char>(expectedInput.begin(),
  1244. expectedInput.begin() + nRead)) {
  1245. RETURN_FALSE(" read() failure)");
  1246. }
  1247. // Check remainder.
  1248. readBuf.resize(bufSize * 2);
  1249. nRead = child.read(readBuf);
  1250. if ( (bufSize - nRequested) != nRead) {
  1251. RETURN_FALSE(" read() failure");
  1252. }
  1253. if (readBuf != std::vector<char>(expectedInput.begin() + nRequested,
  1254. expectedInput.begin() + bufSize)) {
  1255. RETURN_FALSE(" read() failure)");
  1256. }
  1257. // Write remainder.
  1258. output.erase(output.begin(),
  1259. output.begin() + bufSize);
  1260. expectedInput.erase(expectedInput.begin(),
  1261. expectedInput.begin() + bufSize);
  1262. size_t nExpectedBytes = output.size();
  1263. if (child.writeAndShrink(output) != nExpectedBytes) {
  1264. RETURN_FALSE(" writeAndShrink() failure");
  1265. }
  1266. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1267. if (child.read(readBuf) != nExpectedBytes) {
  1268. RETURN_FALSE(" read() failure");
  1269. }
  1270. if (readBuf != expectedInput) {
  1271. RETURN_FALSE(" read() failure");
  1272. }
  1273. // Send exit message.
  1274. if (!child.write("q")) {
  1275. RETURN_FALSE(" write() failure");
  1276. }
  1277. // Verify exit.
  1278. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1279. if (!child.isDone()) {
  1280. std::cout << " Failure in testChildVectorReadWrite: "
  1281. << "Child program did not exit." << std::endl;
  1282. return false;
  1283. }
  1284. std::string errorDescription;
  1285. if (child.errorOccurred(errorDescription)) {
  1286. std::ostringstream temp;
  1287. temp << " Failure in: testChildVectorReadWrite: "
  1288. << errorDescription;
  1289. RETURN_FALSE(temp.str());
  1290. }
  1291. child.close();
  1292. } catch (std::exception &e) {
  1293. EXCEPTION_TERM("testChildVectorReadWrite");
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. bool testChildReadDelimited() {
  1299. try {
  1300. std::vector<std::string> cmd;
  1301. cmd.push_back(childName);
  1302. size_t bufSize = 16;
  1303. std::uint16_t
  1304. nominalAboveMin_ms = 15,
  1305. delta_ms = 0;
  1306. CodeDweller::Child child(cmd,
  1307. bufSize,
  1308. nominalAboveMin_ms,
  1309. delta_ms);
  1310. // Check delimiter at beginning of buffer.
  1311. std::vector<char> childInput = {'a', 'b', 'c', 'd', 'e'};
  1312. if (!child.write(childInput)) {
  1313. RETURN_FALSE(" write() failure");
  1314. }
  1315. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1316. // Check
  1317. std::vector<char> readVec;
  1318. if (!child.readDelimited(readVec, "AB")) {
  1319. RETURN_FALSE(" readDelimited() failure");
  1320. }
  1321. if (!readVec.empty()) {
  1322. RETURN_FALSE(" readDelimited() failure");
  1323. }
  1324. if (child.readDelimited(readVec, "n")) {
  1325. RETURN_FALSE(" readDelimited() failure");
  1326. }
  1327. if (child.readDelimited(readVec, "nlelk;lqm;foim")) {
  1328. RETURN_FALSE(" readDelimited() failure");
  1329. }
  1330. // Empty buffer.
  1331. std::string temp;
  1332. (void) child.read(temp);
  1333. if ("CDE" != temp) {
  1334. RETURN_FALSE(" readDelimited() failure");
  1335. }
  1336. // Check delimiter at middle of buffer.
  1337. if (!child.write(childInput)) {
  1338. RETURN_FALSE(" write() failure");
  1339. }
  1340. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1341. // Check
  1342. std::string readStr;
  1343. std::string delim = "CD";
  1344. if (!child.readDelimited(readStr, delim)) {
  1345. RETURN_FALSE(" readDelimited() failure");
  1346. }
  1347. if ("AB" != readStr) {
  1348. RETURN_FALSE(" readDelimited() failure");
  1349. }
  1350. delim = "n";
  1351. if (child.readDelimited(readStr, delim)) {
  1352. RETURN_FALSE(" readDelimited() failure");
  1353. }
  1354. delim = "fimsoim3m4";
  1355. if (child.readDelimited(readStr, delim)) {
  1356. RETURN_FALSE(" readDelimited() failure");
  1357. }
  1358. readStr = "orirw";
  1359. delim = "";
  1360. if (!child.readDelimited(readStr, delim)) {
  1361. RETURN_FALSE(" readDelimited() failure");
  1362. }
  1363. if (!readStr.empty()) {
  1364. RETURN_FALSE(" readDelimited() failure");
  1365. }
  1366. // Empty buffer.
  1367. (void) child.read(temp);
  1368. if ("E" != temp) {
  1369. RETURN_FALSE(" readDelimited() failure");
  1370. }
  1371. // Check delimiter at end of buffer.
  1372. if (!child.write(childInput)) {
  1373. RETURN_FALSE(" write() failure");
  1374. }
  1375. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  1376. // Check
  1377. std::vector<char> delimVec = {'E'};
  1378. if (!child.readDelimited(readStr, delimVec)) {
  1379. RETURN_FALSE(" readDelimited() failure");
  1380. }
  1381. if ("ABCD" != readStr) {
  1382. RETURN_FALSE(" readDelimited() failure");
  1383. }
  1384. delimVec[0] = 'n';
  1385. if (child.readDelimited(readStr, delimVec)) {
  1386. RETURN_FALSE(" readDelimited() failure");
  1387. }
  1388. delimVec.push_back('4');
  1389. delimVec.push_back(',');
  1390. delimVec.push_back('m');
  1391. delimVec.push_back('y');
  1392. delimVec.push_back('t');
  1393. delimVec.push_back('2');
  1394. delimVec.push_back('e');
  1395. delimVec.push_back('t');
  1396. if (child.readDelimited(readStr, delimVec)) {
  1397. RETURN_FALSE(" readDelimited() failure");
  1398. }
  1399. // Empty buffer.
  1400. (void) child.read(temp);
  1401. if ("" != temp) {
  1402. RETURN_FALSE(" readDelimited() failure");
  1403. }
  1404. // Send exit message.
  1405. if (!child.write("q")) {
  1406. RETURN_FALSE(" write() failure");
  1407. }
  1408. // Verify exit.
  1409. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1410. if (!child.isDone()) {
  1411. std::cout << " Failure in testChildReadDelimited: "
  1412. << "Child program did not exit." << std::endl;
  1413. return false;
  1414. }
  1415. std::string errorDescription;
  1416. if (child.errorOccurred(errorDescription)) {
  1417. std::ostringstream temp;
  1418. temp << " Failure in: testChildReadDelimited: "
  1419. << errorDescription;
  1420. RETURN_FALSE(temp.str());
  1421. }
  1422. child.close();
  1423. } catch (std::exception &e) {
  1424. EXCEPTION_TERM("testChildReadDelimited");
  1425. return false;
  1426. }
  1427. return true;
  1428. }
  1429. ////////////////////////////////////////////////////////////////////////////////
  1430. // End of tests ////////////////////////////////////////////////////////////////
  1431. ////////////////////////////////////////////////////////////////////////////////
  1432. int main()
  1433. {
  1434. std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
  1435. RUN_TEST(testChildStreamIsDone);
  1436. RUN_TEST(testChildStreamIsRunning);
  1437. RUN_TEST(testChildStreamResult);
  1438. RUN_TEST(testChildStreamClose);
  1439. RUN_TEST(testChildStreamReader);
  1440. RUN_TEST(testChildStreamReaderWriter);
  1441. RUN_TEST(testChildStreamBinaryRead);
  1442. RUN_TEST(testChildStreamNonBlockingRead);
  1443. std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
  1444. RUN_TEST(testChildIsDone);
  1445. RUN_TEST(testChildIsRunning);
  1446. RUN_TEST(testChildResult);
  1447. RUN_TEST(testChildClose);
  1448. RUN_TEST(testChildOpen);
  1449. RUN_TEST(testChildIsFinishedWriting);
  1450. RUN_TEST(testChildRead);
  1451. RUN_TEST(testChildReadWrite);
  1452. RUN_TEST(testChildNonBlockingReadWrite1);
  1453. RUN_TEST(testChildNonBlockingReadWrite2);
  1454. RUN_TEST(testChildVectorReadWrite);
  1455. RUN_TEST(testChildReadDelimited);
  1456. SUMMARY;
  1457. return 0;
  1458. }