diff options
Diffstat (limited to 'unit_tests/scripts/coro/yielding_function.lua')
| -rw-r--r-- | unit_tests/scripts/coro/yielding_function.lua | 45 |
1 files changed, 27 insertions, 18 deletions
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(...) | |||
| 17 | local _ack = coroutine.yield(_val) | 17 | local _ack = coroutine.yield(_val) |
| 18 | assert(_ack == _i) | 18 | assert(_ack == _i) |
| 19 | end | 19 | end |
| 20 | return "done!" | 20 | return "bye!" |
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | -------------------------------------------------------------------------------------------------- | 23 | -------------------------------------------------------------------------------------------------- |
| 24 | -- TEST: if we start a non-coroutine lane with a yielding function, we should get an error, right? | 24 | -- TEST: if we start a non-coroutine lane with a yielding function, we should get an error, right? |
| 25 | -------------------------------------------------------------------------------------------------- | 25 | -------------------------------------------------------------------------------------------------- |
| 26 | if true then | 26 | if false then |
| 27 | local fun_g = lanes.gen("*", { name = 'auto' }, yielder) | 27 | local fun_g = lanes.gen("*", { name = 'auto' }, yielder) |
| 28 | local h = fun_g("hello", "world", "!") | 28 | local h = fun_g("hello", "world", "!") |
| 29 | local err, status, stack = h:join() | 29 | local err, status, stack = h:join() |
| @@ -48,7 +48,7 @@ local coro_g = lanes.coro("*", {name = "auto"}, yielder) | |||
| 48 | ------------------------------------------------------------------------------------------------- | 48 | ------------------------------------------------------------------------------------------------- |
| 49 | -- TEST: we can resume as many times as the lane yields, then read the returned value on indexing | 49 | -- TEST: we can resume as many times as the lane yields, then read the returned value on indexing |
| 50 | ------------------------------------------------------------------------------------------------- | 50 | ------------------------------------------------------------------------------------------------- |
| 51 | if true then | 51 | if false then |
| 52 | -- launch coroutine lane | 52 | -- launch coroutine lane |
| 53 | local h = coro_g("hello", "world", "!") | 53 | local h = coro_g("hello", "world", "!") |
| 54 | -- read the yielded values, sending back the expected index | 54 | -- read the yielded values, sending back the expected index |
| @@ -57,13 +57,13 @@ if true then | |||
| 57 | assert(h:resume(3) == "!") | 57 | assert(h:resume(3) == "!") |
| 58 | -- the lane return value is available as usual | 58 | -- the lane return value is available as usual |
| 59 | local r = h[1] | 59 | local r = h[1] |
| 60 | assert(r == "done!") | 60 | assert(r == "bye!") |
| 61 | end | 61 | end |
| 62 | 62 | ||
| 63 | --------------------------------------------------------------------------------------------- | 63 | --------------------------------------------------------------------------------------------- |
| 64 | -- TEST: we can resume as many times as the lane yields, then read the returned value on join | 64 | -- TEST: we can resume as many times as the lane yields, then read the returned value on join |
| 65 | --------------------------------------------------------------------------------------------- | 65 | --------------------------------------------------------------------------------------------- |
| 66 | if true then | 66 | if false then |
| 67 | -- launch coroutine lane | 67 | -- launch coroutine lane |
| 68 | local h = coro_g("hello", "world", "!") | 68 | local h = coro_g("hello", "world", "!") |
| 69 | -- read the yielded values, sending back the expected index | 69 | -- read the yielded values, sending back the expected index |
| @@ -72,7 +72,7 @@ if true then | |||
| 72 | assert(h:resume(3) == "!") | 72 | assert(h:resume(3) == "!") |
| 73 | -- the lane return value is available as usual | 73 | -- the lane return value is available as usual |
| 74 | local s, r = h:join() | 74 | local s, r = h:join() |
| 75 | assert(h.status == "done" and s == true and r == "done!") | 75 | assert(h.status == "done" and s == true and r == "bye!") |
| 76 | end | 76 | end |
| 77 | 77 | ||
| 78 | --------------------------------------------------------------------------------------------------- | 78 | --------------------------------------------------------------------------------------------------- |
| @@ -83,23 +83,32 @@ if true then | |||
| 83 | local h = coro_g("hello", "world", "!") | 83 | local h = coro_g("hello", "world", "!") |
| 84 | -- read the first yielded value, sending back the expected index | 84 | -- read the first yielded value, sending back the expected index |
| 85 | assert(h:resume(1) == "hello") | 85 | assert(h:resume(1) == "hello") |
| 86 | -- join the lane. since it will reach a yield point, it remains suspended, and we should get a timeout | 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) | 87 | local b, r = h:join(0.5) |
| 88 | local s = h.status | 88 | local s = h.status |
| 89 | assert(s == "suspended" and b == nil and r == "timeout", "got " .. s .. " " .. tostring(b) .. " " .. r) | 89 | assert(s == "done" and b == true and r == "world", "got " .. s .. " " .. tostring(b) .. " " .. tostring(r)) |
| 90 | -- trying to resume again should proceed normally, since nothing changed | ||
| 91 | assert(h:resume(2) == "world") | ||
| 92 | assert(h:resume(3) == "!") | ||
| 93 | -- the lane return value is available as usual | ||
| 94 | local s, r = h:join() | ||
| 95 | assert(h.status == "done" and s == true and r == "done!") | ||
| 96 | end | 90 | end |
| 97 | 91 | ||
| 98 | --------------------------------------------------------- | 92 | ----------------------------------------------------------------------- |
| 99 | -- TEST: if we index yielded lane, we should get an error | 93 | -- TEST: if we index yielded lane, we should get the last yielded value |
| 100 | --------------------------------------------------------- | 94 | ----------------------------------------------------------------------- |
| 101 | -- TODO: implement this test! | 95 | if false 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 | assert(h:resume(2) == "world") | ||
| 102 | 106 | ||
| 107 | -- THERE IS AN INCONSISTENCY: h:resume pulls the yielded values directly out of the lane's stack | ||
| 108 | -- but h[n] removes them and stores them in the internal values storage table | ||
| 109 | -- TODO: so we need to decide: should indexing a yielded lane work like resume()? | ||
| 110 | assert(h:resume(3) == "!") | ||
| 111 | end | ||
| 103 | 112 | ||
| 104 | ------------------------------------------------------------------------------------- | 113 | ------------------------------------------------------------------------------------- |
| 105 | -- TEST: if we close yielded lane, we can join it and get the last yielded values out | 114 | -- TEST: if we close yielded lane, we can join it and get the last yielded values out |
