aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/coro/regular_function.lua
blob: 09aa3b750fa353d44b9c90d7fdb943a86ff34ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
local lanes = require "lanes".configure()

local utils = lanes.require "_utils"
local PRINT = utils.MAKE_PRINT()

-- a lane body that just returns some value
local returner = function(msg_)
    local utils = lanes.require "_utils"
    local PRINT = utils.MAKE_PRINT()
    PRINT "In lane"
    assert(msg_ == "hi")
    return "bye"
end

-- a function that returns some value can run in a coroutine
if true then
    -- the generator
    local g = lanes.coro("*", {name = "auto"}, returner)

    -- launch lane
    local h = g("hi")

    local r = h[1]
    assert(r == "bye")
end

-- can't resume a coro after the lane body has returned
if true then
    -- the generator
    local g = lanes.coro("*", {name = "auto"}, returner)

    -- launch lane
    local h = g("hi")

    -- resuming a lane that terminated execution should raise an error
    local b, e = pcall(h.resume, h)
    assert(b == false and type(e) == "string")
end