123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626 |
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <sstream>
- #include <algorithm>
- #include <random>
-
- #include "CodeDweller/child.hpp"
-
- ////////////////////////////////////////////////////////////////////////////////
- // Configuration ///////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- /// Child program name.
- const std::string childName("./childProgram");
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of configuration ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int nTotalTests = 0;
- int nPass = 0;
- int nFail = 0;
-
- bool result;
-
- #define NO_EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " failed to throw exception at line " \
- << __LINE__ << "." << std::endl
-
- #define EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " threw unexpected exception: " << e.what() << std::endl
-
- #define RETURN_FALSE(msg) \
- std::cout \
- << msg << " at line " << __LINE__ << std::endl; \
- return false;
-
- #define RUN_TEST(test) \
- std::cout << " " #test ": "; \
- std::cout.flush(); \
- result = test(); \
- std::cout << (result ? "ok" : "fail") << std::endl; \
- nTotalTests++; \
- if (result) nPass++; else nFail++;
-
- #define SUMMARY \
- std::cout \
- << "\nPass: " << nPass \
- << ", Fail: " << nFail \
- << ", Total: " << nTotalTests << std::endl
-
-
- bool doReadWrite(size_t bufSize,
- size_t nominalAboveMin_ms,
- size_t delta_ms,
- size_t maxChar) {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- // Characters for testing.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
-
- // Calculate how often to output a "." to indicate progress.
- int maxChunks = maxChar / bufSize;
- int nChunks = 0;
- int nDots = 50;
- int outputEveryChunk = maxChunks / nDots / 2;
-
- if (outputEveryChunk == 0) {
- outputEveryChunk = 2;
- }
-
- // Send and receive random-sized chunks of data.
- size_t nChar = 0;
-
- while (nChar < maxChar) {
-
- std::uniform_int_distribution<int> randomChunk(1, bufSize * 4);
- std::default_random_engine generator(4828943);
-
- nChunks++;
-
- if (nChunks % outputEveryChunk == 0) {
- std::cout << ".";
- std::cout.flush();
- }
-
- int chunkSize;
- std::string sentChunk, expectedChunk;
-
- // Get a random chunk size for sending.
- chunkSize = randomChunk(generator);
-
- if (chunkSize + nChar > maxChar) {
- chunkSize = maxChar - nChar;
- }
-
- // Generate output to child.
- sentChunk.clear();
-
- for (size_t i = 0; i < chunkSize; i++) {
- sentChunk.push_back(randomLetter[std::rand() % 26]);
- }
-
- // Generated expected input from child.
- expectedChunk = sentChunk;
-
- for (int i = 0; i < expectedChunk.size(); i++) {
- expectedChunk[i] = std::toupper(expectedChunk[i]);
- }
-
- // Send the data.
- while (!sentChunk.empty()) {
-
- if (child.writeAndShrink(sentChunk) == 0) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
-
- // Read and compare while sending data.
- std::string chunkFromChild;
- size_t chunkFromChildSize;
-
- (void) child.read(chunkFromChild);
-
- chunkFromChildSize = chunkFromChild.size();
-
- if (!chunkFromChild.empty()) {
-
- if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
- expectedChunk.erase(0, chunkFromChildSize);
- } else {
- std::cout << "Expected: \""
- << expectedChunk.substr(0, chunkFromChildSize)
- << "\", received \"" << chunkFromChild << "\"."
- << std::endl;
- std::cout << "Remaining chunk: \"" << expectedChunk << "\""
- << std::endl;
- child.write("q");
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- RETURN_FALSE(" comparison failure");
- }
-
- }
-
- }
-
- // Read and compare after all data was sent.
- while (!expectedChunk.empty()) {
-
- std::string chunkFromChild;
- size_t chunkFromChildSize;
-
- (void) child.read(chunkFromChild);
-
- chunkFromChildSize = chunkFromChild.size();
-
- if (0 == chunkFromChildSize) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- } else {
-
- if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
- expectedChunk.erase(0, chunkFromChildSize);
- } else {
- std::cout << "Expected: \""
- << expectedChunk.substr(0, chunkFromChildSize)
- << "\", received \"" << chunkFromChild << "\"."
- << std::endl;
- std::cout << "Remaining chunk: \"" << expectedChunk << "\""
- << std::endl;
- child.write("q");
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- RETURN_FALSE(" comparison failure");
- }
-
- }
-
- }
-
- nChar += chunkSize;
-
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testChildNonblockingReadWrite2: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildNonBlockingReadWrite2: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // ChildStream Tests ///////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testChildStreamIsDone() {
-
- try {
-
- CodeDweller::ChildStream child;
-
- // Test exception if called out-of-order.
- try {
- child.isDone();
- NO_EXCEPTION_TERM("isDone() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Start the child.
- child.open(childName);
-
- if (child.isDone()) {
- std::cout << "isDone() failure; returned true." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- child << 'q';
- child.flush();
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "isDone() failure; returned false." << std::endl;
- return false;
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("isDone()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamIsRunning() {
-
- try {
-
- CodeDweller::ChildStream child;
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true before starting."
- << std::endl;
- return false;
- }
-
- // Start the child.
- child.open(childName);
-
- if (!child.isRunning()) {
- std::cout << "isRunning() failure; returned false." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- child << 'q';
- child.flush();
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.close();
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true after stopping."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("testChildStreamIsRunning");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamResult() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit"); // Child sleeps for 50 ms and then exits.
-
- CodeDweller::ChildStream child(cmd);
-
- // Test exception if called out-of-order.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test exception if called while child is running.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called before child exited");
- return false;
- } catch (std::exception &e) {
- }
-
- // Wait for the child to exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- int32_t result = child.result();
- if (25 != result) {
- std::cout << "result() failure; returned " << result
- << " instead of 25." << std::endl;
- return false;
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("result()");
- return false;
- }
- return true;
- }
-
- bool testChildStreamClose() {
-
- // Test with no waiting.
- try {
- CodeDweller::ChildStream child(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::ChildStream child(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with 100 ms waiting");
- return false;
- }
-
- // Test after the child exits.
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- try {
- CodeDweller::ChildStream child(cmd);
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() after child exits");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
-
- CodeDweller::ChildStream child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildStreamReaderWriter() {
-
- try {
- size_t bufSize = 15;
- CodeDweller::ChildStream child(bufSize);
- std::ostringstream childOutput;
- std::vector<std::string> expectedChildOutput;
- char readChar;
-
- // Generate input.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
- int nChar = bufSize - 1;
- int nLines = 2;
- for (int iLine = 0; iLine < nLines; iLine++) {
- std::string line;
- line.erase();
- for (int iChar = 0; iChar < nChar; iChar++) {
- line.push_back(randomLetter[std::rand() % 26]);
- }
- expectedChildOutput.push_back(line);
- }
-
- // Test exception.
- try {
- child.exceptions(std::ostream::failbit | std::ostream::badbit);
- child << bufSize;
- child.flush();
- NO_EXCEPTION_TERM(" writer called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Clear the writer stream.
- try {
- child.clear();
- } catch (std::exception &e) {
- }
-
- child.open(childName);
-
- // Write, read, put back, and reread each character.
- for (std::string line : expectedChildOutput) {
-
- // Write one line.
- const char *ptr;
-
- ptr = line.data();
- for (std::string::size_type i = 0; i < line.length(); i++) {
- child << ptr[i];
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
- }
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
-
- // Read one line.
- std::string readLine;
-
- readLine.erase();
- for (std::string::size_type i = 0; i < line.length(); i++) {
- child >> readChar;
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
- }
- readLine.push_back(readChar);
- }
-
- // Convert to upper case.
- std::string expectedLine;
-
- expectedLine = line;
- for (auto &c : expectedLine) {
-
- c = toupper(c);
-
- }
-
- // Compare.
- if (expectedLine != readLine) {
- std::cout << " Failure in testReaderWriter." << std::endl;
- std::cout << " Expected: '" << expectedLine
- << "'\n Received: '" << readLine << "'" << std::endl;
- return false;
-
- }
-
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testReaderWriter: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()/writer()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamReader() {
-
- try {
-
- size_t bufSize = 32;
- CodeDweller::ChildStream child(bufSize);
-
- // Test exception.
- try {
- int temp;
- child.exceptions(std::istream::failbit | std::istream::badbit);
- child >> temp;
- NO_EXCEPTION_TERM(" reader called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- child.clear();
- child.clear();
- std::ostringstream childOutput;
- std::string expectedChildOutput("This is a test");
- char readChar;
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("write");
-
- child.open(cmd);
- child.exceptions(std::istream::badbit);
- child >> std::noskipws;
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
-
- while (child >> readChar) {
-
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- child.putback(readChar);
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- child >> readChar;
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- childOutput << readChar;
-
- }
-
- if (!child.eof()) {
- RETURN_FALSE(" Failure in testReader: Error occured before "
- "EOF was reached while reading");
- }
-
- // Check.
- if (childOutput.str() != expectedChildOutput) {
- std::cout << " reader() failure in testReader." << std::endl;
- std::cout << " Expected: '" << expectedChildOutput
- << "'\n Received: '" << childOutput.str() << "'"
- << std::endl;
- return false;
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "first isDone() failure in testReader." << std::endl;
- return false;
- }
-
- if (!child.isDone()) {
- std::cout << "second isDone() failure in testReader." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamBinaryRead() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 164;
- CodeDweller::ChildStream child(bufSize);
-
- child.open(cmd);
-
- // Write.
- std::string childInput("abc");
-
- child << childInput;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Read one character.
- char ch;
-
- child.read(&ch, 1);
-
- if (ch != 'A') {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Read.
- char buf[bufSize * 2];
-
- child.read(buf, 2);
- buf[2] = '\0';
-
- std::string input(buf);
-
- if (input != "BC") {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- child << output;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.read(&ch, 1);
-
- int index = 0;
-
- if (ch != expectedInput[index++]) {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- size_t nBytesRead = expectedInput.size() - 1;
-
- child.read(buf, nBytesRead);
- buf[nBytesRead] = '\0';
-
- if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testNonblockingReader: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Binary read test");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamNonBlockingRead() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- CodeDweller::ChildStream child(cmd, bufSize);
-
- // Check for available input with no input.
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0");
- }
-
- // Check for available input with input.
- std::string childInput("abc");
-
- child << childInput;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Check that input is available.
- if (child.numBytesAvailable() != 1) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 1");
- }
-
- // Read one character.
- char ch;
-
- child.read(&ch, 1);
-
- if (ch != 'A') {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Check that input is available.
- if (child.numBytesAvailable() != 2) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 2");
- }
-
- // Read.
- char buf[bufSize * 2];
-
- child.read(buf, 2);
- buf[2] = '\0';
-
- std::string input(buf);
-
- if (input != "BC") {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Check that no input is available.
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
- "after reading");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- child << output;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (child.numBytesAvailable() != 1) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 1");
- }
-
- child.read(&ch, 1);
-
- int index = 0;
-
- if (ch != expectedInput[index++]) {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- size_t nBytesAvailable;
-
- nBytesAvailable = child.numBytesAvailable();
- if (nBytesAvailable != bufSize) {
- RETURN_FALSE(" numBytesAvailable() did not return expected value");
- }
- std::fill_n(buf, sizeof(buf), 0);
-
- child.read(buf, nBytesAvailable);
-
- if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
- index += nBytesAvailable;
-
- nBytesAvailable = child.numBytesAvailable();
- if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
- RETURN_FALSE(" numBytesAvailable() did not return expected value");
- }
- std::fill_n(buf, sizeof(buf), 0);
-
- child.read(buf, nBytesAvailable);
-
- if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
- index += nBytesAvailable;
-
- if (expectedInput.size() != index) {
- RETURN_FALSE(" not all data was read by reader.read()");
- }
-
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0");
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testNonblockingReader: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // Child Tests ///////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testChildIsDone() {
-
- try {
-
- CodeDweller::Child child;
-
- // Test exception if called out-of-order.
- try {
- child.isDone();
- NO_EXCEPTION_TERM("isDone() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Start the child.
- child.open(childName);
-
- if (child.isDone()) {
- std::cout << "isDone() failure; returned true." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- if (!child.write("q")) {
- RETURN_FALSE(" Failure in testChildIsDone: write() failure");
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "isDone() failure; returned false." << std::endl;
- return false;
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("isDone()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildIsRunning() {
-
- try {
-
- CodeDweller::Child child;
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true before starting."
- << std::endl;
- return false;
- }
-
- // Start the child.
- child.open(childName);
-
- if (!child.isRunning()) {
- std::cout << "isRunning() failure; returned false." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.close();
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true after stopping."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("isRunning()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildResult() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- CodeDweller::Child child(cmd);
-
- // Test exception if called out-of-order.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test exception if called while child is running.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called before child exited");
- return false;
- } catch (std::exception &e) {
- }
-
- // Wait for the child to exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- int32_t result = child.result();
-
- if (25 != result) {
- std::cout << "result() failure; returned " << result
- << " instead of 25." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("result()");
- return false;
- }
-
- return true;
- }
-
- bool testChildClose() {
-
- // Test with no child process.
- try {
- CodeDweller::Child child;
- child.close();
- NO_EXCEPTION_TERM("close() with no child process");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test with no waiting.
- try {
- CodeDweller::Child child(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::Child child(childName);
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with 100 ms waiting");
- return false;
- }
-
- // Test after the child exits.
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- try {
- CodeDweller::Child child(cmd);
-
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() after child exits");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
- CodeDweller::Child child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildOpen() {
-
- // Test with no waiting.
- try {
- CodeDweller::Child child(childName);
- child.close();
- child.open(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close()/open() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::Child child(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- child.open(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close()/open() with 100 ms waiting");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
-
- CodeDweller::Child child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildReadWrite() {
-
- size_t bufSize = 15;
- int nChar = bufSize - 1; // Size of each packet.
-
-
- try {
- CodeDweller::Child child(bufSize);
- std::ostringstream childOutput;
- std::vector<std::string> expectedChildOutput;
- char readChar;
-
- // Generate input.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
- int nChar = bufSize - 1;
- int nLines = 2;
- for (int iLine = 0; iLine < nLines; iLine++) {
- std::string line;
- line.erase();
- for (int iChar = 0; iChar < nChar; iChar++) {
- line.push_back(randomLetter[std::rand() % 26]);
- }
- expectedChildOutput.push_back(line);
- }
-
- // Test exception.
- try {
- child.write("Should Throw");
- NO_EXCEPTION_TERM(" writer called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- child.open(childName);
-
- // Write, read test.
- int nLine = 0;
- for (std::string &line : expectedChildOutput) {
-
- nLine++;
-
- // Try to queue.
- bool done;
- int nTries = 100;
-
- for (int i = 0; i < nTries; i++) {
-
- done = child.write(line);
-
- if (done) {
- break;
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- if (!done) {
- std::ostringstream temp;
-
- temp << " Failure from write(std::string const &) on line " << nLine;
- RETURN_FALSE(temp.str());
- }
-
- // Wait for transmission.
- for (int i = 0; i < nTries; i++) {
-
- done = child.isFinishedWriting();
-
- if (done) {
- break;
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- if (!done) {
- std::ostringstream temp;
-
- temp << " Failure from isFinishedWriting() on line " << nLine;
- RETURN_FALSE(temp.str());
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- // Read one line.
- std::string readLine, temp;
- int nCharToRead, nCharRead;
-
- readLine.clear();
- nCharToRead = line.size();
-
- for (int i = 0; i < nTries; i++) {
-
- nCharRead = child.read(temp, nCharToRead);
- readLine += temp;
-
- nCharToRead -= nCharRead;
-
- if (nCharToRead <= 0) {
- break;
- }
-
- }
-
- if (0 != nCharToRead) {
- std::ostringstream temp;
-
- temp << " Failure from () on line " << nLine
- << ": nCharRead: " << nCharRead;
- RETURN_FALSE(temp.str());
- }
-
- // Convert to upper case.
- std::string expectedLine;
-
- expectedLine = line;
- for (auto &c : expectedLine) {
-
- c = toupper(c);
-
- }
-
- // Compare.
- if (expectedLine != readLine) {
- std::cout << " Failure in testReadWrite." << std::endl;
- std::cout << " Expected: '" << expectedLine
- << "'\n Received: '" << readLine << "'" << std::endl;
- return false;
-
- }
-
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" Failure in testChildIsDone: write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testReadWrite: Child program did not exit."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("read()/write()/isFinishedWriting()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildIsFinishedWriting() {
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- std::uint16_t
- nominalAboveMin_ms = 100,
- delta_ms = 0;
-
- try {
-
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- if (!child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- if (!child.write("0123456789")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Busy wait until the data is written.
- while (!child.isFinishedWriting())
- ;
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Writer thread should be pausing now.
- if (!child.write("0123456789")) {
- RETURN_FALSE(" write() failure");
- }
-
- if (child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
-
- if (child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(80));
-
- if (!child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
-
- if (!child.isDone()) {
- std::cout << " Failure in testIsFinishedWriting: "
- << "Child program did not exit."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("write()/isFinishedWriting()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildRead() {
-
- try {
-
- size_t bufSize = 32;
- CodeDweller::Child child(bufSize);
-
- // Test exception.
- try {
- std::string temp;
- (void) child.read(temp);
- NO_EXCEPTION_TERM(" read() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- std::ostringstream childOutput;
- std::string expectedChildOutput("This is a test");
- std::string readBuf;
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("write");
-
- child.open(cmd);
-
- int nTries = 100;
- size_t nRead = 0;
-
- for (int i = 0; i < nTries; i++) {
-
- nRead += child.read(readBuf);
- childOutput << readBuf;
-
- if (expectedChildOutput.size() == nRead) {
- break;
- }
-
- if (child.errorOccurred(readBuf)) {
- std::ostringstream temp;
-
- temp << " Failure in testReader on try " << i << ": " << readBuf;
- RETURN_FALSE(temp.str());
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- // Check.
- if (childOutput.str() != expectedChildOutput) {
- std::cout << " read() failure in testChildRead." << std::endl;
- std::cout << " Expected: '" << expectedChildOutput
- << "'\n Received: '" << childOutput.str() << "'"
- << std::endl;
- return false;
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "first isDone() failure in testReader." << std::endl;
- return false;
- }
-
- if (!child.isDone()) {
- std::cout << "second isDone() failure in testReader." << std::endl;
- return false;
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildNonBlockingReadWrite1() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- std::uint16_t
- nominalAboveMin_ms = 15,
- delta_ms = 0;
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- // Check for available input with input.
- std::string childInput("abc");
-
- if (!child.write(childInput)) {
- RETURN_FALSE(" first write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Read one character.
- std::string readBuf;
-
- if (child.read(readBuf, 1) != 1) {
- RETURN_FALSE(" read() failure");
- }
- if ("A" != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Read.
- if (child.read(readBuf, 2) != 2) {
- RETURN_FALSE(" read() failure");
- }
- if ("BC" != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Check that no input is available.
- if (child.read(readBuf) != 0) {
- RETURN_FALSE(" read() failure");
- }
- if (!readBuf.empty()) {
- RETURN_FALSE(" read() failure");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
- std::string expectedOutputAfter1(output.substr(bufSize));
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- if (child.writeAndShrink(output) != bufSize) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
- if (output != expectedOutputAfter1) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- int expectedLeftOver = output.size();
-
- if (child.writeAndShrink(output) != expectedLeftOver) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
- if (!output.empty()) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- size_t nRead = child.read(readBuf);
-
- if (nRead != bufSize) {
- RETURN_FALSE(" read() failure");
- }
-
- if (readBuf != expectedInput.substr(0, bufSize)) {
- RETURN_FALSE(" read() failure)");
- }
-
- // Wait for reader thread to fill input buffer.
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- if (child.read(readBuf) != expectedLeftOver) {
- RETURN_FALSE(" read() failure");
- }
-
- if (expectedInput.substr(bufSize) != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testChildNonblockingReadWrite1: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildNonBlockingReadWrite1: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildNonBlockingReadWrite2() {
-
- size_t bufSize, maxChar;
- std::uint16_t
- nominalAboveMin_ms = 0,
- delta_ms = 0;
-
- bufSize = 16;
- maxChar = 1000 * 100;
- std::cout << "\n many small buffers";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- bufSize = 128 * 1024;
- maxChar = 1000 * 10;
- std::cout << "\n one big buffer";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- bufSize = 128 * 1024;
- maxChar = 1000 * 1000 * 10;
- std::cout << "\n many big buffers";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
-
- std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
-
- RUN_TEST(testChildStreamIsDone);
- RUN_TEST(testChildStreamIsRunning);
- RUN_TEST(testChildStreamResult);
- RUN_TEST(testChildStreamClose);
- RUN_TEST(testChildStreamReader);
- RUN_TEST(testChildStreamReaderWriter);
- RUN_TEST(testChildStreamBinaryRead);
- RUN_TEST(testChildStreamNonBlockingRead);
-
- std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
-
- RUN_TEST(testChildIsDone);
- RUN_TEST(testChildIsRunning);
- RUN_TEST(testChildResult);
- RUN_TEST(testChildClose);
- RUN_TEST(testChildOpen);
- RUN_TEST(testChildIsFinishedWriting);
- RUN_TEST(testChildRead);
- RUN_TEST(testChildReadWrite);
- RUN_TEST(testChildNonBlockingReadWrite1);
- RUN_TEST(testChildNonBlockingReadWrite2);
-
- SUMMARY;
-
- return 0;
- }
|