Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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. // ChildStream is not supported.
  1503. #if 0
  1504. std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
  1505. RUN_TEST(testChildStreamIsDone);
  1506. RUN_TEST(testChildStreamIsRunning);
  1507. RUN_TEST(testChildStreamResult);
  1508. RUN_TEST(testChildStreamClose);
  1509. RUN_TEST(testChildStreamReader);
  1510. RUN_TEST(testChildStreamReaderWriter);
  1511. RUN_TEST(testChildStreamBinaryRead);
  1512. RUN_TEST(testChildStreamNonBlockingRead);
  1513. #endif
  1514. std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
  1515. RUN_TEST(testChildIsDone);
  1516. RUN_TEST(testChildIsRunning);
  1517. RUN_TEST(testChildResult);
  1518. RUN_TEST(testChildClose);
  1519. RUN_TEST(testChildStdInClose);
  1520. RUN_TEST(testChildOpen);
  1521. RUN_TEST(testChildIsFinishedWriting);
  1522. RUN_TEST(testChildRead);
  1523. RUN_TEST(testChildReadWrite);
  1524. RUN_TEST(testChildNonBlockingReadWrite1);
  1525. RUN_TEST(testChildNonBlockingReadWrite2);
  1526. RUN_TEST(testChildVectorReadWrite);
  1527. RUN_TEST(testChildReadDelimited);
  1528. SUMMARY;
  1529. return 0;
  1530. }