From d7d756e30680bcff036118b47ac45b740e020ea2 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 26 Jun 2025 09:18:54 +0200 Subject: Preparation for lane:close() and correct to-be-closed variables * lane:join() can no longer be used to read yielded values * same with lane indexing * stub for lane:close(), similar to coroutine.close() (not implemented yet) * preparing tests for to-be-closed variables in yielded coroutine lanes * yielded lanes unlock and terminate properly at Lanes shutdown --- unit_tests/scripts/coro/basics.lua | 99 -------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 unit_tests/scripts/coro/basics.lua (limited to 'unit_tests/scripts/coro/basics.lua') diff --git a/unit_tests/scripts/coro/basics.lua b/unit_tests/scripts/coro/basics.lua deleted file mode 100644 index c0b7a36..0000000 --- a/unit_tests/scripts/coro/basics.lua +++ /dev/null @@ -1,99 +0,0 @@ -local lanes = require "lanes" - -local fixture = require "fixture" -lanes.finally(fixture.throwing_finalizer) - -local utils = lanes.require "_utils" -local PRINT = utils.MAKE_PRINT() - -if true then - -- a lane body that just returns some value - local lane = function(msg_) - local utils = lanes.require "_utils" - local PRINT = utils.MAKE_PRINT() - PRINT "In lane" - assert(msg_ == "hi") - return "bye" - end - - -- the generator - local g1 = lanes.coro("*", {name = "auto"}, lane) - - -- launch lane - local h1 = g1("hi") - - local r = h1[1] - assert(r == "bye") -end - --- a lane coroutine that yields back what got in, one element at a time -local yielder = function(...) - local utils = lanes.require "_utils" - local PRINT = utils.MAKE_PRINT() - PRINT "In lane" - for _i = 1, select('#', ...) do - local _val = select(_i, ...) - PRINT("yielding #", _i, _val) - local _ack = coroutine.yield(_val) - assert(_ack == _i) - end - return "done!" -end - -if true then - -- if we start a non-coroutine lane with a yielding function, we should get an error, right? - local fun_g = lanes.gen("*", { name = 'auto' }, yielder) - local h = fun_g("hello", "world", "!") - local err, status, stack = h:join() - PRINT(err, status, stack) - -- the actual error message is not the same for Lua 5.1 - -- of course, it also has to be different for LuaJIT as well - -- also, LuaJIT prepends a file:line to the actual error message, which Lua5.1 does not. - local msgs = { - ["Lua 5.1"] = jit and "attempt to yield across C-call boundary" or "attempt to yield across metamethod/C-call boundary", - ["Lua 5.2"] = "attempt to yield from outside a coroutine", - ["Lua 5.3"] = "attempt to yield from outside a coroutine", - ["Lua 5.4"] = "attempt to yield from outside a coroutine" - } - local expected_msg = msgs[_VERSION] - PRINT("expected_msg = " .. expected_msg) - assert(err == nil and string.find(status, expected_msg, 1, true) and stack == nil, "status = " .. status) -end - --- the generator -local coro_g = lanes.coro("*", {name = "auto"}, yielder) - -if true then - -- launch coroutine lane - local h2 = coro_g("hello", "world", "!") - -- read the yielded values, sending back the expected index - assert(h2:resume(1) == "hello") - assert(h2:resume(2) == "world") - assert(h2:resume(3) == "!") - -- the lane return value is available as usual - local r = h2[1] - assert(r == "done!") -end - -if true then - -- another coroutine lane - local h3 = coro_g("hello", "world", "!") - - -- yielded values are available as regular return values - assert(h3[1] == "hello" and h3.status == "suspended") - -- since we consumed the returned values, they should not be here when we resume - assert(h3:resume(1) == nil) - - -- similarly, we can get them with join() - local r3, ret3 = h3:join() - assert(r3 == true and ret3 == "world" and h3.status == "suspended") - -- since we consumed the returned values, they should not be here when we resume - assert(h3:resume(2) == nil) - - -- the rest should work as usual - assert(h3:resume(3) == "!") - - -- the final return value of the lane body remains to be read - local r3, ret3 = h3:join() - assert(r3 == true and ret3 == "done!" and h3.status == "done") -end -- cgit v1.2.3-55-g6feb