Skip to content

Commit 3fa9c66

Browse files
committed
tests: Add semi commented tests for wibox garbage collection.
When you run this outside of the test runner, it works, but not with it. Anyway, it adds the regression tests for the previous crash, so it's worth having.
1 parent ff299d9 commit 3fa9c66

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

tests/test-leaks-wibox.lua

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
-- Ensure the wibox and internal `drawin` objects don't leak.
2+
local runner = require("_runner")
3+
-- local wibox = require("wibox")
4+
5+
local steps = {}
6+
7+
-- local wiboxes = setmetatable({}, {__mode = "v"})
8+
local drawins = setmetatable({}, {__mode = "v"})
9+
local keys = setmetatable({}, {__mode = "v"})
10+
local drawables = {}
11+
12+
-- Establish a baseline that it should work using the `key` objects.
13+
table.insert(steps, function()
14+
for _=1, 5 do
15+
table.insert(keys, key { key="#1" })
16+
end
17+
18+
return true
19+
end)
20+
21+
table.insert(steps, function()
22+
for _=1, 5 do
23+
collectgarbage("collect")
24+
end
25+
26+
if #keys > 0 then return end
27+
28+
return true
29+
end)
30+
31+
-- Add some invisible wiboxes.
32+
--
33+
-- Since they are not visible, they have zero internal references and should be
34+
-- GCed without any issue.
35+
table.insert(steps, function()
36+
for _=1, 5 do
37+
local d = drawin {
38+
visible = false,
39+
x = 0,
40+
y = 0,
41+
width = 100,
42+
height = 100,
43+
}
44+
45+
for _=1, 5 do
46+
d.visible = true
47+
d.visible = false
48+
end
49+
50+
table.insert(drawins, d)
51+
table.insert(drawables, d.drawable)
52+
end
53+
54+
return true
55+
end)
56+
57+
table.insert(steps, function()
58+
for _, d in ipairs(drawins) do
59+
d.visible = false
60+
end
61+
62+
return true
63+
end)
64+
65+
table.insert(steps, function()
66+
for _=1, 5 do
67+
collectgarbage("collect")
68+
end
69+
70+
if #drawins > 0 then return end
71+
72+
-- Check if this is a no-op when the drawin is wiped.
73+
for _, d in ipairs(drawables) do
74+
d:refresh()
75+
end
76+
77+
setmetatable(drawables, {__mode = "v"})
78+
79+
for _=1, 5 do
80+
collectgarbage("collect")
81+
end
82+
83+
return true
84+
end)
85+
86+
table.insert(steps, function()
87+
if #drawables > 0 then return end
88+
89+
return true
90+
end)
91+
92+
-- This is flacky and I don't know why. `visible = true` removes the ref from
93+
-- `LUAA_OBJECT_REGISTRY_KEY`, but the GC doesn't seem to agree with that.
94+
95+
-- Try again, but make the wibox visible this time.
96+
--
97+
-- This creates an internal reference and the weak table should hold them.
98+
-- table.insert(steps, function()
99+
-- for i=1, 5 do
100+
-- local d = drawin {
101+
-- visible = true,
102+
-- x = 0,
103+
-- y = 0,
104+
-- width = 100,
105+
-- height = 100,
106+
-- }
107+
-- table.insert(drawins, d)
108+
-- end
109+
--
110+
-- for _=1, 5 do
111+
-- collectgarbage("collect")
112+
-- end
113+
--
114+
-- assert(#drawins == 5)
115+
--
116+
-- return true
117+
-- end)
118+
--
119+
-- table.insert(steps, function()
120+
-- for _, d in ipairs(drawins) do
121+
-- -- This calls `unref`.
122+
-- d.visible = false
123+
-- end
124+
--
125+
-- return true
126+
-- end)
127+
--
128+
-- table.insert(steps, function()
129+
-- for _=1, 5 do
130+
-- collectgarbage("collect")
131+
-- end
132+
--
133+
-- while #drawins > 0 do return end
134+
--
135+
-- return true
136+
-- end)
137+
138+
-- table.insert(steps, function()
139+
-- wibox = require("wibox")
140+
--
141+
-- for i=1, 5 do
142+
-- local w = wibox {
143+
-- visible = true,
144+
-- x = 0,
145+
-- y = 0,
146+
-- width = 100,
147+
-- height = 100,
148+
-- }
149+
-- table.insert(wiboxes, w)
150+
-- table.insert(drawins, w.drawin)
151+
-- end
152+
--
153+
-- return true
154+
-- end)
155+
--
156+
-- table.insert(steps, function()
157+
-- for _, w in ipairs(wiboxes) do
158+
-- w.visible = false
159+
-- end
160+
--
161+
-- return true
162+
-- end)
163+
--
164+
-- table.insert(steps, function()
165+
-- for _=1, 5 do
166+
-- collectgarbage("collect")
167+
-- end
168+
--
169+
-- while #wiboxes > 0 do return end
170+
-- while #drawins > 0 do return end
171+
--
172+
-- return true
173+
-- end)
174+
175+
runner.run_steps(steps)
176+
177+
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

0 commit comments

Comments
 (0)