Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

testChild.cpp 39KB

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