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

View file

@ -96,11 +96,10 @@ where
loop { loop {
let mut rx = self.futures_break_signal.subscribe(); let mut rx = self.futures_break_signal.subscribe();
let mut futs = self let mut futs = self
.futures .futures
.try_lock() .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 // Wait until we either manually break out of resumption or a future completes
tokio::select! { tokio::select! {

View file

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