aboutsummaryrefslogtreecommitdiff
path: root/tests/tobeclosed.lua
blob: ef09df34ce72221053bcfd67b9663f9515866195 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
--
-- TOBECLOSED.LUA           Copyright (C) 2024 benoit Germain <bnt.germain@gmail.com>
--

local require_lanes_result_1, require_lanes_result_2 = require "lanes"
print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
local lanes = require_lanes_result_1

local WR = function(...)
    local str=""
    for i=1,select('#',...) do
        local v = select(i,...)
        if type(v) == "function" then
            local infos = debug.getinfo(v)
            --[[for k,v in pairs(infos) do
                print(k,v)
            end]]
            v = infos.source..":"..infos.linedefined
        end
        str= str..tostring(v).."\t"
    end
    if io then
        io.stderr:write(str.."\n")
    end
end

-- #################################################################################################
-- test that table and function handlers work fine, even with upvalues
WR "================================================================================================"
WR "Basic to-be-closed"
do
    local closed_by_f = false
    local closed_by_t = false
    do
        local close_handler_f = function(linda_, err_)
            WR("f closing ", linda_)
            closed_by_f = true
        end
        local lf <close> = lanes.linda("closed by f", close_handler_f)

        local close_handler_t = setmetatable({},
            {
                __call = function(self_, linda_)
                    WR("t closing ", linda_)
                    closed_by_t = true
                end
            }
        )
        local lt <close> = lanes.linda("closed by t", close_handler_t)
    end
    assert(closed_by_f == true)
    assert(closed_by_t == true)
end


-- #################################################################################################
-- test that linda transfer still works even when they contain a close handler
WR "================================================================================================"
WR "Through Linda"
do
    local l = lanes.linda("channel")

    local close_handler_f = function(linda_, err_)
        WR("f closing ", linda_)
        linda_:set("closed", true)
    end
    local l_in = lanes.linda("voyager", close_handler_f)
    l:set("trip", l_in)

    do

        local _, l_out <close> = l:get("trip")
    end
    local _count, _closed = l_in:get("closed")
    assert(_count == 1 and _closed == true)
end

-- #################################################################################################
-- test that lane closing works
WR "================================================================================================"
WR "Lane closing"
do
    local lane_body = function()
        WR "In lane body"
        lanes.sleep(1)
        return "success"
    end

    local h = lanes.gen("*", { name = 'auto' }, lane_body)()
    do
        local tobeclosed <close> = h
    end
    assert(h.status == "done")
    return "success"
end

-- #################################################################################################
-- test that linda with a close handler can still be transferred in a Lane
WR "================================================================================================"
WR "Linda closing through Lane"
do
    local l = lanes.linda("channel")
    local lane_body = function(l_arg_)
        WR "In lane body"
        -- linda obtained through a linda
        local _count, l_out <close> = l:get("trip")
        -- linda from arguments
        local l_arg <close> = l_arg_
        return true
    end

    local close_handler_f = function(linda_, err_)
        WR("f closing ", linda_)
        local _count, _closed = linda_:get("closed")
        linda_:set("closed", (_closed or 0) + 1)
    end
    local l_in = lanes.linda("voyager", close_handler_f)
    l:set("trip", l_in)

    do
    lanes.gen("*", { name = 'auto' }, lane_body)(l_in):join()
    end
    local _count, _closed = l_in:get("closed")
    assert(_count == 1 and _closed == 2)
end

WR "================================================================================================"
WR "TEST OK"