From 9bb385455447a94130eff8947f9dc35aef64f47d Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 20 Aug 2023 16:55:06 -0500 Subject: [PATCH] Net serve test improvements, add some impl notes to scheduler --- src/lune/scheduler/impl_async.rs | 17 +++++++++++------ src/lune/scheduler/impl_runner.rs | 3 +-- tests/net/serve/requests.luau | 24 +++++++++++++++++++----- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/lune/scheduler/impl_async.rs b/src/lune/scheduler/impl_async.rs index 374ab9b..925dd03 100644 --- a/src/lune/scheduler/impl_async.rs +++ b/src/lune/scheduler/impl_async.rs @@ -14,10 +14,10 @@ where where F: Future + '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> + '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(()) } diff --git a/src/lune/scheduler/impl_runner.rs b/src/lune/scheduler/impl_runner.rs index 86efcf1..efcb39b 100644 --- a/src/lune/scheduler/impl_runner.rs +++ b/src/lune/scheduler/impl_runner.rs @@ -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! { diff --git a/tests/net/serve/requests.luau b/tests/net/serve/requests.luau index 522d35f..a2dc551 100644 --- a/tests/net/serve/requests.luau +++ b/tests/net/serve/requests.luau @@ -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