Migrated engine to using dlmalloc through eng::
This commit is contained in:
144
luprex/core/cpp/eng-tests.cpp
Normal file
144
luprex/core/cpp/eng-tests.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include "wrap-string.hpp"
|
||||
|
||||
#include "eng-tests.hpp"
|
||||
#include "drivenengine.hpp"
|
||||
#include "streambuffer.hpp"
|
||||
#include "world.hpp"
|
||||
|
||||
#include <iomanip>
|
||||
|
||||
static void write_closed_message(Channel *ch, StreamBuffer *out) {
|
||||
eng::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) {
|
||||
eng::string l = in->readline();
|
||||
if (l == "") break;
|
||||
eng::ostringstream oss;
|
||||
oss << "Chan " << chid << ": " << l;
|
||||
out->write_bytes(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
// This test is the minimal possible DrivenEngine.
|
||||
class DriverStubTest : public DrivenEngine {
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
stop_driver();
|
||||
}
|
||||
};
|
||||
|
||||
// This test connects to a public webserver and prints
|
||||
// the output from the server.
|
||||
class DriverWebServerTest : public DrivenEngine {
|
||||
public:
|
||||
eng::vector<SharedChannel> channels_;
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
SharedChannel 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() {
|
||||
SharedChannel stdioch = get_stdio_channel();
|
||||
dump_lines(stdioch->in(), stdioch->out(), 0);
|
||||
eng::vector<SharedChannel> keep;
|
||||
for (SharedChannel &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:
|
||||
eng::vector<SharedChannel> channels_;
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
SharedChannel 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() {
|
||||
SharedChannel stdioch = get_stdio_channel();
|
||||
dump_lines(stdioch->in(), stdioch->out(), 0);
|
||||
eng::vector<SharedChannel> keep;
|
||||
for (SharedChannel &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:
|
||||
int count_;
|
||||
double last_clock_;
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
count_ = 0;
|
||||
last_clock_ = 0.0;
|
||||
}
|
||||
|
||||
virtual void event_update() {
|
||||
double clock = get_clock();
|
||||
if (clock > last_clock_ + 0.5) {
|
||||
int ms = dlmalloc_hash();
|
||||
stdostream() << std::fixed << std::setprecision(2) << clock << " " << std::hex << ms << " ";
|
||||
count_++;
|
||||
last_clock_ = clock;
|
||||
}
|
||||
if (count_ == 4) {
|
||||
stdostream() << std::endl;
|
||||
count_ = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
UniqueDrivenEngine make_DriverStubTest() {
|
||||
return UniqueDrivenEngine(new DriverStubTest);
|
||||
}
|
||||
|
||||
UniqueDrivenEngine make_DriverWebServerTest() {
|
||||
return UniqueDrivenEngine(new DriverWebServerTest);
|
||||
}
|
||||
|
||||
UniqueDrivenEngine make_DriverDNSFailTest() {
|
||||
return UniqueDrivenEngine(new DriverDNSFailTest);
|
||||
}
|
||||
|
||||
UniqueDrivenEngine make_DriverPrintClockTest() {
|
||||
return UniqueDrivenEngine(new DriverPrintClockTest);
|
||||
}
|
||||
|
||||
UniqueDrivenEngine make_RunUnitTests() {
|
||||
return UniqueDrivenEngine(new RunUnitTests);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user