diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-01-01 12:14:56 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-01-01 12:14:56 -0200 |
| commit | c6f7181e910b6b2ff1346b5486a31be87b1da5af (patch) | |
| tree | 92cc716487c83ecd9860444f23fd55ef65358cbb /testes | |
| parent | 437a5b07d415e1a74160ddfd804017171d6cc5cb (diff) | |
| download | lua-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.lua | 11 | ||||
| -rw-r--r-- | testes/api.lua | 28 | ||||
| -rw-r--r-- | testes/gc.lua | 87 |
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() | |||
| 190 | dofile('files.lua') | 190 | dofile('files.lua') |
| 191 | 191 | ||
| 192 | if #msgs > 0 then | 192 | if #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") | ||
| 197 | end | 198 | end |
| 198 | 199 | ||
| 200 | print("(there should be two warnings now)") | ||
| 201 | warn("#This is "); warn("an expected"); warn(" warning\n") | ||
| 202 | warn("#This is"); warn(" another one\n") | ||
| 203 | |||
| 199 | -- no test module should define 'debug' | 204 | -- no test module should define 'debug' |
| 200 | assert(debug == nil) | 205 | assert(debug == nil) |
| 201 | 206 | ||
| @@ -219,10 +224,6 @@ local _G, showmem, print, format, clock, time, difftime, assert, open = | |||
| 219 | local fname = T and "time-debug.txt" or "time.txt" | 224 | local fname = T and "time-debug.txt" or "time.txt" |
| 220 | local lasttime | 225 | local lasttime |
| 221 | 226 | ||
| 222 | |||
| 223 | warn("*This is "); warn("an expected"); warn(" warning\n") | ||
| 224 | warn("*This is"); warn(" another one\n") | ||
| 225 | |||
| 226 | if not usertests then | 227 | if 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 |
| 116 | T.testC([[ | 116 | T.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") |
| 919 | end | 909 | end |
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 |
| 356 | do | 356 | if T then |
| 357 | collectgarbage("stop") -- stop collection | 357 | collectgarbage("stop") -- stop collection |
| 358 | local u = {} | 358 | local u = {} |
| 359 | local s = {}; setmetatable(s, {__mode = 'k'}) | 359 | local s = {}; setmetatable(s, {__mode = 'k'}) |
| 360 | setmetatable(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 |
| 365 | end}) | 365 | end}) |
| 366 | 366 | ||
| 367 | for 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 |
| 370 | end | 370 | end |
| 371 | |||
| 372 | assert(not pcall(collectgarbage)) | ||
| 373 | for i = 8, 10 do assert(s[i]) end | ||
| 374 | |||
| 375 | for i = 1, 5 do | ||
| 376 | local n = setmetatable({}, getmetatable(u)) | ||
| 377 | s[n] = i | ||
| 378 | end | ||
| 379 | 371 | ||
| 380 | collectgarbage() | 372 | collectgarbage() |
| 381 | for 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 | ||
| 383 | getmetatable(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 |
| 387 | setmetatable({}, {__gc = function () error{} end}) | ||
| 388 | local a, b = pcall(collectgarbage) | ||
| 389 | assert(not a and type(b) == "string" and string.find(b, "error in __gc")) | ||
| 390 | 386 | ||
| 391 | end | 387 | end |
| 392 | print '+' | 388 | print '+' |
| @@ -478,9 +474,11 @@ end | |||
| 478 | 474 | ||
| 479 | 475 | ||
| 480 | -- errors during collection | 476 | -- errors during collection |
| 481 | u = setmetatable({}, {__gc = function () error "!!!" end}) | 477 | if T then |
| 482 | u = nil | 478 | u = setmetatable({}, {__gc = function () error "@expected error" end}) |
| 483 | assert(not pcall(collectgarbage)) | 479 | u = nil |
| 480 | collectgarbage() | ||
| 481 | end | ||
| 484 | 482 | ||
| 485 | 483 | ||
| 486 | if not _soft then | 484 | if not _soft then |
| @@ -645,11 +643,26 @@ do | |||
| 645 | end | 643 | end |
| 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 |
| 648 | do | 646 | if 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 |
| 654 | end | 667 | end |
| 655 | 668 | ||
