1 #include "TestInstance.hpp"
3 #include <cppunit/extensions/HelperMacros.h>
13 Process::Arguments combine_args(const TempDir &dir, const Process::Arguments &in, bool cmd) {
14 Process::Arguments out;
15 out.reserve(in.size() + (cmd ? 5 : 3));
16 out.emplace_back("blank");
17 out.insert(out.end(), in.begin(), in.end());
18 out.emplace_back("--save-path");
19 out.emplace_back(dir.Path());
21 out.emplace_back("--cmd-port");
22 out.emplace_back("12354");
29 TestInstance::TestInstance(const Process::Arguments &args, bool cmd)
31 , proc("./blank" BLANK_SUFFIX, combine_args(dir, args, cmd))
36 , name("blank" BLANK_SUFFIX) {
38 // wait for command service startup
39 WaitOutputLine("listening on TCP port 12354");
40 // connect to command service
41 conn = tcp::Socket("localhost", 12354);
43 for (const auto &arg : args) {
49 TestInstance::~TestInstance() {
54 void TestInstance::WriteInput(const string &data) {
56 const char *i = data.c_str();
57 const char *end = i + data.length();
59 size_t len = proc.WriteIn(i, end - i);
61 throw runtime_error("failed write to stdin of " + name);
67 void TestInstance::ReadOutputLine(string &line) {
68 while (!out_buf.Extract(line)) {
69 // buffer exhausted, fetch more data
70 int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain(), 5000);
72 throw runtime_error("failed read from stdout of " + name);
78 void TestInstance::AssertOutputLine(const string &expected) {
80 if (past_out.empty()) {
83 line = past_out.front();
86 CPPUNIT_ASSERT_EQUAL_MESSAGE(
87 "unexpected line in stdout of " + name,
91 void TestInstance::WaitOutputLine(const string &expected) {
92 for (list<string>::iterator i(past_out.begin()); i != past_out.end(); ++i) {
100 ReadOutputLine(line);
101 if (line == expected) {
104 past_out.push_back(line);
109 void TestInstance::ExhaustOutput(string &output) {
111 for (const auto &line : past_out) {
118 if (out_buf.Extract(line)) {
122 // buffer exhausted, fetch more data
123 int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain(), 5000);
133 void TestInstance::AssertNoOutput() {
135 ExhaustOutput(output);
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(
137 "unexpected output of test instance " + name,
142 void TestInstance::ReadErrorLine(string &line) {
143 while (!err_buf.Extract(line)) {
144 // buffer exhausted, fetch more data
145 int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain(), 5000);
147 throw runtime_error("failed read from stderr of " + name);
153 void TestInstance::AssertErrorLine(const string &expected) {
155 if (past_err.empty()) {
158 line = past_err.front();
159 past_err.pop_front();
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
162 "unexpected line in stderr",
166 void TestInstance::WaitErrorLine(const string &expected) {
167 for (list<string>::iterator i(past_err.begin()); i != past_err.end(); ++i) {
168 if (*i == expected) {
176 if (line == expected) {
179 past_err.push_back(line);
184 void TestInstance::ExhaustError(string &error) {
186 for (const auto &line : past_err) {
193 if (err_buf.Extract(line)) {
197 // buffer exhausted, fetch more data
198 int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain(), 5000);
208 void TestInstance::AssertNoError() {
211 CPPUNIT_ASSERT_EQUAL_MESSAGE(
212 "unexpected error output of test instance " + name,
217 void TestInstance::Terminate() {
218 if (!proc.Terminated()) {
223 void TestInstance::AssertRunning() {
224 CPPUNIT_ASSERT_MESSAGE(
225 "test instance " + name + " terminated unexpectedly",
229 void TestInstance::AssertTerminated() {
230 CPPUNIT_ASSERT_MESSAGE(
231 "test instance " + name + " did not terminate as expected",
235 void TestInstance::AssertExitStatus(int expected) {
236 CPPUNIT_ASSERT_EQUAL_MESSAGE(
237 "unexpected exit status from child program " + name,
238 expected, proc.Join());
242 void TestInstance::WaitCommandMessage(const string &line) {
243 WaitCommandLine(" > " + line);
246 void TestInstance::WaitCommandError(const string &line) {
247 WaitCommandLine(" ! " + line);
250 void TestInstance::WaitCommandBroadcast(const string &line) {
251 WaitCommandLine(" @ " + line);
254 void TestInstance::WaitCommandLine(const string &expected) {
257 if (!cmd_buf.Extract(line)) {
258 // buffer exhausted, fetch more data
259 cmd_buf.Update(conn.Recv(cmd_buf.WriteHead(), cmd_buf.Remain()));
262 if (line == expected) {