aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-01-01 12:14:56 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-01-01 12:14:56 -0200
commitc6f7181e910b6b2ff1346b5486a31be87b1da5af (patch)
tree92cc716487c83ecd9860444f23fd55ef65358cbb /testes
parent437a5b07d415e1a74160ddfd804017171d6cc5cb (diff)
downloadlua-c6f7181e910b6b2ff1346b5486a31be87b1da5af.tar.gz
lua-c6f7181e910b6b2ff1346b5486a31be87b1da5af.tar.bz2
lua-c6f7181e910b6b2ff1346b5486a31be87b1da5af.zip
No more LUA_ERRGCMM errors
Errors in finalizers (__gc metamethods) are never propagated. Instead, they generate a warning.
Diffstat (limited to 'testes')
-rw-r--r--testes/all.lua11
-rw-r--r--testes/api.lua28
-rw-r--r--testes/gc.lua87
3 files changed, 65 insertions, 61 deletions
diff --git a/testes/all.lua b/testes/all.lua
index bde4195e..506afad2 100644
--- a/testes/all.lua
+++ b/testes/all.lua
@@ -190,12 +190,17 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage()
190dofile('files.lua') 190dofile('files.lua')
191 191
192if #msgs > 0 then 192if #msgs > 0 then
193 warn("*tests not performed:\n ") 193 warn("#tests not performed:\n ")
194 for i=1,#msgs do 194 for i=1,#msgs do
195 warn(msgs[i]); warn("\n ") 195 warn(msgs[i]); warn("\n ")
196 end 196 end
197 warn("\n")
197end 198end
198 199
200print("(there should be two warnings now)")
201warn("#This is "); warn("an expected"); warn(" warning\n")
202warn("#This is"); warn(" another one\n")
203
199-- no test module should define 'debug' 204-- no test module should define 'debug'
200assert(debug == nil) 205assert(debug == nil)
201 206
@@ -219,10 +224,6 @@ local _G, showmem, print, format, clock, time, difftime, assert, open =
219local fname = T and "time-debug.txt" or "time.txt" 224local fname = T and "time-debug.txt" or "time.txt"
220local lasttime 225local lasttime
221 226
222
223warn("*This is "); warn("an expected"); warn(" warning\n")
224warn("*This is"); warn(" another one\n")
225
226if not usertests then 227if not usertests then
227 -- open file with time of last performed test 228 -- open file with time of last performed test
228 local f = io.open(fname) 229 local f = io.open(fname)
diff --git a/testes/api.lua b/testes/api.lua
index b4d63866..893a36cb 100644
--- a/testes/api.lua
+++ b/testes/api.lua
@@ -114,13 +114,12 @@ end
114 114
115-- testing warnings 115-- testing warnings
116T.testC([[ 116T.testC([[
117 warning "*This " 117 warning "#This shold be a"
118 warning "warning " 118 warning " single "
119 warning "should be in a" 119 warning "warning
120 warning " single line
121" 120"
122 warning "*This should be " 121 warning "#This should be "
123 warning "another warning 122 warning "another one
124" 123"
125]]) 124]])
126 125
@@ -896,24 +895,15 @@ do -- testing errors during GC
896 a[i] = T.newuserdata(i) -- creates several udata 895 a[i] = T.newuserdata(i) -- creates several udata
897 end 896 end
898 for i=1,20,2 do -- mark half of them to raise errors during GC 897 for i=1,20,2 do -- mark half of them to raise errors during GC
899 debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) 898 debug.setmetatable(a[i],
899 {__gc = function (x) error("@expected error in gc") end})
900 end 900 end
901 for i=2,20,2 do -- mark the other half to count and to create more garbage 901 for i=2,20,2 do -- mark the other half to count and to create more garbage
902 debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) 902 debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
903 end 903 end
904 a = nil
904 _G.A = 0 905 _G.A = 0
905 a = 0 906 collectgarbage()
906 while 1 do
907 local stat, msg = pcall(collectgarbage)
908 if stat then
909 break -- stop when no more errors
910 else
911 a = a + 1
912 assert(string.find(msg, "__gc"))
913 end
914 end
915 assert(a == 10) -- number of errors
916
917 assert(A == 10) -- number of normal collections 907 assert(A == 10) -- number of normal collections
918 collectgarbage("restart") 908 collectgarbage("restart")
919end 909end
diff --git a/testes/gc.lua b/testes/gc.lua
index 8b9179c8..84e8ffb7 100644
--- a/testes/gc.lua
+++ b/testes/gc.lua
@@ -353,40 +353,36 @@ GC()
353 353
354 354
355-- testing errors during GC 355-- testing errors during GC
356do 356if T then
357collectgarbage("stop") -- stop collection 357 collectgarbage("stop") -- stop collection
358local u = {} 358 local u = {}
359local s = {}; setmetatable(s, {__mode = 'k'}) 359 local s = {}; setmetatable(s, {__mode = 'k'})
360setmetatable(u, {__gc = function (o) 360 setmetatable(u, {__gc = function (o)
361 local i = s[o] 361 local i = s[o]
362 s[i] = true 362 s[i] = true
363 assert(not s[i - 1]) -- check proper finalization order 363 assert(not s[i - 1]) -- check proper finalization order
364 if i == 8 then error("here") end -- error during GC 364 if i == 8 then error("@expected@") end -- error during GC
365end}) 365 end})
366 366
367for i = 6, 10 do 367 for i = 6, 10 do
368 local n = setmetatable({}, getmetatable(u)) 368 local n = setmetatable({}, getmetatable(u))
369 s[n] = i 369 s[n] = i
370end 370 end
371
372assert(not pcall(collectgarbage))
373for i = 8, 10 do assert(s[i]) end
374
375for i = 1, 5 do
376 local n = setmetatable({}, getmetatable(u))
377 s[n] = i
378end
379 371
380collectgarbage() 372 collectgarbage()
381for i = 1, 10 do assert(s[i]) end 373 assert(string.find(_WARN, "error in __gc metamethod"))
374 assert(string.match(_WARN, "@(.-)@") == "expected")
375 for i = 8, 10 do assert(s[i]) end
382 376
383getmetatable(u).__gc = false 377 for i = 1, 5 do
378 local n = setmetatable({}, getmetatable(u))
379 s[n] = i
380 end
384 381
382 collectgarbage()
383 for i = 1, 10 do assert(s[i]) end
385 384
386-- __gc errors with non-string messages 385 getmetatable(u).__gc = false
387setmetatable({}, {__gc = function () error{} end})
388local a, b = pcall(collectgarbage)
389assert(not a and type(b) == "string" and string.find(b, "error in __gc"))
390 386
391end 387end
392print '+' 388print '+'
@@ -478,9 +474,11 @@ end
478 474
479 475
480-- errors during collection 476-- errors during collection
481u = setmetatable({}, {__gc = function () error "!!!" end}) 477if T then
482u = nil 478 u = setmetatable({}, {__gc = function () error "@expected error" end})
483assert(not pcall(collectgarbage)) 479 u = nil
480 collectgarbage()
481end
484 482
485 483
486if not _soft then 484if not _soft then
@@ -645,11 +643,26 @@ do
645end 643end
646 644
647-- create several objects to raise errors when collected while closing state 645-- create several objects to raise errors when collected while closing state
648do 646if T then
649 local mt = {__gc = function (o) return o + 1 end} 647 local error, assert, warn, find = error, assert, warn, string.find
650 for i = 1,10 do 648 local n = 0
649 local lastmsg
650 local mt = {__gc = function (o)
651 n = n + 1
652 assert(n == o[1])
653 if n == 1 then
654 _WARN = nil
655 elseif n == 2 then
656 assert(find(_WARN, "@expected warning"))
657 lastmsg = _WARN -- get message from previous error (first 'o')
658 else
659 assert(lastmsg == _WARN) -- subsequent error messages are equal
660 end
661 error"@expected warning"
662 end}
663 for i = 10, 1, -1 do
651 -- create object and preserve it until the end 664 -- create object and preserve it until the end
652 table.insert(___Glob, setmetatable({}, mt)) 665 table.insert(___Glob, setmetatable({i}, mt))
653 end 666 end
654end 667end
655 668