Net serve test improvements, add some impl notes to scheduler

This commit is contained in:
Filip Tibell 2023-08-20 16:55:06 -05:00
parent a3b364ae23
commit 9bb3854554
3 changed files with 31 additions and 13 deletions

View file

@ -14,10 +14,10 @@ where
where
F: Future<Output = ()> + 'fut,
{
let futs = self.futures.try_lock().expect(
"Failed to lock futures queue - \
make sure not to schedule futures during futures resumption",
);
let futs = self
.futures
.try_lock()
.expect("TODO: Make scheduling futures during resumption work");
futs.push(Box::pin(fut))
}
@ -36,7 +36,12 @@ where
F: Future<Output = LuaResult<FR>> + 'fut,
{
let thread = thread.into_lua_thread(self.lua)?;
self.schedule_future(async move {
let futs = self.futures.try_lock().expect(
"Failed to lock futures queue - \
can't schedule future lua threads during futures resumption",
);
futs.push(Box::pin(async move {
match fut.await.and_then(|rets| rets.into_lua_multi(self.lua)) {
Err(e) => {
self.push_err(thread, e)
@ -47,7 +52,7 @@ where
.expect("Failed to schedule future thread");
}
}
});
}));
Ok(())
}

View file

@ -96,11 +96,10 @@ where
loop {
let mut rx = self.futures_break_signal.subscribe();
let mut futs = self
.futures
.try_lock()
.expect("Failed to lock futures queue");
.expect("Failed to lock futures for resumption");
// Wait until we either manually break out of resumption or a future completes
tokio::select! {

View file

@ -1,33 +1,47 @@
local net = require("@lune/net")
local process = require("@lune/process")
local stdio = require("@lune/stdio")
local task = require("@lune/task")
local PORT = 8080
local URL = `http://127.0.0.1:{PORT}`
local RESPONSE = "Hello, lune!"
-- Serve should not block the thread from continuing
local thread = task.delay(0.2, function()
task.spawn(error, "Serve must not block the current thread")
stdio.ewrite("Serve must not block the current thread\n")
task.wait()
process.exit(1)
end)
local handle = net.serve(PORT, function(request)
-- info("Request:", request)
-- info("Responding with", RESPONSE)
print("Request:", request)
print("Responding with", RESPONSE)
assert(request.path == "/some/path")
assert(request.query.key == "param2")
assert(request.query.key2 == "param3")
return RESPONSE
end)
task.cancel(thread)
-- Serve should respond to a request we send to it
local thread2 = task.delay(0.2, function()
stdio.ewrite("Serve should respond to requests in a reasonable amount of time\n")
task.wait()
process.exit(1)
end)
local response = net.request(URL .. "/some/path?key=param1&key=param2&key2=param3").body
assert(response == RESPONSE, "Invalid response from server")
task.cancel(thread)
handle.stop()
task.cancel(thread2)
-- Stopping is not guaranteed to happen instantly since it is async, but
-- it should happen on the next yield, so we wait the minimum amount here
handle.stop()
task.wait()
-- Sending a net request may error if there was