feat(test): lua timing checks
This commit is contained in:
parent
b117f3cd4d
commit
92ab9f6fb9
4 changed files with 94 additions and 5 deletions
|
@ -4,9 +4,15 @@
|
|||
|
||||
tu_time_t TIME_STARTED_MICROS = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count();
|
||||
|
||||
static tu_time_t timeOverride = -1UL;
|
||||
|
||||
tu_time_t tu_clock_micros() {
|
||||
if (timeOverride != -1UL) return timeOverride;
|
||||
tu_time_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count();;
|
||||
|
||||
return now - TIME_STARTED_MICROS;
|
||||
}
|
||||
|
||||
void tu_set_override(tu_time_t destTime) {
|
||||
timeOverride = destTime;
|
||||
}
|
|
@ -5,4 +5,8 @@
|
|||
typedef uint64_t tu_time_t;
|
||||
|
||||
// Provides a high-accuracy time since the program started in microseconds (via std::chrono)
|
||||
tu_time_t tu_clock_micros();
|
||||
tu_time_t tu_clock_micros();
|
||||
|
||||
#ifdef TU_TIME_EXPOSE_TEST
|
||||
void tu_set_override(tu_time_t destTime);
|
||||
#endif
|
|
@ -1,12 +1,14 @@
|
|||
#include "objects/service/script/scriptcontext.h"
|
||||
#include "testutil.h"
|
||||
|
||||
#include "logger.h"
|
||||
#include "objects/datamodel.h"
|
||||
#include "objects/script.h"
|
||||
#include "timeutil.h"
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
std::string luaEval(DATAMODEL_REF m, std::string source) {
|
||||
std::string luaEvalOut(DATAMODEL_REF m, std::string source) {
|
||||
std::stringstream out;
|
||||
Logger::initTest(&out);
|
||||
|
||||
|
@ -15,13 +17,84 @@ std::string luaEval(DATAMODEL_REF m, std::string source) {
|
|||
s->source = source;
|
||||
s->Run();
|
||||
|
||||
Logger::initTest(nullptr);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
void luaEval(DATAMODEL_REF m, std::string source) {
|
||||
auto s = Script::New();
|
||||
m->AddChild(s);
|
||||
s->source = source;
|
||||
s->Run();
|
||||
}
|
||||
|
||||
void test_output(DATAMODEL_REF m) {
|
||||
ASSERT_EQ("INFO: Hello, world!\n", luaEval(m, "print('Hello, world!')"));
|
||||
// ASSERT_EQ("WARN: Some warning here.\n", luaEval(m, "warn('Some warning here.')"));
|
||||
// ASSERT_EQ("ERROR: An error!.\n", luaEval(m, "error('An error!')"));
|
||||
ASSERT_EQ("INFO: Hello, world!\n", luaEvalOut(m, "print('Hello, world!')"));
|
||||
// ASSERT_EQ("WARN: Some warning here.\n", luaEvalOut(m, "warn('Some warning here.')"));
|
||||
// ASSERT_EQ("ERROR: An error!.\n", luaEvalOut(m, "error('An error!')"));
|
||||
}
|
||||
|
||||
void test_wait1(DATAMODEL_REF m) {
|
||||
auto ctx = m->GetService<ScriptContext>();
|
||||
std::stringstream out;
|
||||
Logger::initTest(&out);
|
||||
|
||||
tu_set_override(0);
|
||||
luaEval(m, "wait(1) print('Wait')");
|
||||
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
TT_ADVANCETIME(0.5);
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
TT_ADVANCETIME(0.5);
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("INFO: Wait\n", out.str());
|
||||
|
||||
Logger::initTest(nullptr);
|
||||
}
|
||||
|
||||
void test_wait0(DATAMODEL_REF m) {
|
||||
auto ctx = m->GetService<ScriptContext>();
|
||||
std::stringstream out;
|
||||
Logger::initTest(&out);
|
||||
|
||||
tu_set_override(0);
|
||||
luaEval(m, "wait(0) print('Wait')");
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
TT_ADVANCETIME(0.03);
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("INFO: Wait\n", out.str());
|
||||
|
||||
Logger::initTest(nullptr);
|
||||
}
|
||||
|
||||
void test_delay(DATAMODEL_REF m) {
|
||||
auto ctx = m->GetService<ScriptContext>();
|
||||
std::stringstream out;
|
||||
Logger::initTest(&out);
|
||||
|
||||
tu_set_override(0);
|
||||
luaEval(m, "delay(1, function() print('Delay') end)");
|
||||
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
TT_ADVANCETIME(0.5);
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("", out.str());
|
||||
|
||||
TT_ADVANCETIME(0.5);
|
||||
ctx->RunSleepingThreads();
|
||||
ASSERT_EQ("INFO: Delay\n", out.str());
|
||||
|
||||
Logger::initTest(nullptr);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
@ -29,6 +102,9 @@ int main() {
|
|||
m->Init(true);
|
||||
|
||||
test_output(m);
|
||||
test_wait1(m);
|
||||
test_wait0(m);
|
||||
test_delay(m);
|
||||
|
||||
return TEST_STATUS;
|
||||
}
|
|
@ -13,6 +13,9 @@
|
|||
|
||||
#define DATAMODEL_REF std::shared_ptr<DataModel>
|
||||
|
||||
#define TU_TIME_EXPOSE_TEST
|
||||
#define TT_ADVANCETIME(secs) tu_set_override(tu_clock_micros() + secs * 1'000'000);
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue