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))
37 // wait for command service startup
38 // TODO: timeouts for reading from process
39 WaitOutputLine("listening on TCP port 12354");
40 // connect to command service
41 conn = tcp::Socket("localhost", 12354);
45 TestInstance::~TestInstance() {
50 void TestInstance::WriteInput(const string &data) {
52 const char *i = data.c_str();
53 const char *end = i + data.length();
55 size_t len = proc.WriteIn(i, end - i);
57 throw runtime_error("failed write to child process' stdin");
63 void TestInstance::ReadOutputLine(string &line) {
64 while (!out_buf.Extract(line)) {
65 // buffer exhausted, fetch more data
66 int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain());
68 throw runtime_error("failed read from child process' stdout");
74 void TestInstance::AssertOutputLine(const string &expected) {
77 CPPUNIT_ASSERT_EQUAL_MESSAGE(
78 "unexpected line in stdout",
82 void TestInstance::WaitOutputLine(const string &expected) {
86 if (line == expected) {
92 void TestInstance::ExhaustOutput(string &output) {
93 while (!out_buf.Extract(output)) {
94 // buffer exhausted, fetch more data
95 int len = proc.ReadOut(out_buf.WriteHead(), out_buf.Remain());
103 void TestInstance::AssertNoOutput() {
105 ExhaustOutput(output);
106 CPPUNIT_ASSERT_EQUAL_MESSAGE(
107 "test instance produced unexpected output",
112 void TestInstance::ReadErrorLine(string &line) {
113 while (!err_buf.Extract(line)) {
114 // buffer exhausted, fetch more data
115 int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain());
117 throw runtime_error("failed read from child process' stderr");
123 void TestInstance::AssertErrorLine(const string &expected) {
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(
127 "unexpected line in stderr",
131 void TestInstance::WaitErrorLine(const string &expected) {
135 if (line == expected) {
141 void TestInstance::ExhaustError(string &error) {
142 while (!err_buf.Extract(error)) {
143 // buffer exhausted, fetch more data
144 int len = proc.ReadErr(err_buf.WriteHead(), err_buf.Remain());
152 void TestInstance::AssertNoError() {
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "test instance produced unexpected error output",
161 void TestInstance::Terminate() {
165 void TestInstance::AssertRunning() {
166 CPPUNIT_ASSERT_MESSAGE(
167 "test instance terminated unexpectedly",
171 void TestInstance::AssertTerminated() {
172 CPPUNIT_ASSERT_MESSAGE(
173 "test instance did not terminate as expected",
177 void TestInstance::AssertExitStatus(int expected) {
178 CPPUNIT_ASSERT_EQUAL_MESSAGE(
179 "unexpected exit status from child program",
180 expected, proc.Join());
184 void TestInstance::WaitCommandMessage(const string &line) {
185 WaitCommandLine(" > " + line);
188 void TestInstance::WaitCommandError(const string &line) {
189 WaitCommandLine(" ! " + line);
192 void TestInstance::WaitCommandBroadcast(const string &line) {
193 WaitCommandLine(" @ " + line);
196 void TestInstance::WaitCommandLine(const string &expected) {
199 if (!cmd_buf.Extract(line)) {
200 // buffer exhausted, fetch more data
201 cmd_buf.Update(conn.Recv(cmd_buf.WriteHead(), cmd_buf.Remain()));
204 if (line == expected) {