mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-10 21:40:55 +01:00
Set up basic testing using example files, fix consistency issues in examples
This commit is contained in:
parent
8ec2a647e4
commit
261ceef0da
9 changed files with 75 additions and 12 deletions
|
@ -14,6 +14,6 @@ path = "lib/lib.rs"
|
||||||
basic-sleep = "examples/basic_sleep.rs"
|
basic-sleep = "examples/basic_sleep.rs"
|
||||||
basic-spawn = "examples/basic_spawn.rs"
|
basic-spawn = "examples/basic_spawn.rs"
|
||||||
callbacks = "examples/callbacks.rs"
|
callbacks = "examples/callbacks.rs"
|
||||||
|
captures = "examples/captures.rs"
|
||||||
lots-of-threads = "examples/lots_of_threads.rs"
|
lots-of-threads = "examples/lots_of_threads.rs"
|
||||||
main = "examples/main.rs"
|
|
||||||
scheduler-ordering = "examples/scheduler_ordering.rs"
|
scheduler-ordering = "examples/scheduler_ordering.rs"
|
||||||
|
|
|
@ -9,13 +9,13 @@ use smol_mlua::{
|
||||||
Callbacks, IntoLuaThread, Runtime,
|
Callbacks, IntoLuaThread, Runtime,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAIN_SCRIPT: &str = include_str!("./lua/main.luau");
|
const MAIN_SCRIPT: &str = include_str!("./lua/captures.luau");
|
||||||
|
|
||||||
pub fn main() -> LuaResult<()> {
|
pub fn main() -> LuaResult<()> {
|
||||||
// Set up persistent lua environment
|
// Set up persistent lua environment
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
lua.globals().set(
|
lua.globals().set(
|
||||||
"wait",
|
"sleep",
|
||||||
lua.create_async_function(|_, duration: Option<f64>| async move {
|
lua.create_async_function(|_, duration: Option<f64>| async move {
|
||||||
let duration = duration.unwrap_or_default().max(1.0 / 250.0);
|
let duration = duration.unwrap_or_default().max(1.0 / 250.0);
|
||||||
let before = Instant::now();
|
let before = Instant::now();
|
|
@ -1,9 +1,17 @@
|
||||||
--!nocheck
|
--!nocheck
|
||||||
--!nolint UnknownGlobal
|
--!nolint UnknownGlobal
|
||||||
|
|
||||||
local file = readFile("Cargo.toml")
|
local _, err = pcall(function()
|
||||||
if file ~= nil then
|
local file = readFile("Cargo.toml")
|
||||||
print("Cargo.toml found!")
|
if file ~= nil then
|
||||||
print("Contents:")
|
print("Cargo.toml found!")
|
||||||
print(file)
|
print("Contents:")
|
||||||
|
print(file)
|
||||||
|
else
|
||||||
|
print("Cargo.toml not found!")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
if err ~= nil then
|
||||||
|
print("Error while reading file: " .. err)
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ local start = os.clock()
|
||||||
local counter = 0
|
local counter = 0
|
||||||
for j = 1, 10_000 do
|
for j = 1, 10_000 do
|
||||||
__runtime__spawn(function()
|
__runtime__spawn(function()
|
||||||
wait()
|
sleep()
|
||||||
counter += 1
|
counter += 1
|
||||||
if counter == 10_000 then
|
if counter == 10_000 then
|
||||||
local elapsed = os.clock() - start
|
local elapsed = os.clock() - start
|
|
@ -1,7 +1,7 @@
|
||||||
--!nocheck
|
--!nocheck
|
||||||
--!nolint UnknownGlobal
|
--!nolint UnknownGlobal
|
||||||
|
|
||||||
local NUM_BATCHES = 100
|
local NUM_BATCHES = 10
|
||||||
local NUM_THREADS = 10_000
|
local NUM_THREADS = 10_000
|
||||||
|
|
||||||
print(`Spawning {NUM_BATCHES * NUM_THREADS} threads split into {NUM_BATCHES} batches\n`)
|
print(`Spawning {NUM_BATCHES * NUM_THREADS} threads split into {NUM_BATCHES} batches\n`)
|
||||||
|
|
|
@ -19,7 +19,7 @@ end)
|
||||||
-- which will resume their calling thread with their result automatically
|
-- which will resume their calling thread with their result automatically
|
||||||
__runtime__spawn(function()
|
__runtime__spawn(function()
|
||||||
print(3)
|
print(3)
|
||||||
wait(1)
|
sleep(1)
|
||||||
print(6)
|
print(6)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ pub fn main() -> LuaResult<()> {
|
||||||
// Set up persistent lua environment
|
// Set up persistent lua environment
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
lua.globals().set(
|
lua.globals().set(
|
||||||
"wait",
|
"sleep",
|
||||||
lua.create_async_function(|_, duration: Option<f64>| async move {
|
lua.create_async_function(|_, duration: Option<f64>| async move {
|
||||||
let duration = duration.unwrap_or_default().max(1.0 / 250.0);
|
let duration = duration.unwrap_or_default().max(1.0 / 250.0);
|
||||||
let before = Instant::now();
|
let before = Instant::now();
|
||||||
|
|
|
@ -10,3 +10,6 @@ pub use smol;
|
||||||
pub use callbacks::Callbacks;
|
pub use callbacks::Callbacks;
|
||||||
pub use runtime::Runtime;
|
pub use runtime::Runtime;
|
||||||
pub use traits::{IntoLuaThread, LuaExecutorExt};
|
pub use traits::{IntoLuaThread, LuaExecutorExt};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
52
lib/tests.rs
Normal file
52
lib/tests.rs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
use smol::{fs::read_to_string, Timer};
|
||||||
|
|
||||||
|
use crate::Runtime;
|
||||||
|
|
||||||
|
macro_rules! create_tests {
|
||||||
|
($($name:ident: $value:expr,)*) => { $(
|
||||||
|
#[test]
|
||||||
|
fn $name() -> LuaResult<()> {
|
||||||
|
// Read the test script
|
||||||
|
let script = std::fs::read_to_string(concat!($value, ".luau"))?;
|
||||||
|
|
||||||
|
// Set up persistent lua environment
|
||||||
|
let lua = Lua::new();
|
||||||
|
lua.globals().set(
|
||||||
|
"sleep",
|
||||||
|
lua.create_async_function(|_, duration: Option<f64>| async move {
|
||||||
|
let duration = duration.unwrap_or_default().max(1.0 / 250.0);
|
||||||
|
let before = Instant::now();
|
||||||
|
let after = Timer::after(Duration::from_secs_f64(duration)).await;
|
||||||
|
Ok((after - before).as_secs_f64())
|
||||||
|
})?
|
||||||
|
)?;
|
||||||
|
lua.globals().set(
|
||||||
|
"readFile",
|
||||||
|
lua.create_async_function(|_, path: String| async move {
|
||||||
|
Ok(read_to_string(path).await?)
|
||||||
|
})?
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Load the main script into a runtime and run it until completion
|
||||||
|
let rt = Runtime::new(&lua)?;
|
||||||
|
let main = lua.load(script);
|
||||||
|
rt.push_thread(main, ());
|
||||||
|
rt.run_blocking();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
)* }
|
||||||
|
}
|
||||||
|
|
||||||
|
create_tests! {
|
||||||
|
basic_sleep: "examples/lua/basic_sleep",
|
||||||
|
basic_spawn: "examples/lua/basic_spawn",
|
||||||
|
callbacks: "examples/lua/callbacks",
|
||||||
|
captures: "examples/lua/captures",
|
||||||
|
lots_of_threads: "examples/lua/lots_of_threads",
|
||||||
|
scheduler_ordering: "examples/lua/scheduler_ordering",
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue