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 50KB

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