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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <random>
  7. #include "CodeDweller/child.hpp"
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Configuration ///////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /// Child program name.
  12. const std::string childName("./childProgram");
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // End of configuration ////////////////////////////////////////////////////////
  15. ////////////////////////////////////////////////////////////////////////////////
  16. int nTotalTests = 0;
  17. int nPass = 0;
  18. int nFail = 0;
  19. bool result;
  20. #define NO_EXCEPTION_TERM(msg) \
  21. std::cout \
  22. << msg << " failed to throw exception at line " \
  23. << __LINE__ << "." << std::endl
  24. #define EXCEPTION_TERM(msg) \
  25. std::cout \
  26. << msg << " threw unexpected exception: " << e.what() << std::endl
  27. #define RETURN_FALSE(msg) \
  28. std::cout \
  29. << msg << " at line " << __LINE__ << std::endl; \
  30. return false;
  31. #define RUN_TEST(test) \
  32. std::cout << " " #test ": "; \
  33. std::cout.flush(); \
  34. result = test(); \
  35. std::cout << (result ? "ok" : "fail") << std::endl; \
  36. nTotalTests++; \
  37. if (result) nPass++; else nFail++;
  38. #define SUMMARY \
  39. std::cout \
  40. << "\nPass: " << nPass \
  41. << ", Fail: " << nFail \
  42. << ", Total: " << nTotalTests << std::endl
  43. bool doReadWrite(size_t bufSize,
  44. size_t nominalAboveMin_ms,
  45. size_t delta_ms,
  46. size_t maxChar) {
  47. try {
  48. std::vector<std::string> cmd;
  49. cmd.push_back(childName);
  50. CodeDweller::Child child(cmd,
  51. bufSize,
  52. nominalAboveMin_ms,
  53. delta_ms);
  54. // Characters for testing.
  55. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  56. // Calculate how often to output a "." to indicate progress.
  57. int maxChunks = maxChar / bufSize;
  58. int nChunks = 0;
  59. int nDots = 50;
  60. int outputEveryChunk = maxChunks / nDots / 2;
  61. if (outputEveryChunk == 0) {
  62. outputEveryChunk = 2;
  63. }
  64. // Send and receive random-sized chunks of data.
  65. size_t nChar = 0;
  66. while (nChar < maxChar) {
  67. std::uniform_int_distribution<int> randomChunk(1, bufSize * 4);
  68. std::default_random_engine generator(4828943);
  69. nChunks++;
  70. if (nChunks % outputEveryChunk == 0) {
  71. std::cout << ".";
  72. std::cout.flush();
  73. }
  74. int chunkSize;
  75. std::string sentChunk, expectedChunk;
  76. // Get a random chunk size for sending.
  77. chunkSize = randomChunk(generator);
  78. if (chunkSize + nChar > maxChar) {
  79. chunkSize = maxChar - nChar;
  80. }
  81. // Generate output to child.
  82. sentChunk.clear();
  83. for (size_t i = 0; i < chunkSize; i++) {
  84. sentChunk.push_back(randomLetter[std::rand() % 26]);
  85. }
  86. // Generated expected input from child.
  87. expectedChunk = sentChunk;
  88. for (int i = 0; i < expectedChunk.size(); i++) {
  89. expectedChunk[i] = std::toupper(expectedChunk[i]);
  90. }
  91. // Send the data.
  92. while (!sentChunk.empty()) {
  93. if (child.writeAndShrink(sentChunk) == 0) {
  94. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  95. }
  96. // Read and compare while sending data.
  97. std::string chunkFromChild;
  98. size_t chunkFromChildSize;
  99. (void) child.read(chunkFromChild);
  100. chunkFromChildSize = chunkFromChild.size();
  101. if (!chunkFromChild.empty()) {
  102. if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
  103. expectedChunk.erase(0, chunkFromChildSize);
  104. } else {
  105. std::cout << "Expected: \""
  106. << expectedChunk.substr(0, chunkFromChildSize)
  107. << "\", received \"" << chunkFromChild << "\"."
  108. << std::endl;
  109. std::cout << "Remaining chunk: \"" << expectedChunk << "\""
  110. << std::endl;
  111. child.write("q");
  112. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  113. RETURN_FALSE(" comparison failure");
  114. }
  115. }
  116. }
  117. // Read and compare after all data was sent.
  118. while (!expectedChunk.empty()) {
  119. std::string chunkFromChild;
  120. size_t chunkFromChildSize;
  121. (void) child.read(chunkFromChild);
  122. chunkFromChildSize = chunkFromChild.size();
  123. if (0 == chunkFromChildSize) {
  124. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  125. } else {
  126. if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
  127. expectedChunk.erase(0, chunkFromChildSize);
  128. } else {
  129. std::cout << "Expected: \""
  130. << expectedChunk.substr(0, chunkFromChildSize)
  131. << "\", received \"" << chunkFromChild << "\"."
  132. << std::endl;
  133. std::cout << "Remaining chunk: \"" << expectedChunk << "\""
  134. << std::endl;
  135. child.write("q");
  136. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  137. RETURN_FALSE(" comparison failure");
  138. }
  139. }
  140. }
  141. nChar += chunkSize;
  142. }
  143. // Send exit message.
  144. if (!child.write("q")) {
  145. RETURN_FALSE(" write() failure");
  146. }
  147. // Verify exit.
  148. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  149. if (!child.isDone()) {
  150. std::cout << " Failure in testChildNonblockingReadWrite2: "
  151. << "Child program did not exit." << std::endl;
  152. return false;
  153. }
  154. std::string errorDescription;
  155. if (child.errorOccurred(errorDescription)) {
  156. std::ostringstream temp;
  157. temp << " Failure in: testChildNonBlockingReadWrite2: "
  158. << errorDescription;
  159. RETURN_FALSE(temp.str());
  160. }
  161. child.close();
  162. } catch (std::exception &e) {
  163. EXCEPTION_TERM("Non-blocking reader test");
  164. return false;
  165. }
  166. return true;
  167. }
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // ChildStream Tests ///////////////////////////////////////////////////////////
  170. ////////////////////////////////////////////////////////////////////////////////
  171. bool testChildStreamIsDone() {
  172. try {
  173. CodeDweller::ChildStream child;
  174. // Test exception if called out-of-order.
  175. try {
  176. child.isDone();
  177. NO_EXCEPTION_TERM("isDone() called without run()");
  178. return false;
  179. } catch (std::exception &e) {
  180. }
  181. // Start the child.
  182. child.open(childName);
  183. if (child.isDone()) {
  184. std::cout << "isDone() failure; returned true." << std::endl;
  185. return false;
  186. }
  187. // Command the child to exit.
  188. child << 'q';
  189. child.flush();
  190. // Sleep to let the child exit.
  191. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  192. if (!child.isDone()) {
  193. std::cout << "isDone() failure; returned false." << std::endl;
  194. return false;
  195. }
  196. } catch (std::exception &e) {
  197. EXCEPTION_TERM("isDone()");
  198. return false;
  199. }
  200. return true;
  201. }
  202. bool testChildStreamIsRunning() {
  203. try {
  204. CodeDweller::ChildStream child;
  205. if (child.isRunning()) {
  206. std::cout << "isRunning() failure; returned true before starting."
  207. << std::endl;
  208. return false;
  209. }
  210. // Start the child.
  211. child.open(childName);
  212. if (!child.isRunning()) {
  213. std::cout << "isRunning() failure; returned false." << std::endl;
  214. return false;
  215. }
  216. // Command the child to exit.
  217. child << 'q';
  218. child.flush();
  219. // Sleep to let the child exit.
  220. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  221. child.close();
  222. if (child.isRunning()) {
  223. std::cout << "isRunning() failure; returned true after stopping."
  224. << std::endl;
  225. return false;
  226. }
  227. } catch (std::exception &e) {
  228. EXCEPTION_TERM("testChildStreamIsRunning");
  229. return false;
  230. }
  231. return true;
  232. }
  233. bool testChildStreamResult() {
  234. try {
  235. std::vector<std::string> cmd;
  236. cmd.push_back(childName);
  237. cmd.push_back("quit"); // Child sleeps for 50 ms and then exits.
  238. CodeDweller::ChildStream child(cmd);
  239. // Test exception if called out-of-order.
  240. try {
  241. (void) child.result();
  242. NO_EXCEPTION_TERM(" result() called without run()");
  243. return false;
  244. } catch (std::exception &e) {
  245. }
  246. // Test exception if called while child is running.
  247. try {
  248. (void) child.result();
  249. NO_EXCEPTION_TERM(" result() called before child exited");
  250. return false;
  251. } catch (std::exception &e) {
  252. }
  253. // Wait for the child to exit.
  254. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  255. int32_t result = child.result();
  256. if (25 != result) {
  257. std::cout << "result() failure; returned " << result
  258. << " instead of 25." << std::endl;
  259. return false;
  260. }
  261. } catch (std::exception &e) {
  262. EXCEPTION_TERM("result()");
  263. return false;
  264. }
  265. return true;
  266. }
  267. bool testChildStreamClose() {
  268. // Test with no waiting.
  269. try {
  270. CodeDweller::ChildStream child(childName);
  271. child.close();
  272. } catch (std::exception &e) {
  273. EXCEPTION_TERM("close() with no waiting");
  274. return false;
  275. }
  276. // Test with waiting.
  277. try {
  278. CodeDweller::ChildStream child(childName);
  279. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  280. child.close();
  281. } catch (std::exception &e) {
  282. EXCEPTION_TERM("close() with 100 ms waiting");
  283. return false;
  284. }
  285. // Test after the child exits.
  286. std::vector<std::string> cmd;
  287. cmd.push_back(childName);
  288. cmd.push_back("quit");
  289. try {
  290. CodeDweller::ChildStream child(cmd);
  291. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  292. child.close();
  293. } catch (std::exception &e) {
  294. EXCEPTION_TERM("close() after child exits");
  295. return false;
  296. }
  297. // Test exception thrown for out-of-order calling.
  298. try {
  299. CodeDweller::ChildStream child;
  300. child.close();
  301. NO_EXCEPTION_TERM("close() called without run()");
  302. return false;
  303. } catch (std::exception &e) {
  304. }
  305. return true;
  306. }
  307. bool testChildStreamReaderWriter() {
  308. try {
  309. size_t bufSize = 15;
  310. CodeDweller::ChildStream child(bufSize);
  311. std::ostringstream childOutput;
  312. std::vector<std::string> expectedChildOutput;
  313. char readChar;
  314. // Generate input.
  315. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  316. int nChar = bufSize - 1;
  317. int nLines = 2;
  318. for (int iLine = 0; iLine < nLines; iLine++) {
  319. std::string line;
  320. line.erase();
  321. for (int iChar = 0; iChar < nChar; iChar++) {
  322. line.push_back(randomLetter[std::rand() % 26]);
  323. }
  324. expectedChildOutput.push_back(line);
  325. }
  326. // Test exception.
  327. try {
  328. child.exceptions(std::ostream::failbit | std::ostream::badbit);
  329. child << bufSize;
  330. child.flush();
  331. NO_EXCEPTION_TERM(" writer called without run()");
  332. return false;
  333. } catch (std::exception &e) {
  334. }
  335. // Clear the writer stream.
  336. try {
  337. child.clear();
  338. } catch (std::exception &e) {
  339. }
  340. child.open(childName);
  341. // Write, read, put back, and reread each character.
  342. for (std::string line : expectedChildOutput) {
  343. // Write one line.
  344. const char *ptr;
  345. ptr = line.data();
  346. for (std::string::size_type i = 0; i < line.length(); i++) {
  347. child << ptr[i];
  348. if (!child) {
  349. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  350. }
  351. }
  352. child.flush();
  353. if (!child) {
  354. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  355. }
  356. // Read one line.
  357. std::string readLine;
  358. readLine.erase();
  359. for (std::string::size_type i = 0; i < line.length(); i++) {
  360. child >> readChar;
  361. if (!child) {
  362. RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
  363. }
  364. readLine.push_back(readChar);
  365. }
  366. // Convert to upper case.
  367. std::string expectedLine;
  368. expectedLine = line;
  369. for (auto &c : expectedLine) {
  370. c = toupper(c);
  371. }
  372. // Compare.
  373. if (expectedLine != readLine) {
  374. std::cout << " Failure in testReaderWriter." << std::endl;
  375. std::cout << " Expected: '" << expectedLine
  376. << "'\n Received: '" << readLine << "'" << std::endl;
  377. return false;
  378. }
  379. }
  380. // Send exit message.
  381. child << 'q';
  382. child.flush();
  383. if (!child) {
  384. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  385. }
  386. // Verify exit.
  387. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  388. if (!child.isDone()) {
  389. std::cout << " Failure in testReaderWriter: "
  390. << "Child program did not exit." << std::endl;
  391. return false;
  392. }
  393. } catch (std::exception &e) {
  394. EXCEPTION_TERM("reader()/writer()");
  395. return false;
  396. }
  397. return true;
  398. }
  399. bool testChildStreamReader() {
  400. try {
  401. size_t bufSize = 32;
  402. CodeDweller::ChildStream child(bufSize);
  403. // Test exception.
  404. try {
  405. int temp;
  406. child.exceptions(std::istream::failbit | std::istream::badbit);
  407. child >> temp;
  408. NO_EXCEPTION_TERM(" reader called without run()");
  409. return false;
  410. } catch (std::exception &e) {
  411. }
  412. child.clear();
  413. child.clear();
  414. std::ostringstream childOutput;
  415. std::string expectedChildOutput("This is a test");
  416. char readChar;
  417. std::vector<std::string> cmd;
  418. cmd.push_back(childName);
  419. cmd.push_back("write");
  420. child.open(cmd);
  421. child.exceptions(std::istream::badbit);
  422. child >> std::noskipws;
  423. if (!child) {
  424. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  425. }
  426. while (child >> readChar) {
  427. if (!child) {
  428. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  429. }
  430. child.putback(readChar);
  431. if (!child) {
  432. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  433. }
  434. child >> readChar;
  435. if (!child) {
  436. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  437. }
  438. childOutput << readChar;
  439. }
  440. if (!child.eof()) {
  441. RETURN_FALSE(" Failure in testReader: Error occured before "
  442. "EOF was reached while reading");
  443. }
  444. // Check.
  445. if (childOutput.str() != expectedChildOutput) {
  446. std::cout << " reader() failure in testReader." << std::endl;
  447. std::cout << " Expected: '" << expectedChildOutput
  448. << "'\n Received: '" << childOutput.str() << "'"
  449. << std::endl;
  450. return false;
  451. }
  452. // Sleep to let the child exit.
  453. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  454. if (!child.isDone()) {
  455. std::cout << "first isDone() failure in testReader." << std::endl;
  456. return false;
  457. }
  458. if (!child.isDone()) {
  459. std::cout << "second isDone() failure in testReader." << std::endl;
  460. return false;
  461. }
  462. } catch (std::exception &e) {
  463. EXCEPTION_TERM("reader()");
  464. return false;
  465. }
  466. return true;
  467. }
  468. bool testChildStreamBinaryRead() {
  469. try {
  470. std::vector<std::string> cmd;
  471. cmd.push_back(childName);
  472. size_t bufSize = 164;
  473. CodeDweller::ChildStream child(bufSize);
  474. child.open(cmd);
  475. // Write.
  476. std::string childInput("abc");
  477. child << childInput;
  478. child.flush();
  479. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  480. // Read one character.
  481. char ch;
  482. child.read(&ch, 1);
  483. if (ch != 'A') {
  484. RETURN_FALSE(" reader.read() returned incorrect value");
  485. }
  486. // Read.
  487. char buf[bufSize * 2];
  488. child.read(buf, 2);
  489. buf[2] = '\0';
  490. std::string input(buf);
  491. if (input != "BC") {
  492. RETURN_FALSE(" reader.read() returned incorrect value");
  493. }
  494. // Fill input buffer.
  495. std::string output("abcdefghijklmnopprstuvwxyz");
  496. std::string expectedInput(output);
  497. for (int i = 0; i < output.size(); i++) {
  498. expectedInput[i] = std::toupper(output[i]);
  499. }
  500. child << output;
  501. child.flush();
  502. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  503. child.read(&ch, 1);
  504. int index = 0;
  505. if (ch != expectedInput[index++]) {
  506. RETURN_FALSE(" reader.read() returned incorrect value");
  507. }
  508. size_t nBytesRead = expectedInput.size() - 1;
  509. child.read(buf, nBytesRead);
  510. buf[nBytesRead] = '\0';
  511. if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
  512. RETURN_FALSE(" reader.read() failure");
  513. }
  514. // Send exit message.
  515. child << 'q';
  516. child.flush();
  517. if (!child) {
  518. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  519. }
  520. // Verify exit.
  521. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  522. if (!child.isDone()) {
  523. std::cout << " Failure in testNonblockingReader: "
  524. << "Child program did not exit." << std::endl;
  525. return false;
  526. }
  527. } catch (std::exception &e) {
  528. EXCEPTION_TERM("Binary read test");
  529. return false;
  530. }
  531. return true;
  532. }
  533. bool testChildStreamNonBlockingRead() {
  534. try {
  535. std::vector<std::string> cmd;
  536. cmd.push_back(childName);
  537. size_t bufSize = 16;
  538. CodeDweller::ChildStream child(cmd, bufSize);
  539. // Check for available input with no input.
  540. if (child.numBytesAvailable() != 0) {
  541. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  542. }
  543. // Check for available input with input.
  544. std::string childInput("abc");
  545. child << childInput;
  546. child.flush();
  547. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  548. // Check that input is available.
  549. if (child.numBytesAvailable() != 1) {
  550. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  551. }
  552. // Read one character.
  553. char ch;
  554. child.read(&ch, 1);
  555. if (ch != 'A') {
  556. RETURN_FALSE(" reader.read() returned incorrect value");
  557. }
  558. // Check that input is available.
  559. if (child.numBytesAvailable() != 2) {
  560. RETURN_FALSE(" numBytesAvailable() did not return expected 2");
  561. }
  562. // Read.
  563. char buf[bufSize * 2];
  564. child.read(buf, 2);
  565. buf[2] = '\0';
  566. std::string input(buf);
  567. if (input != "BC") {
  568. RETURN_FALSE(" reader.read() returned incorrect value");
  569. }
  570. // Check that no input is available.
  571. if (child.numBytesAvailable() != 0) {
  572. RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
  573. "after reading");
  574. }
  575. // Fill input buffer.
  576. std::string output("abcdefghijklmnopprstuvwxyz");
  577. std::string expectedInput(output);
  578. for (int i = 0; i < output.size(); i++) {
  579. expectedInput[i] = std::toupper(output[i]);
  580. }
  581. child << output;
  582. child.flush();
  583. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  584. if (child.numBytesAvailable() != 1) {
  585. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  586. }
  587. child.read(&ch, 1);
  588. int index = 0;
  589. if (ch != expectedInput[index++]) {
  590. RETURN_FALSE(" reader.read() returned incorrect value");
  591. }
  592. size_t nBytesAvailable;
  593. nBytesAvailable = child.numBytesAvailable();
  594. if (nBytesAvailable != bufSize) {
  595. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  596. }
  597. std::fill_n(buf, sizeof(buf), 0);
  598. child.read(buf, nBytesAvailable);
  599. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  600. RETURN_FALSE(" reader.read() failure");
  601. }
  602. index += nBytesAvailable;
  603. nBytesAvailable = child.numBytesAvailable();
  604. if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
  605. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  606. }
  607. std::fill_n(buf, sizeof(buf), 0);
  608. child.read(buf, nBytesAvailable);
  609. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  610. RETURN_FALSE(" reader.read() failure");
  611. }
  612. index += nBytesAvailable;
  613. if (expectedInput.size() != index) {
  614. RETURN_FALSE(" not all data was read by reader.read()");
  615. }
  616. if (child.numBytesAvailable() != 0) {
  617. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  618. }
  619. // Send exit message.
  620. child << 'q';
  621. child.flush();
  622. if (!child) {
  623. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  624. }
  625. // Verify exit.
  626. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  627. if (!child.isDone()) {
  628. std::cout << " Failure in testNonblockingReader: "
  629. << "Child program did not exit." << std::endl;
  630. return false;
  631. }
  632. } catch (std::exception &e) {
  633. EXCEPTION_TERM("Non-blocking reader test");
  634. return false;
  635. }
  636. return true;
  637. }
  638. ////////////////////////////////////////////////////////////////////////////////
  639. // Child Tests ///////////////////////////////////////////////////////////
  640. ////////////////////////////////////////////////////////////////////////////////
  641. bool testChildIsDone() {
  642. try {
  643. CodeDweller::Child child;
  644. // Test exception if called out-of-order.
  645. try {
  646. child.isDone();
  647. NO_EXCEPTION_TERM("isDone() called without run()");
  648. return false;
  649. } catch (std::exception &e) {
  650. }
  651. // Start the child.
  652. child.open(childName);
  653. if (child.isDone()) {
  654. std::cout << "isDone() failure; returned true." << std::endl;
  655. return false;
  656. }
  657. // Command the child to exit.
  658. if (!child.write("q")) {
  659. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  660. }
  661. // Sleep to let the child exit.
  662. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  663. if (!child.isDone()) {
  664. std::cout << "isDone() failure; returned false." << std::endl;
  665. return false;
  666. }
  667. child.close();
  668. } catch (std::exception &e) {
  669. EXCEPTION_TERM("isDone()");
  670. return false;
  671. }
  672. return true;
  673. }
  674. bool testChildIsRunning() {
  675. try {
  676. CodeDweller::Child child;
  677. if (child.isRunning()) {
  678. std::cout << "isRunning() failure; returned true before starting."
  679. << std::endl;
  680. return false;
  681. }
  682. // Start the child.
  683. child.open(childName);
  684. if (!child.isRunning()) {
  685. std::cout << "isRunning() failure; returned false." << std::endl;
  686. return false;
  687. }
  688. // Command the child to exit.
  689. if (!child.write("q")) {
  690. RETURN_FALSE(" write() failure");
  691. }
  692. // Sleep to let the child exit.
  693. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  694. child.close();
  695. if (child.isRunning()) {
  696. std::cout << "isRunning() failure; returned true after stopping."
  697. << std::endl;
  698. return false;
  699. }
  700. } catch (std::exception &e) {
  701. EXCEPTION_TERM("isRunning()");
  702. return false;
  703. }
  704. return true;
  705. }
  706. bool testChildResult() {
  707. try {
  708. std::vector<std::string> cmd;
  709. cmd.push_back(childName);
  710. cmd.push_back("quit");
  711. CodeDweller::Child child(cmd);
  712. // Test exception if called out-of-order.
  713. try {
  714. (void) child.result();
  715. NO_EXCEPTION_TERM(" result() called without run()");
  716. return false;
  717. } catch (std::exception &e) {
  718. }
  719. // Test exception if called while child is running.
  720. try {
  721. (void) child.result();
  722. NO_EXCEPTION_TERM(" result() called before child exited");
  723. return false;
  724. } catch (std::exception &e) {
  725. }
  726. // Wait for the child to exit.
  727. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  728. int32_t result = child.result();
  729. if (25 != result) {
  730. std::cout << "result() failure; returned " << result
  731. << " instead of 25." << std::endl;
  732. return false;
  733. }
  734. } catch (std::exception &e) {
  735. EXCEPTION_TERM("result()");
  736. return false;
  737. }
  738. return true;
  739. }
  740. bool testChildClose() {
  741. // Test with no child process.
  742. try {
  743. CodeDweller::Child child;
  744. child.close();
  745. NO_EXCEPTION_TERM("close() with no child process");
  746. return false;
  747. } catch (std::exception &e) {
  748. }
  749. // Test with no waiting.
  750. try {
  751. CodeDweller::Child child(childName);
  752. child.close();
  753. } catch (std::exception &e) {
  754. EXCEPTION_TERM("close() with no waiting");
  755. return false;
  756. }
  757. // Test with waiting.
  758. try {
  759. CodeDweller::Child child(childName);
  760. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  761. child.close();
  762. } catch (std::exception &e) {
  763. EXCEPTION_TERM("close() with 100 ms waiting");
  764. return false;
  765. }
  766. // Test after the child exits.
  767. std::vector<std::string> cmd;
  768. cmd.push_back(childName);
  769. cmd.push_back("quit");
  770. try {
  771. CodeDweller::Child child(cmd);
  772. if (!child.write("q")) {
  773. RETURN_FALSE(" write() failure");
  774. }
  775. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  776. child.close();
  777. } catch (std::exception &e) {
  778. EXCEPTION_TERM("close() after child exits");
  779. return false;
  780. }
  781. // Test exception thrown for out-of-order calling.
  782. try {
  783. CodeDweller::Child child;
  784. child.close();
  785. NO_EXCEPTION_TERM("close() called without open()");
  786. return false;
  787. } catch (std::exception &e) {
  788. }
  789. return true;
  790. }
  791. bool testChildOpen() {
  792. // Test with no waiting.
  793. try {
  794. CodeDweller::Child child(childName);
  795. child.close();
  796. child.open(childName);
  797. child.close();
  798. } catch (std::exception &e) {
  799. EXCEPTION_TERM("close()/open() with no waiting");
  800. return false;
  801. }
  802. // Test with waiting.
  803. try {
  804. CodeDweller::Child child(childName);
  805. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  806. child.close();
  807. child.open(childName);
  808. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  809. child.close();
  810. } catch (std::exception &e) {
  811. EXCEPTION_TERM("close()/open() with 100 ms waiting");
  812. return false;
  813. }
  814. // Test exception thrown for out-of-order calling.
  815. try {
  816. CodeDweller::Child child;
  817. child.close();
  818. NO_EXCEPTION_TERM("close() called without open()");
  819. return false;
  820. } catch (std::exception &e) {
  821. }
  822. return true;
  823. }
  824. bool testChildReadWrite() {
  825. size_t bufSize = 15;
  826. int nChar = bufSize - 1; // Size of each packet.
  827. try {
  828. CodeDweller::Child child(bufSize);
  829. std::ostringstream childOutput;
  830. std::vector<std::string> expectedChildOutput;
  831. char readChar;
  832. // Generate input.
  833. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  834. int nChar = bufSize - 1;
  835. int nLines = 2;
  836. for (int iLine = 0; iLine < nLines; iLine++) {
  837. std::string line;
  838. line.erase();
  839. for (int iChar = 0; iChar < nChar; iChar++) {
  840. line.push_back(randomLetter[std::rand() % 26]);
  841. }
  842. expectedChildOutput.push_back(line);
  843. }
  844. // Test exception.
  845. try {
  846. child.write("Should Throw");
  847. NO_EXCEPTION_TERM(" writer called without run()");
  848. return false;
  849. } catch (std::exception &e) {
  850. }
  851. child.open(childName);
  852. // Write, read test.
  853. int nLine = 0;
  854. for (std::string &line : expectedChildOutput) {
  855. nLine++;
  856. // Try to queue.
  857. bool done;
  858. int nTries = 100;
  859. for (int i = 0; i < nTries; i++) {
  860. done = child.write(line);
  861. if (done) {
  862. break;
  863. }
  864. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  865. }
  866. if (!done) {
  867. std::ostringstream temp;
  868. temp << " Failure from write(std::string const &) on line " << nLine;
  869. RETURN_FALSE(temp.str());
  870. }
  871. // Wait for transmission.
  872. for (int i = 0; i < nTries; i++) {
  873. done = child.isFinishedWriting();
  874. if (done) {
  875. break;
  876. }
  877. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  878. }
  879. if (!done) {
  880. std::ostringstream temp;
  881. temp << " Failure from isFinishedWriting() on line " << nLine;
  882. RETURN_FALSE(temp.str());
  883. }
  884. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  885. // Read one line.
  886. std::string readLine, temp;
  887. int nCharToRead, nCharRead;
  888. readLine.clear();
  889. nCharToRead = line.size();
  890. for (int i = 0; i < nTries; i++) {
  891. nCharRead = child.read(temp, nCharToRead);
  892. readLine += temp;
  893. nCharToRead -= nCharRead;
  894. if (nCharToRead <= 0) {
  895. break;
  896. }
  897. }
  898. if (0 != nCharToRead) {
  899. std::ostringstream temp;
  900. temp << " Failure from () on line " << nLine
  901. << ": nCharRead: " << nCharRead;
  902. RETURN_FALSE(temp.str());
  903. }
  904. // Convert to upper case.
  905. std::string expectedLine;
  906. expectedLine = line;
  907. for (auto &c : expectedLine) {
  908. c = toupper(c);
  909. }
  910. // Compare.
  911. if (expectedLine != readLine) {
  912. std::cout << " Failure in testReadWrite." << std::endl;
  913. std::cout << " Expected: '" << expectedLine
  914. << "'\n Received: '" << readLine << "'" << std::endl;
  915. return false;
  916. }
  917. }
  918. // Send exit message.
  919. if (!child.write("q")) {
  920. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  921. }
  922. // Verify exit.
  923. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  924. if (!child.isDone()) {
  925. std::cout << " Failure in testReadWrite: Child program did not exit."
  926. << std::endl;
  927. return false;
  928. }
  929. } catch (std::exception &e) {
  930. EXCEPTION_TERM("read()/write()/isFinishedWriting()");
  931. return false;
  932. }
  933. return true;
  934. }
  935. bool testChildIsFinishedWriting() {
  936. std::vector<std::string> cmd;
  937. cmd.push_back(childName);
  938. size_t bufSize = 16;
  939. std::uint16_t
  940. nominalAboveMin_ms = 100,
  941. delta_ms = 0;
  942. try {
  943. CodeDweller::Child child(cmd,
  944. bufSize,
  945. nominalAboveMin_ms,
  946. delta_ms);
  947. if (!child.isFinishedWriting()) {
  948. RETURN_FALSE(" isFinishedWriting() failure");
  949. }
  950. if (!child.write("0123456789")) {
  951. RETURN_FALSE(" write() failure");
  952. }
  953. // Busy wait until the data is written.
  954. while (!child.isFinishedWriting())
  955. ;
  956. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  957. // Writer thread should be pausing now.
  958. if (!child.write("0123456789")) {
  959. RETURN_FALSE(" write() failure");
  960. }
  961. if (child.isFinishedWriting()) {
  962. RETURN_FALSE(" isFinishedWriting() failure");
  963. }
  964. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  965. if (child.isFinishedWriting()) {
  966. RETURN_FALSE(" isFinishedWriting() failure");
  967. }
  968. std::this_thread::sleep_for(std::chrono::milliseconds(80));
  969. if (!child.isFinishedWriting()) {
  970. RETURN_FALSE(" isFinishedWriting() failure");
  971. }
  972. // Send exit message.
  973. if (!child.write("q")) {
  974. RETURN_FALSE(" write() failure");
  975. }
  976. // Verify exit.
  977. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  978. if (!child.isDone()) {
  979. std::cout << " Failure in testIsFinishedWriting: "
  980. << "Child program did not exit."
  981. << std::endl;
  982. return false;
  983. }
  984. } catch (std::exception &e) {
  985. EXCEPTION_TERM("write()/isFinishedWriting()");
  986. return false;
  987. }
  988. return true;
  989. }
  990. bool testChildRead() {
  991. try {
  992. size_t bufSize = 32;
  993. CodeDweller::Child child(bufSize);
  994. // Test exception.
  995. try {
  996. std::string temp;
  997. (void) child.read(temp);
  998. NO_EXCEPTION_TERM(" read() called without open()");
  999. return false;
  1000. } catch (std::exception &e) {
  1001. }
  1002. std::ostringstream childOutput;
  1003. std::string expectedChildOutput("This is a test");
  1004. std::string readBuf;
  1005. std::vector<std::string> cmd;
  1006. cmd.push_back(childName);
  1007. cmd.push_back("write");
  1008. child.open(cmd);
  1009. int nTries = 100;
  1010. size_t nRead = 0;
  1011. for (int i = 0; i < nTries; i++) {
  1012. nRead += child.read(readBuf);
  1013. childOutput << readBuf;
  1014. if (expectedChildOutput.size() == nRead) {
  1015. break;
  1016. }
  1017. if (child.errorOccurred(readBuf)) {
  1018. std::ostringstream temp;
  1019. temp << " Failure in testReader on try " << i << ": " << readBuf;
  1020. RETURN_FALSE(temp.str());
  1021. }
  1022. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1023. }
  1024. // Check.
  1025. if (childOutput.str() != expectedChildOutput) {
  1026. std::cout << " read() failure in testChildRead." << std::endl;
  1027. std::cout << " Expected: '" << expectedChildOutput
  1028. << "'\n Received: '" << childOutput.str() << "'"
  1029. << std::endl;
  1030. return false;
  1031. }
  1032. // Sleep to let the child exit.
  1033. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1034. if (!child.isDone()) {
  1035. std::cout << "first isDone() failure in testReader." << std::endl;
  1036. return false;
  1037. }
  1038. if (!child.isDone()) {
  1039. std::cout << "second isDone() failure in testReader." << std::endl;
  1040. return false;
  1041. }
  1042. child.close();
  1043. } catch (std::exception &e) {
  1044. EXCEPTION_TERM("reader()");
  1045. return false;
  1046. }
  1047. return true;
  1048. }
  1049. bool testChildNonBlockingReadWrite1() {
  1050. try {
  1051. std::vector<std::string> cmd;
  1052. cmd.push_back(childName);
  1053. size_t bufSize = 16;
  1054. std::uint16_t
  1055. nominalAboveMin_ms = 15,
  1056. delta_ms = 0;
  1057. CodeDweller::Child child(cmd,
  1058. bufSize,
  1059. nominalAboveMin_ms,
  1060. delta_ms);
  1061. // Check for available input with input.
  1062. std::string childInput("abc");
  1063. if (!child.write(childInput)) {
  1064. RETURN_FALSE(" first write() failure");
  1065. }
  1066. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1067. // Read one character.
  1068. std::string readBuf;
  1069. if (child.read(readBuf, 1) != 1) {
  1070. RETURN_FALSE(" read() failure");
  1071. }
  1072. if ("A" != readBuf) {
  1073. RETURN_FALSE(" read() failure");
  1074. }
  1075. // Read.
  1076. if (child.read(readBuf, 2) != 2) {
  1077. RETURN_FALSE(" read() failure");
  1078. }
  1079. if ("BC" != readBuf) {
  1080. RETURN_FALSE(" read() failure");
  1081. }
  1082. // Check that no input is available.
  1083. if (child.read(readBuf) != 0) {
  1084. RETURN_FALSE(" read() failure");
  1085. }
  1086. if (!readBuf.empty()) {
  1087. RETURN_FALSE(" read() failure");
  1088. }
  1089. // Fill input buffer.
  1090. std::string output("abcdefghijklmnopprstuvwxyz");
  1091. std::string expectedInput(output);
  1092. std::string expectedOutputAfter1(output.substr(bufSize));
  1093. for (int i = 0; i < output.size(); i++) {
  1094. expectedInput[i] = std::toupper(output[i]);
  1095. }
  1096. if (child.writeAndShrink(output) != bufSize) {
  1097. RETURN_FALSE(" writeAndShrink() failure");
  1098. }
  1099. if (output != expectedOutputAfter1) {
  1100. RETURN_FALSE(" writeAndShrink() failure");
  1101. }
  1102. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1103. int expectedLeftOver = output.size();
  1104. if (child.writeAndShrink(output) != expectedLeftOver) {
  1105. RETURN_FALSE(" writeAndShrink() failure");
  1106. }
  1107. if (!output.empty()) {
  1108. RETURN_FALSE(" writeAndShrink() failure");
  1109. }
  1110. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1111. size_t nRead = child.read(readBuf);
  1112. if (nRead != bufSize) {
  1113. RETURN_FALSE(" read() failure");
  1114. }
  1115. if (readBuf != expectedInput.substr(0, bufSize)) {
  1116. RETURN_FALSE(" read() failure)");
  1117. }
  1118. // Wait for reader thread to fill input buffer.
  1119. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  1120. if (child.read(readBuf) != expectedLeftOver) {
  1121. RETURN_FALSE(" read() failure");
  1122. }
  1123. if (expectedInput.substr(bufSize) != readBuf) {
  1124. RETURN_FALSE(" read() failure");
  1125. }
  1126. // Send exit message.
  1127. if (!child.write("q")) {
  1128. RETURN_FALSE(" write() failure");
  1129. }
  1130. // Verify exit.
  1131. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1132. if (!child.isDone()) {
  1133. std::cout << " Failure in testChildNonblockingReadWrite1: "
  1134. << "Child program did not exit." << std::endl;
  1135. return false;
  1136. }
  1137. std::string errorDescription;
  1138. if (child.errorOccurred(errorDescription)) {
  1139. std::ostringstream temp;
  1140. temp << " Failure in: testChildNonBlockingReadWrite1: "
  1141. << errorDescription;
  1142. RETURN_FALSE(temp.str());
  1143. }
  1144. child.close();
  1145. } catch (std::exception &e) {
  1146. EXCEPTION_TERM("Non-blocking reader test");
  1147. return false;
  1148. }
  1149. return true;
  1150. }
  1151. bool testChildNonBlockingReadWrite2() {
  1152. size_t bufSize, maxChar;
  1153. std::uint16_t
  1154. nominalAboveMin_ms = 0,
  1155. delta_ms = 0;
  1156. bufSize = 16;
  1157. maxChar = 1000 * 100;
  1158. std::cout << "\n many small buffers";
  1159. std::cout.flush();
  1160. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1161. RETURN_FALSE(" read()/write() failure");
  1162. }
  1163. bufSize = 128 * 1024;
  1164. maxChar = 1000 * 10;
  1165. std::cout << "\n one big buffer";
  1166. std::cout.flush();
  1167. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1168. RETURN_FALSE(" read()/write() failure");
  1169. }
  1170. bufSize = 128 * 1024;
  1171. maxChar = 1000 * 1000 * 10;
  1172. std::cout << "\n many big buffers";
  1173. std::cout.flush();
  1174. if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
  1175. RETURN_FALSE(" read()/write() failure");
  1176. }
  1177. return true;
  1178. }
  1179. ////////////////////////////////////////////////////////////////////////////////
  1180. // End of tests ////////////////////////////////////////////////////////////////
  1181. ////////////////////////////////////////////////////////////////////////////////
  1182. int main()
  1183. {
  1184. std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
  1185. RUN_TEST(testChildStreamIsDone);
  1186. RUN_TEST(testChildStreamIsRunning);
  1187. RUN_TEST(testChildStreamResult);
  1188. RUN_TEST(testChildStreamClose);
  1189. RUN_TEST(testChildStreamReader);
  1190. RUN_TEST(testChildStreamReaderWriter);
  1191. RUN_TEST(testChildStreamBinaryRead);
  1192. RUN_TEST(testChildStreamNonBlockingRead);
  1193. std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
  1194. RUN_TEST(testChildIsDone);
  1195. RUN_TEST(testChildIsRunning);
  1196. RUN_TEST(testChildResult);
  1197. RUN_TEST(testChildClose);
  1198. RUN_TEST(testChildOpen);
  1199. RUN_TEST(testChildIsFinishedWriting);
  1200. RUN_TEST(testChildRead);
  1201. RUN_TEST(testChildReadWrite);
  1202. RUN_TEST(testChildNonBlockingReadWrite1);
  1203. RUN_TEST(testChildNonBlockingReadWrite2);
  1204. SUMMARY;
  1205. return 0;
  1206. }