aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/coro/yielding_function.lua
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2025-07-04 09:18:19 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2025-07-04 09:18:19 +0200
commit7c040842b09f952e98187b65019cd55176a5ddf4 (patch)
tree0e5c3ac027429ddad0a4a85961eb97381a0f360a /unit_tests/scripts/coro/yielding_function.lua
parent72d7b36e020fd3f11ec002c110e7340f667d6628 (diff)
downloadlanes-7c040842b09f952e98187b65019cd55176a5ddf4.tar.gz
lanes-7c040842b09f952e98187b65019cd55176a5ddf4.tar.bz2
lanes-7c040842b09f952e98187b65019cd55176a5ddf4.zip
Split coro tests in several scripts
Diffstat (limited to 'unit_tests/scripts/coro/yielding_function.lua')
-rw-r--r--unit_tests/scripts/coro/yielding_function.lua108
1 files changed, 0 insertions, 108 deletions
diff --git a/unit_tests/scripts/coro/yielding_function.lua b/unit_tests/scripts/coro/yielding_function.lua
deleted file mode 100644
index 6518d1f..0000000
--- a/unit_tests/scripts/coro/yielding_function.lua
+++ /dev/null
@@ -1,108 +0,0 @@
1local lanes = require "lanes"
2
3local fixture = require "fixture"
4lanes.finally(fixture.throwing_finalizer)
5
6local utils = lanes.require "_utils"
7local PRINT = utils.MAKE_PRINT()
8
9-- a lane coroutine that yields back what got in, one element at a time
10local yielder = function(...)
11 local utils = lanes.require "_utils"
12 local PRINT = utils.MAKE_PRINT()
13 PRINT "In lane"
14 for _i = 1, select('#', ...) do
15 local _val = select(_i, ...)
16 PRINT("yielding #", _i, _val)
17 local _ack = coroutine.yield(_val)
18 assert(_ack == _i)
19 end
20 return "bye!"
21end
22
23--------------------------------------------------------------------------------------------------
24-- TEST: if we start a non-coroutine lane with a yielding function, we should get an error, right?
25--------------------------------------------------------------------------------------------------
26if true then
27 local fun_g = lanes.gen("*", { name = 'auto' }, yielder)
28 local h = fun_g("hello", "world", "!")
29 local err, status, stack = h:join()
30 PRINT(err, status, stack)
31 -- the actual error message is not the same for Lua 5.1
32 -- of course, it also has to be different for LuaJIT as well
33 -- also, LuaJIT prepends a file:line to the actual error message, which Lua5.1 does not.
34 local msgs = {
35 ["Lua 5.1"] = jit and "attempt to yield across C-call boundary" or "attempt to yield across metamethod/C-call boundary",
36 ["Lua 5.2"] = "attempt to yield from outside a coroutine",
37 ["Lua 5.3"] = "attempt to yield from outside a coroutine",
38 ["Lua 5.4"] = "attempt to yield from outside a coroutine"
39 }
40 local expected_msg = msgs[_VERSION]
41 PRINT("expected_msg = " .. expected_msg)
42 assert(err == nil and string.find(status, expected_msg, 1, true) and stack == nil, "status = " .. status)
43end
44
45-- the coroutine generator
46local coro_g = lanes.coro("*", {name = "auto"}, yielder)
47
48-------------------------------------------------------------------------------------------------
49-- TEST: we can resume as many times as the lane yields, then read the returned value on indexing
50-------------------------------------------------------------------------------------------------
51if true then
52 -- launch coroutine lane
53 local h = coro_g("hello", "world", "!")
54 -- read the yielded values, sending back the expected index
55 assert(h:resume(1) == "hello")
56 assert(h:resume(2) == "world")
57 assert(h:resume(3) == "!")
58 -- the lane return value is available as usual
59 local r = h[1]
60 assert(r == "bye!")
61end
62
63---------------------------------------------------------------------------------------------
64-- TEST: we can resume as many times as the lane yields, then read the returned value on join
65---------------------------------------------------------------------------------------------
66if true then
67 -- launch coroutine lane
68 local h = coro_g("hello", "world", "!")
69 -- read the yielded values, sending back the expected index
70 assert(h:resume(1) == "hello")
71 assert(h:resume(2) == "world")
72 assert(h:resume(3) == "!")
73 -- the lane return value is available as usual
74 local s, r = h:join()
75 assert(h.status == "done" and s == true and r == "bye!")
76end
77
78---------------------------------------------------
79-- TEST: if we join a yielded lane, the lane aborts
80---------------------------------------------------
81if true then
82 -- launch coroutine lane
83 local h = coro_g("hello", "world", "!")
84 -- read the first yielded value, sending back the expected index
85 assert(h:resume(1) == "hello")
86 -- join the lane. since it will reach a yield point, it unblocks and ends. last yielded values are returned normally
87 local b, r = h:join(0.5)
88 local s = h.status
89 assert(s == "done" and b == true and r == "world", "got " .. s .. " " .. tostring(b) .. " " .. tostring(r))
90end
91
92-------------------------------------------------------------------------
93-- TEST: if we index a yielded lane, we should get the last yielded value
94-------------------------------------------------------------------------
95if true then
96 -- launch coroutine lane
97 local h = coro_g("hello", "world", "!")
98 -- read the first yielded value, sending back the expected index
99 assert(h:resume(1) == "hello")
100 -- indexing multiple times gives back the same us the same yielded value
101 local r1 = h[1]
102 local r2 = h[1]
103 local r3 = h[1]
104 assert(r1 == "world" and r2 == "world" and r3 == "world", "got " .. r1 .. " " .. r2 .. " " .. r3)
105 -- once the lane was indexed, it is no longer resumable (just like after join)
106 local b, e = pcall(h.resume, h, 2)
107 assert(b == false and e == "cannot resume non-suspended coroutine Lane")
108end