diff options
Diffstat (limited to 'unit_tests/scripts/coro/regular_function.lua')
-rw-r--r-- | unit_tests/scripts/coro/regular_function.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/unit_tests/scripts/coro/regular_function.lua b/unit_tests/scripts/coro/regular_function.lua new file mode 100644 index 0000000..09aa3b7 --- /dev/null +++ b/unit_tests/scripts/coro/regular_function.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | local lanes = require "lanes".configure() | ||
2 | |||
3 | local utils = lanes.require "_utils" | ||
4 | local PRINT = utils.MAKE_PRINT() | ||
5 | |||
6 | -- a lane body that just returns some value | ||
7 | local returner = function(msg_) | ||
8 | local utils = lanes.require "_utils" | ||
9 | local PRINT = utils.MAKE_PRINT() | ||
10 | PRINT "In lane" | ||
11 | assert(msg_ == "hi") | ||
12 | return "bye" | ||
13 | end | ||
14 | |||
15 | -- a function that returns some value can run in a coroutine | ||
16 | if true then | ||
17 | -- the generator | ||
18 | local g = lanes.coro("*", {name = "auto"}, returner) | ||
19 | |||
20 | -- launch lane | ||
21 | local h = g("hi") | ||
22 | |||
23 | local r = h[1] | ||
24 | assert(r == "bye") | ||
25 | end | ||
26 | |||
27 | -- can't resume a coro after the lane body has returned | ||
28 | if true then | ||
29 | -- the generator | ||
30 | local g = lanes.coro("*", {name = "auto"}, returner) | ||
31 | |||
32 | -- launch lane | ||
33 | local h = g("hi") | ||
34 | |||
35 | -- resuming a lane that terminated execution should raise an error | ||
36 | local b, e = pcall(h.resume, h) | ||
37 | assert(b == false and type(e) == "string") | ||
38 | end | ||