X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=tst%2Fintegration%2FTestInstance.cpp;h=fe61fdc12a156dc6da5c8eadb67938d903b3352d;hb=aee07b0f8d8c0d9af66dd7507938d83985d53833;hp=270c09ca88a2e1ab8920dc50661fc763c0bf8883;hpb=3b617c12da216c2d1744c5fbb62a7ef381e1e0b3;p=blank.git diff --git a/tst/integration/TestInstance.cpp b/tst/integration/TestInstance.cpp index 270c09c..fe61fdc 100644 --- a/tst/integration/TestInstance.cpp +++ b/tst/integration/TestInstance.cpp @@ -32,14 +32,18 @@ TestInstance::TestInstance(const Process::Arguments &args, bool cmd) , conn() , out_buf() , err_buf() -, cmd_buf() { +, cmd_buf() +, name("blank" BLANK_SUFFIX) { if (cmd) { // wait for command service startup - // TODO: timeouts for reading from process WaitOutputLine("listening on TCP port 12354"); // connect to command service conn = tcp::Socket("localhost", 12354); } + for (const auto &arg : args) { + name += ' '; + name += arg; + } } TestInstance::~TestInstance() { @@ -54,7 +58,7 @@ void TestInstance::WriteInput(const string &data) { while (i != end) { size_t len = proc.WriteIn(i, end - i); if (len == 0) { - throw runtime_error("failed write to child process' stdin"); + throw runtime_error("failed write to stdin of " + name); } i += len; } @@ -63,9 +67,9 @@ void TestInstance::WriteInput(const string &data) { void TestInstance::ReadOutputLine(string &line) { while (!out_buf.Extract(line)) { // buffer exhausted, fetch more data - int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain()); + int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain(), 5000); if (len == 0) { - throw runtime_error("failed read from child process' stdout"); + throw runtime_error("failed read from stdout of " + name); } out_buf.Update(len); } @@ -73,30 +77,56 @@ void TestInstance::ReadOutputLine(string &line) { void TestInstance::AssertOutputLine(const string &expected) { string line; - ReadOutputLine(line); + if (past_out.empty()) { + ReadOutputLine(line); + } else { + line = past_out.front(); + past_out.pop_front(); + } CPPUNIT_ASSERT_EQUAL_MESSAGE( - "unexpected line in stdout", + "unexpected line in stdout of " + name, expected, line); } void TestInstance::WaitOutputLine(const string &expected) { + for (list::iterator i(past_out.begin()); i != past_out.end(); ++i) { + if (*i == expected) { + past_out.erase(i); + return; + } + } string line; while (true) { ReadOutputLine(line); if (line == expected) { return; + } else { + past_out.push_back(line); } } } void TestInstance::ExhaustOutput(string &output) { - while (!out_buf.Extract(output)) { - // buffer exhausted, fetch more data - int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain()); - if (len == 0) { - return; + output.clear(); + for (const auto &line : past_out) { + output += line; + output += '\n'; + } + past_out.clear(); + string line; + while (true) { + if (out_buf.Extract(line)) { + output += line; + output += '\n'; + } else { + // buffer exhausted, fetch more data + int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain(), 5000); + if (len == 0) { + // eof + return; + } + out_buf.Update(len); } - out_buf.Update(len); } } @@ -104,7 +134,7 @@ void TestInstance::AssertNoOutput() { string output; ExhaustOutput(output); CPPUNIT_ASSERT_EQUAL_MESSAGE( - "test instanced produced unexpected output", + "unexpected output of test instance " + name, string(""), output); } @@ -112,9 +142,9 @@ void TestInstance::AssertNoOutput() { void TestInstance::ReadErrorLine(string &line) { while (!err_buf.Extract(line)) { // buffer exhausted, fetch more data - int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain()); + int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain(), 5000); if (len == 0) { - throw runtime_error("failed read from child process' stderr"); + throw runtime_error("failed read from stderr of " + name); } err_buf.Update(len); } @@ -122,30 +152,56 @@ void TestInstance::ReadErrorLine(string &line) { void TestInstance::AssertErrorLine(const string &expected) { string line; - ReadErrorLine(line); + if (past_err.empty()) { + ReadErrorLine(line); + } else { + line = past_err.front(); + past_err.pop_front(); + } CPPUNIT_ASSERT_EQUAL_MESSAGE( "unexpected line in stderr", expected, line); } void TestInstance::WaitErrorLine(const string &expected) { + for (list::iterator i(past_err.begin()); i != past_err.end(); ++i) { + if (*i == expected) { + past_err.erase(i); + return; + } + } string line; while (true) { ReadErrorLine(line); if (line == expected) { return; + } else { + past_err.push_back(line); } } } void TestInstance::ExhaustError(string &error) { - while (!err_buf.Extract(error)) { - // buffer exhausted, fetch more data - int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain()); - if (len == 0) { - return; + error.clear(); + for (const auto &line : past_err) { + error += line; + error += '\n'; + } + past_err.clear(); + string line; + while (true) { + if (err_buf.Extract(line)) { + error += line; + error += '\n'; + } else { + // buffer exhausted, fetch more data + int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain(), 5000); + if (len == 0) { + // eof + return; + } + err_buf.Update(len); } - err_buf.Update(len); } } @@ -153,30 +209,32 @@ void TestInstance::AssertNoError() { string error; ExhaustError(error); CPPUNIT_ASSERT_EQUAL_MESSAGE( - "test instanced produced unexpected error output", + "unexpected error output of test instance " + name, string(""), error); } void TestInstance::Terminate() { - proc.Terminate(); + if (!proc.Terminated()) { + proc.Terminate(); + } } void TestInstance::AssertRunning() { CPPUNIT_ASSERT_MESSAGE( - "test instance terminated unexpectedly", + "test instance " + name + " terminated unexpectedly", !proc.Terminated()); } void TestInstance::AssertTerminated() { CPPUNIT_ASSERT_MESSAGE( - "test instance did not terminate as expected", + "test instance " + name + " did not terminate as expected", proc.Terminated()); } void TestInstance::AssertExitStatus(int expected) { CPPUNIT_ASSERT_EQUAL_MESSAGE( - "unexpected exit status from child program", + "unexpected exit status from child program " + name, expected, proc.Join()); }