2021-10-14 15:51:38 -04:00
|
|
|
#include "drivertests.hpp"
|
|
|
|
|
#include "drivenengine.hpp"
|
2021-11-14 15:57:18 -05:00
|
|
|
#include "world.hpp"
|
2021-10-14 15:51:38 -04:00
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
|
|
static void write_closed_message(Channel *ch, StreamBuffer *out) {
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << "Chan " << ch->chid() << " closed [" << ch->error() << "]\n";
|
|
|
|
|
out->write_bytes(oss.str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void dump_lines(StreamBuffer *in, StreamBuffer *out, int chid) {
|
|
|
|
|
while (true) {
|
|
|
|
|
std::string l = in->readline();
|
|
|
|
|
if (l == "") break;
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << "Chan " << chid << ": " << l;
|
|
|
|
|
out->write_bytes(oss.str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This test allows input on stdin or on port 8085.
|
|
|
|
|
// You can type lines and see them echoed.
|
|
|
|
|
class DriverListenTest : public DrivenEngine {
|
|
|
|
|
public:
|
|
|
|
|
std::vector<UniqueChannel> channels_;
|
2021-10-14 16:41:24 -04:00
|
|
|
virtual void event_init(int argc, char *argv[]) {
|
2021-10-14 15:51:38 -04:00
|
|
|
listen_port(8085);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void event_update() {
|
|
|
|
|
while (true) {
|
|
|
|
|
UniqueChannel ch = new_incoming_channel();
|
|
|
|
|
if (ch == nullptr) break;
|
|
|
|
|
ch->set_readline(true);
|
|
|
|
|
channels_.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Channel *stdioch = get_stdio_channel();
|
|
|
|
|
dump_lines(stdioch->in(), stdioch->out(), 0);
|
|
|
|
|
std::vector<UniqueChannel> keep;
|
|
|
|
|
for (UniqueChannel &ch : channels_) {
|
|
|
|
|
dump_lines(ch->in(), stdioch->out(), ch->chid());
|
|
|
|
|
if (ch->closed()) {
|
|
|
|
|
write_closed_message(ch.get(), stdioch->out());
|
|
|
|
|
} else {
|
|
|
|
|
keep.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
channels_ = std::move(keep);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This test connects to a public webserver and prints
|
|
|
|
|
// the output from the server.
|
|
|
|
|
class DriverWebServerTest : public DrivenEngine {
|
|
|
|
|
public:
|
|
|
|
|
std::vector<UniqueChannel> channels_;
|
2021-10-14 16:41:24 -04:00
|
|
|
virtual void event_init(int argc, char *argv[]) {
|
2021-10-14 15:51:38 -04:00
|
|
|
UniqueChannel ch = new_outgoing_channel("stanford.edu:80");
|
|
|
|
|
ch->out()->write_bytes("GET http://stanford.edu/index.html HTTP/1.1\n\n");
|
|
|
|
|
channels_.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void event_update() {
|
|
|
|
|
Channel *stdioch = get_stdio_channel();
|
|
|
|
|
dump_lines(stdioch->in(), stdioch->out(), 0);
|
|
|
|
|
std::vector<UniqueChannel> keep;
|
|
|
|
|
for (UniqueChannel &ch : channels_) {
|
|
|
|
|
dump_lines(ch->in(), stdioch->out(), ch->chid());
|
|
|
|
|
if (ch->closed()) {
|
|
|
|
|
write_closed_message(ch.get(), stdioch->out());
|
|
|
|
|
} else {
|
|
|
|
|
keep.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
channels_ = std::move(keep);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This test produces a DNS resolution failure.
|
|
|
|
|
class DriverDNSFailTest : public DrivenEngine {
|
|
|
|
|
public:
|
|
|
|
|
std::vector<UniqueChannel> channels_;
|
2021-10-14 16:41:24 -04:00
|
|
|
virtual void event_init(int argc, char *argv[]) {
|
2021-10-14 15:51:38 -04:00
|
|
|
UniqueChannel ch = new_outgoing_channel("akjsdkajshdakjshd.alk:80");
|
|
|
|
|
ch->out()->write_bytes("GET http://stanford.edu/index.html HTTP/1.1\n\n");
|
|
|
|
|
channels_.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void event_update() {
|
|
|
|
|
Channel *stdioch = get_stdio_channel();
|
|
|
|
|
dump_lines(stdioch->in(), stdioch->out(), 0);
|
|
|
|
|
std::vector<UniqueChannel> keep;
|
|
|
|
|
for (UniqueChannel &ch : channels_) {
|
|
|
|
|
dump_lines(ch->in(), stdioch->out(), ch->chid());
|
|
|
|
|
if (ch->closed()) {
|
|
|
|
|
write_closed_message(ch.get(), stdioch->out());
|
|
|
|
|
} else {
|
|
|
|
|
keep.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
channels_ = std::move(keep);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This test just prints the time.
|
|
|
|
|
class DriverPrintClockTest : public DrivenEngine {
|
|
|
|
|
public:
|
2021-11-04 13:40:42 -04:00
|
|
|
int count_;
|
|
|
|
|
double last_clock_;
|
2021-10-14 16:41:24 -04:00
|
|
|
virtual void event_init(int argc, char *argv[]) {
|
2021-11-04 13:40:42 -04:00
|
|
|
count_ = 0;
|
|
|
|
|
last_clock_ = 0.0;
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void event_update() {
|
2021-11-04 13:40:42 -04:00
|
|
|
double clock = get_clock();
|
|
|
|
|
if (clock > last_clock_ + 0.5) {
|
|
|
|
|
stdostream() << std::fixed << std::setprecision(2) << clock << " ";
|
|
|
|
|
count_++;
|
|
|
|
|
last_clock_ = clock;
|
|
|
|
|
}
|
|
|
|
|
if (count_ == 10) {
|
|
|
|
|
stdostream() << std::endl;
|
|
|
|
|
count_ = 0;
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-14 15:57:18 -05:00
|
|
|
|
|
|
|
|
class RunUnitTests : public DrivenEngine {
|
|
|
|
|
private:
|
|
|
|
|
UniqueWorld world_;
|
|
|
|
|
|
|
|
|
|
void event_init(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
world_.reset(new World(util::WORLD_TYPE_STANDALONE));
|
|
|
|
|
world_->update_source(get_lua_source());
|
|
|
|
|
world_->run_unittests();
|
|
|
|
|
stop_driver();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2021-10-14 15:51:38 -04:00
|
|
|
UniqueDrivenEngine make_DriverListenTest() {
|
|
|
|
|
return UniqueDrivenEngine(new DriverListenTest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UniqueDrivenEngine make_DriverWebServerTest() {
|
|
|
|
|
return UniqueDrivenEngine(new DriverWebServerTest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UniqueDrivenEngine make_DriverDNSFailTest() {
|
|
|
|
|
return UniqueDrivenEngine(new DriverDNSFailTest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UniqueDrivenEngine make_DriverPrintClockTest() {
|
|
|
|
|
return UniqueDrivenEngine(new DriverPrintClockTest);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 15:57:18 -05:00
|
|
|
UniqueDrivenEngine make_RunUnitTests() {
|
|
|
|
|
return UniqueDrivenEngine(new RunUnitTests);
|
|
|
|
|
}
|
2021-10-14 15:51:38 -04:00
|
|
|
|