From 45774df1eeeaae0868420104a4cdad4691995dc9 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 1 Jul 2025 08:01:46 +0200 Subject: Clarified interactions between join() and coroutines --- unit_tests/scripts/coro/yielding_function.lua | 45 ++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'unit_tests/scripts') diff --git a/unit_tests/scripts/coro/yielding_function.lua b/unit_tests/scripts/coro/yielding_function.lua index e7367ea..636f094 100644 --- a/unit_tests/scripts/coro/yielding_function.lua +++ b/unit_tests/scripts/coro/yielding_function.lua @@ -17,13 +17,13 @@ local yielder = function(...) local _ack = coroutine.yield(_val) assert(_ack == _i) end - return "done!" + return "bye!" end -------------------------------------------------------------------------------------------------- -- TEST: if we start a non-coroutine lane with a yielding function, we should get an error, right? -------------------------------------------------------------------------------------------------- -if true then +if false then local fun_g = lanes.gen("*", { name = 'auto' }, yielder) local h = fun_g("hello", "world", "!") local err, status, stack = h:join() @@ -48,7 +48,7 @@ local coro_g = lanes.coro("*", {name = "auto"}, yielder) ------------------------------------------------------------------------------------------------- -- TEST: we can resume as many times as the lane yields, then read the returned value on indexing ------------------------------------------------------------------------------------------------- -if true then +if false then -- launch coroutine lane local h = coro_g("hello", "world", "!") -- read the yielded values, sending back the expected index @@ -57,13 +57,13 @@ if true then assert(h:resume(3) == "!") -- the lane return value is available as usual local r = h[1] - assert(r == "done!") + assert(r == "bye!") end --------------------------------------------------------------------------------------------- -- TEST: we can resume as many times as the lane yields, then read the returned value on join --------------------------------------------------------------------------------------------- -if true then +if false then -- launch coroutine lane local h = coro_g("hello", "world", "!") -- read the yielded values, sending back the expected index @@ -72,7 +72,7 @@ if true then assert(h:resume(3) == "!") -- the lane return value is available as usual local s, r = h:join() - assert(h.status == "done" and s == true and r == "done!") + assert(h.status == "done" and s == true and r == "bye!") end --------------------------------------------------------------------------------------------------- @@ -83,23 +83,32 @@ if true then local h = coro_g("hello", "world", "!") -- read the first yielded value, sending back the expected index assert(h:resume(1) == "hello") - -- join the lane. since it will reach a yield point, it remains suspended, and we should get a timeout + -- join the lane. since it will reach a yield point, it unblocks and ends. last yielded values are returned normally local b, r = h:join(0.5) local s = h.status - assert(s == "suspended" and b == nil and r == "timeout", "got " .. s .. " " .. tostring(b) .. " " .. r) - -- trying to resume again should proceed normally, since nothing changed - assert(h:resume(2) == "world") - assert(h:resume(3) == "!") - -- the lane return value is available as usual - local s, r = h:join() - assert(h.status == "done" and s == true and r == "done!") + assert(s == "done" and b == true and r == "world", "got " .. s .. " " .. tostring(b) .. " " .. tostring(r)) end ---------------------------------------------------------- --- TEST: if we index yielded lane, we should get an error ---------------------------------------------------------- --- TODO: implement this test! +----------------------------------------------------------------------- +-- TEST: if we index yielded lane, we should get the last yielded value +----------------------------------------------------------------------- +if false then + -- launch coroutine lane + local h = coro_g("hello", "world", "!") + -- read the first yielded value, sending back the expected index + assert(h:resume(1) == "hello") + -- indexing multiple times gives back the same us the same yielded value + local r1 = h[1] + local r2 = h[1] + local r3 = h[1] + assert(r1 == "world" and r2 == "world" and r3 == "world", "got " .. r1 .. " " .. r2 .. " " .. r3) + assert(h:resume(2) == "world") + -- THERE IS AN INCONSISTENCY: h:resume pulls the yielded values directly out of the lane's stack + -- but h[n] removes them and stores them in the internal values storage table + -- TODO: so we need to decide: should indexing a yielded lane work like resume()? + assert(h:resume(3) == "!") +end ------------------------------------------------------------------------------------- -- TEST: if we close yielded lane, we can join it and get the last yielded values out -- cgit v1.2.3-55-g6feb