#![cfg(feature = "async")]
use htl::mlua_isle::runtime::CancelToken;
use htl::task::{RecvChannel, Sender};
use htl::teal::HostModule as _;
use htl::{Htl, host_module};
use std::sync::mpsc;
use std::time::Duration;
pub struct Host {
events_tx: mpsc::Sender<Sender<i64>>,
}
#[host_module(name = "host")]
impl Host {
pub fn count(&self, lua: &htl::mlua::Lua) -> i64 {
lua.used_memory() as i64
}
pub fn events(&self, lua: &htl::mlua::Lua) -> RecvChannel<i64> {
let (tx, ch) = RecvChannel::<i64>::new(lua, 1).unwrap();
let _ = self.events_tx.send(tx);
ch
}
pub async fn counted(&self, lua: &htl::mlua::Lua) -> i64 {
tokio::task::yield_now().await;
lua.used_memory() as i64
}
pub fn after(&self, n: i64, lua: &htl::mlua::Lua) -> i64 {
let _ = lua;
n
}
pub fn named_this(&self, this: &htl::mlua::Lua) -> i64 {
this.used_memory() as i64
}
}
fn host() -> (Htl, mpsc::Receiver<Sender<i64>>) {
let h = Htl::new().unwrap();
h.install_task_lib().unwrap();
let (events_tx, events_rx) = mpsc::channel();
Host { events_tx }.htl_preload(&h).unwrap();
(h, events_rx)
}
#[test]
fn the_declaration_leaves_the_lua_parameter_out() {
assert!(
Host::DECL.contains("count: function(self: host): integer\n"),
"{}",
Host::DECL
);
}
#[test]
fn require_host_count_from_teal_returns_an_integer() {
let (h, _events_rx) = host();
let n: i64 = h
.lua()
.load("local host = require('host'); return host:count()")
.eval()
.unwrap();
assert!(n > 0, "mlua's used_memory should be > 0, got {n}");
}
#[test]
fn the_channel_methods_declaration_also_leaves_lua_out() {
assert!(
Host::DECL.contains("events: function(self: host): task.RecvChannel<integer>\n"),
"{}",
Host::DECL
);
}
#[test]
fn teal_receives_a_value_sent_only_after_events_handed_out_the_sender() {
let (h, events_rx) = host();
let sender = std::thread::spawn(move || {
let tx = events_rx
.recv_timeout(Duration::from_secs(10))
.expect("events() did not hand out the sender");
tx.try_send(42).unwrap();
});
h.run_blocking(
"local host = require('host') \
local ch = host:events() \
local v, ok = ch:recv() \
GOT = v \
OK = ok",
"=test",
&[],
&CancelToken::new(),
)
.unwrap();
sender.join().unwrap();
let got: i64 = h.lua().globals().get("GOT").unwrap();
let ok: bool = h.lua().globals().get("OK").unwrap();
assert_eq!(got, 42);
assert!(ok);
}
#[test]
fn the_async_methods_declaration_also_leaves_lua_out() {
assert!(
Host::DECL.contains("counted: function(self: host): integer ---@async"),
"{}",
Host::DECL
);
}
#[tokio::test]
async fn an_async_method_taking_lua_runs_through_call_async() {
let (h, _events_rx) = host();
let f: htl::mlua::Function = h
.lua()
.load("return function() local host = require('host') return host:counted() end")
.eval()
.unwrap();
let got: i64 = f.call_async(()).await.unwrap();
assert!(got > 0, "{got}");
}
#[test]
fn the_declaration_of_a_lua_parameter_after_a_regular_one_keeps_the_regular_one() {
assert!(
Host::DECL.contains("after: function(self: host, n: integer): integer\n"),
"{}",
Host::DECL
);
}
#[test]
fn a_lua_parameter_after_a_regular_one_does_not_disturb_its_value() {
let (h, _events_rx) = host();
let n: i64 = h
.lua()
.load("local host = require('host'); return host:after(7)")
.eval()
.unwrap();
assert_eq!(n, 7);
}
#[test]
fn a_lua_parameter_named_this_does_not_collide_with_the_receiver() {
let (h, _events_rx) = host();
let n: i64 = h
.lua()
.load("local host = require('host'); return host:named_this()")
.eval()
.unwrap();
assert!(n > 0, "{n}");
}