simplify ContextFixture for luajit

This commit is contained in:
falsycat 2023-09-03 11:38:59 +09:00
parent 316cf04605
commit 397984b3a7
4 changed files with 22 additions and 143 deletions

View File

@ -7,11 +7,11 @@ using LuaJIT_Context = nf7::core::luajit::test::ContextFixture;
using LuaJIT_Value = nf7::core::luajit::test::ContextFixture;
TEST_P(LuaJIT_Context, CreateAndDestroy) {
auto sut = env_->Get<nf7::core::luajit::Context>();
auto sut = env().Get<nf7::core::luajit::Context>();
EXPECT_EQ(sut->kind(), GetParam());
}
TEST_P(LuaJIT_Context, Register) {
auto sut = env_->Get<nf7::core::luajit::Context>();
auto sut = env().Get<nf7::core::luajit::Context>();
sut->Exec([](auto& ctx) {
lua_createtable(*ctx, 0, 0);
auto value = ctx.Register();
@ -23,7 +23,7 @@ TEST_P(LuaJIT_Context, Register) {
ConsumeTasks();
}
TEST_P(LuaJIT_Context, Query) {
auto sut = env_->Get<nf7::core::luajit::Context>();
auto sut = env().Get<nf7::core::luajit::Context>();
std::shared_ptr<nf7::core::luajit::Value> value;

View File

@ -18,143 +18,22 @@
#include "iface/subsys/parallelism.hh"
#include "iface/env.hh"
#include "iface/env_test.hh"
namespace nf7::core::luajit::test {
class ContextFixture : public ::testing::TestWithParam<Context::Kind> {
private:
class AsyncDriver final {
public:
explicit AsyncDriver(ContextFixture& parent) noexcept : parent_(parent) { }
void BeginBusy() noexcept { async_busy_ = true; }
void EndBusy() noexcept {
async_busy_ = false;
async_busy_.notify_all();
}
void Drive(AsyncTask&& task) noexcept {
try {
task(param_);
} catch (const Exception& e) {
std::cerr
<< "unexpected exception while async task execution:\n"
<< e << std::endl;
std::abort();
}
}
AsyncTask::Time tick() const noexcept {
const auto now = std::chrono::system_clock::now();
return std::chrono::time_point_cast<AsyncTask::Time::duration>(now);
}
bool nextIdleInterruption() const noexcept { return !parent_.alive_; }
bool nextTaskInterruption() const noexcept { return false; }
void Wait() { async_busy_.wait(true); }
private:
ContextFixture& parent_;
AsyncTaskContext param_;
std::atomic<bool> async_busy_ = false;
};
class SyncDriver final {
public:
explicit SyncDriver(ContextFixture& parent) noexcept : parent_(parent) { }
void BeginBusy() noexcept { }
void EndBusy() noexcept { }
void Drive(SyncTask&& task) noexcept {
try {
task(param_);
} catch (const Exception& e) {
std::cerr
<< "unexpected exception while sync task execution:\n"
<< e << std::endl;
std::abort();
}
}
SyncTask::Time tick() const noexcept {
const auto now = std::chrono::system_clock::now();
return std::chrono::time_point_cast<SyncTask::Time::duration>(now);
}
bool nextIdleInterruption() const noexcept {
return 0 == parent_.syncq_->size();
}
bool nextTaskInterruption() const noexcept { return false; }
private:
ContextFixture& parent_;
SyncTaskContext param_;
};
class ContextFixture :
public nf7::test::EnvFixtureWithTasking,
public ::testing::WithParamInterface<Context::Kind> {
public:
ContextFixture() noexcept : async_driver_(*this) { }
protected:
void SetUp() override {
syncq_ = std::make_shared<SimpleTaskQueue<SyncTask>>();
asyncq_ = std::make_shared<SimpleTaskQueue<AsyncTask>>();
env_.emplace(SimpleEnv::FactoryMap {
{
typeid(subsys::Concurrency), [this](auto&) {
return std::make_shared<
WrappedTaskQueue<subsys::Concurrency>>(syncq_);
},
},
{
typeid(subsys::Parallelism), [this](auto&) {
return std::make_shared<
WrappedTaskQueue<subsys::Parallelism>>(asyncq_);
},
},
{
typeid(Context), [this](auto& env) {
return Context::Create(env, GetParam());
},
}
});
thread_ = std::thread {[this]() { asyncq_->Drive(async_driver_); }};
}
void TearDown() override {
ConsumeTasks();
env_ = std::nullopt;
WaitAsyncTasks(std::chrono::seconds(3));
alive_ = false;
asyncq_->Wake();
thread_.join();
asyncq_ = nullptr;
syncq_ = nullptr;
}
void ConsumeTasks() noexcept {
for (uint32_t i = 0; i < 16; ++i) {
SyncDriver sync_driver {*this};
syncq_->Drive(sync_driver);
WaitAsyncTasks(std::chrono::seconds(1));
}
}
void WaitAsyncTasks(auto dur) noexcept {
if (!asyncq_->WaitForEmpty(dur)) {
std::cerr << "timeout while waiting for task execution" << std::endl;
std::abort();
}
async_driver_.Wait();
}
protected:
std::shared_ptr<SimpleTaskQueue<SyncTask>> syncq_;
std::shared_ptr<SimpleTaskQueue<AsyncTask>> asyncq_;
std::optional<SimpleEnv> env_;
private:
std::atomic<bool> alive_ = true;
uint32_t async_cycle_ = 0;
std::thread thread_;
AsyncDriver async_driver_;
ContextFixture() noexcept
: EnvFixtureWithTasking({
{
typeid(Context), [](auto& env) {
return Context::Create(env, GetParam());
},
},
}) { }
};
} // namespace nf7::core::luajit::test

View File

@ -26,7 +26,7 @@ class LuaJIT_Lambda : public nf7::core::luajit::test::ContextFixture {
std::shared_ptr<nf7::core::luajit::Value> Compile(
const char* script) noexcept {
auto lua = env_->Get<nf7::core::luajit::Context>();
auto lua = env().Get<nf7::core::luajit::Context>();
std::shared_ptr<nf7::core::luajit::Value> func;
lua->Exec([&](auto& lua) {
@ -43,7 +43,7 @@ class LuaJIT_Lambda : public nf7::core::luajit::test::ContextFixture {
const std::vector<nf7::Value>& out = {},
nf7::Env* env = nullptr) {
if (nullptr == env) {
env = &*env_;
env = &this->env();
}
auto func = Compile(script);
@ -100,7 +100,7 @@ TEST_P(LuaJIT_Lambda, CtxMultiRecvWithDelay) {
"nf7:assert(\"null\" == ctx:recv():type())\n"
"nf7:assert(\"integer\" == ctx:recv():type())");
auto sut = std::make_shared<nf7::core::luajit::Lambda>(*env_, func);
auto sut = std::make_shared<nf7::core::luajit::Lambda>(env(), func);
sut->taker()->Take(nf7::Value::Null {});
ConsumeTasks();
EXPECT_EQ(sut->exitCount(), 0);
@ -145,7 +145,7 @@ TEST_P(LuaJIT_Lambda, CtxSleep) {
const auto clock = std::make_shared<nf7::core::Clock>();
nf7::SimpleEnv env {{
{typeid(nf7::subsys::Clock), [&](auto&) { return clock; }},
}, *env_};
}, this->env()};
clock->Tick();
const auto begin = clock->now();
@ -193,7 +193,7 @@ TEST_P(LuaJIT_Lambda, CtxLogging) {
nf7::SimpleEnv env {{
{typeid(nf7::subsys::Logger), [&](auto&) { return logger; }},
}, *env_};
}, this->env()};
Expect(
"local ctx = ...\n"

View File

@ -16,7 +16,7 @@ class LuaJIT_Thread : public nf7::core::luajit::test::ContextFixture {
template <typename... Args>
void TestThread(
const auto& setup, const char* script, Args&&... args) {
auto lua = env_->Get<nf7::core::luajit::Context>();
auto lua = env().Get<nf7::core::luajit::Context>();
auto called = uint32_t {0};
lua->Exec([&](auto& lua) {
const auto compile = luaL_loadstring(*lua, script);