2022-02-23 23:08:28 -05:00
|
|
|
#include "wrap-string.hpp"
|
|
|
|
|
|
2021-10-14 15:51:38 -04:00
|
|
|
#include "drivenengine.hpp"
|
2022-02-23 23:08:28 -05:00
|
|
|
#include "streambuffer.hpp"
|
2021-11-14 15:57:18 -05:00
|
|
|
#include "world.hpp"
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2021-10-14 15:51:38 -04:00
|
|
|
#include <iomanip>
|
|
|
|
|
|
2025-12-18 15:28:05 -05:00
|
|
|
static void write_closed_message(Channel *ch) {
|
|
|
|
|
util::dprint("Chan ", ch->chid(), " closed [", ch->error(), "]\n");
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-18 15:28:05 -05:00
|
|
|
static void dump_lines(StreamBuffer *in, int chid) {
|
2021-10-14 15:51:38 -04:00
|
|
|
while (true) {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string l = in->readline();
|
2021-10-14 15:51:38 -04:00
|
|
|
if (l == "") break;
|
2025-12-18 15:28:05 -05:00
|
|
|
util::dprint("Chan ", chid, ": ", l);
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This test connects to a public webserver and prints
|
|
|
|
|
// the output from the server.
|
|
|
|
|
class DriverWebServerTest : public DrivenEngine {
|
|
|
|
|
public:
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::vector<SharedChannel> channels_;
|
2025-06-16 19:58:26 -04:00
|
|
|
DriverWebServerTest() {
|
2022-03-18 21:52:45 -04:00
|
|
|
SharedChannel ch = new_outgoing_channel("cert:stanford.edu:443");
|
|
|
|
|
ch->out()->write_bytes("GET https://stanford.edu/xbanankjdsh.html HTTP/1.1\n\n");
|
2021-10-14 15:51:38 -04:00
|
|
|
channels_.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 20:57:47 -04:00
|
|
|
virtual void event_update() override {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::vector<SharedChannel> keep;
|
2022-01-05 12:50:16 -05:00
|
|
|
for (SharedChannel &ch : channels_) {
|
2025-12-18 15:28:05 -05:00
|
|
|
dump_lines(ch->in(), ch->chid());
|
2021-10-14 15:51:38 -04:00
|
|
|
if (ch->closed()) {
|
2025-12-18 15:28:05 -05:00
|
|
|
write_closed_message(ch.get());
|
2021-10-14 15:51:38 -04:00
|
|
|
} else {
|
|
|
|
|
keep.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
channels_ = std::move(keep);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This test produces a DNS resolution failure.
|
|
|
|
|
class DriverDNSFailTest : public DrivenEngine {
|
|
|
|
|
public:
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::vector<SharedChannel> channels_;
|
2025-06-16 19:58:26 -04:00
|
|
|
DriverDNSFailTest() {
|
2022-01-05 12:50:16 -05:00
|
|
|
SharedChannel ch = new_outgoing_channel("akjsdkajshdakjshd.alk:80");
|
2021-10-14 15:51:38 -04:00
|
|
|
ch->out()->write_bytes("GET http://stanford.edu/index.html HTTP/1.1\n\n");
|
|
|
|
|
channels_.emplace_back(std::move(ch));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 20:57:47 -04:00
|
|
|
virtual void event_update() override {
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::vector<SharedChannel> keep;
|
2022-01-05 12:50:16 -05:00
|
|
|
for (SharedChannel &ch : channels_) {
|
2025-12-18 15:28:05 -05:00
|
|
|
dump_lines(ch->in(), ch->chid());
|
2021-10-14 15:51:38 -04:00
|
|
|
if (ch->closed()) {
|
2025-12-18 15:28:05 -05:00
|
|
|
write_closed_message(ch.get());
|
2021-10-14 15:51:38 -04:00
|
|
|
} 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_;
|
2025-06-16 19:58:26 -04:00
|
|
|
DriverPrintClockTest() {
|
2021-11-04 13:40:42 -04:00
|
|
|
count_ = 0;
|
|
|
|
|
last_clock_ = 0.0;
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
|
2023-10-23 20:57:47 -04:00
|
|
|
virtual void event_update() override {
|
2021-11-04 13:40:42 -04:00
|
|
|
double clock = get_clock();
|
|
|
|
|
if (clock > last_clock_ + 0.5) {
|
2022-02-28 21:57:54 -05:00
|
|
|
int ms = eng::memhash();
|
2025-12-15 22:42:02 -05:00
|
|
|
util::dprint(std::fixed, std::setprecision(2), clock, " ", std::hex, ms);
|
2021-11-04 13:40:42 -04:00
|
|
|
count_++;
|
|
|
|
|
last_clock_ = clock;
|
|
|
|
|
}
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-14 15:57:18 -05:00
|
|
|
|
|
|
|
|
class RunUnitTests : public DrivenEngine {
|
2025-06-16 19:58:26 -04:00
|
|
|
public:
|
2021-11-14 15:57:18 -05:00
|
|
|
UniqueWorld world_;
|
|
|
|
|
|
2025-06-16 19:58:26 -04:00
|
|
|
RunUnitTests() {
|
2023-03-01 16:07:13 -05:00
|
|
|
world_.reset(new World(WORLD_TYPE_MASTER));
|
2025-06-27 20:03:38 -04:00
|
|
|
rescan_lua_source(true);
|
2025-06-16 19:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void event_access(AccessKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override {
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case AccessKind::INVOKE_LUA_SOURCE: {
|
|
|
|
|
world_->update_source(datapk);
|
|
|
|
|
world_->run_unittests();
|
2025-08-04 17:13:34 -04:00
|
|
|
stop_driver();
|
2025-06-16 19:58:26 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
2021-11-14 15:57:18 -05:00
|
|
|
}
|
2023-10-23 20:57:47 -04:00
|
|
|
|
|
|
|
|
void event_update() override {}
|
2021-11-14 15:57:18 -05:00
|
|
|
};
|
|
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
DrivenEngineDefine("driverwebservertest", DriverWebServerTest);
|
|
|
|
|
DrivenEngineDefine("driverdnsfailtest", DriverDNSFailTest);
|
|
|
|
|
DrivenEngineDefine("driverprintclocktest", DriverPrintClockTest);
|
|
|
|
|
DrivenEngineDefine("rununittests", RunUnitTests);
|
2021-10-14 15:51:38 -04:00
|
|
|
|