aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-28 18:01:55 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-28 18:01:55 +0200
commit92944be3c3718095efa38e2a8db94844b1c7f739 (patch)
treee050cc4e493f6bc69be6b00b9b0562295ba5b123 /tests
parent8d7791f3eb1c5fc449490845254b59fdde30e9e0 (diff)
downloadlanes-92944be3c3718095efa38e2a8db94844b1c7f739.tar.gz
lanes-92944be3c3718095efa38e2a8db94844b1c7f739.tar.bz2
lanes-92944be3c3718095efa38e2a8db94844b1c7f739.zip
New Lanes finalizer API lanes.finally()
Diffstat (limited to 'tests')
-rw-r--r--tests/basic.lua6
-rw-r--r--tests/finalizer.lua70
2 files changed, 46 insertions, 30 deletions
diff --git a/tests/basic.lua b/tests/basic.lua
index cfe6fd5..ab8a080 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -485,6 +485,6 @@ end
485 485
486local nameof_type, nameof_name = lanes.nameof(print) 486local nameof_type, nameof_name = lanes.nameof(print)
487PRINT("name of " .. nameof_type .. " print = '" .. nameof_name .. "'") 487PRINT("name of " .. nameof_type .. " print = '" .. nameof_name .. "'")
488 488-- install a finalizer that gets called upon Lanes's internal Universe is GCed.
489-- 489-- that way, we print our message after anything that can be output by lanes that are still running at that point
490io.stderr:write "Done! :)\n" 490lanes.finally(function() io.stderr:write "\n=======================================\nTEST OK\n" end)
diff --git a/tests/finalizer.lua b/tests/finalizer.lua
index 060e0a6..2acc39d 100644
--- a/tests/finalizer.lua
+++ b/tests/finalizer.lua
@@ -10,20 +10,22 @@
10 10
11local lanes = require "lanes" 11local lanes = require "lanes"
12lanes.configure{with_timers=false} 12lanes.configure{with_timers=false}
13local finally = lanes.finally
13 14
14local FN= "finalizer-test.tmp" 15local FN = "finalizer-test.tmp"
15 16
16local cleanup 17local cleanup
17 18
18local which= os.time() % 2 -- 0/1 19local function lane(error_)
19
20local function lane()
21
22 set_finalizer(cleanup) 20 set_finalizer(cleanup)
23 21
24 local f,err= io.open(FN,"w") 22 local st,err = pcall(finally, cleanup) -- should cause an error because called from a lane
23 assert(not st, "finally() should have thrown an error")
24 io.stderr:write("finally() raised error '", err, "'\n")
25
26 local f,err = io.open(FN,"w")
25 if not f then 27 if not f then
26 error( "Could not create "..FN..": "..err ) 28 error( "Could not create "..FN..": "..err)
27 end 29 end
28 30
29 f:write( "Test file that should get removed." ) 31 f:write( "Test file that should get removed." )
@@ -32,11 +34,10 @@ local function lane()
32 -- don't forget to close the file immediately, else we won't be able to delete it until f is collected 34 -- don't forget to close the file immediately, else we won't be able to delete it until f is collected
33 f:close() 35 f:close()
34 36
35 if which==0 then 37 if error_ then
36 print "you loose" 38 io.stderr:write("Raising ", tostring(error_), "\n")
37 error("aa") -- exception here; the value needs NOT be a string 39 error(error_, 0) -- exception here; the value needs NOT be a string
38 end 40 end
39
40 -- no exception 41 -- no exception
41end 42end
42 43
@@ -44,8 +45,8 @@ end
44-- This is called at the end of the lane; whether succesful or not. 45-- This is called at the end of the lane; whether succesful or not.
45-- Gets the 'error()' parameter as parameter ('nil' if normal return). 46-- Gets the 'error()' parameter as parameter ('nil' if normal return).
46-- 47--
47cleanup= function(err) 48cleanup = function(err)
48 49 io.stderr:write "------------------------ In Worker Finalizer -----------------------\n"
49 -- An error in finalizer will override an error (or success) in the main 50 -- An error in finalizer will override an error (or success) in the main
50 -- chunk. 51 -- chunk.
51 -- 52 --
@@ -57,30 +58,45 @@ cleanup= function(err)
57 io.stderr:write( "Cleanup after normal return\n" ) 58 io.stderr:write( "Cleanup after normal return\n" )
58 end 59 end
59 60
60 local _,err2= os.remove(FN) 61 local _,err2 = os.remove(FN)
61 print( "file removal result: ", tostring( err2)) 62 io.stderr:write( "file removal result: ", tostring(err2), "\n")
62 assert(not err2) -- if this fails, it will be shown in the calling script 63 assert(not err2) -- if this fails, it will be shown in the calling script
63 -- as an error from the lane itself 64 -- as an error from the lane itself
64 65
65 io.stderr:write( "Removed file "..FN.."\n" ) 66 io.stderr:write( "Removed file "..FN.."\n" )
66end 67end
67 68
68local lgen = lanes.gen("*", lane) 69-- we need error_trace_level above "minimal" to get a stack trace out of h:join()
70local lgen = lanes.gen("*", {error_trace_level = "basic"}, lane)
71
72local do_test = function(error_)
69 73
70io.stderr:write "Launching the lane!\n" 74 io.stderr:write "======================== Launching the lane! =======================\n"
71 75
72local h= lgen() 76 local h = lgen(error_)
73 77
74local _,err,stack= h:join() -- wait for the lane (no automatic error propagation) 78 local _,err,stack = h:join() -- wait for the lane (no automatic error propagation)
75if err then 79 if err then
76 assert(stack) 80 assert(stack, "no stack trace on error, check 'error_trace_level'")
77 io.stderr:write( "Lane error: "..tostring(err).."\n" ) 81 io.stderr:write( "Lane error: "..tostring(err).."\n" )
78 io.stderr:write( "\t", table.concat(stack,"\t\n"), "\n" ) 82 io.stderr:write( "\t", table.concat(stack,"\t\n"), "\n" )
83 end
79end 84end
80 85
81local f= io.open(FN,"r") 86do_test(nil)
82if f then 87do_test("An error")
83 error( "CLEANUP DID NOT WORK: "..FN.." still exists!" ) 88
89local on_exit = function()
90 finally(nil)
91 io.stderr:write "=========================In Lanes Finalizer! =======================\n"
92 local f = io.open(FN,"r")
93 if f then
94 error( "CLEANUP DID NOT WORK: "..FN.." still exists!" )
95 else
96 io.stderr:write(FN .. " was successfully removed\n")
97 end
98 io.stderr:write "Finished!\n"
84end 99end
85 100
86io.stderr:write "Finished!\n" 101-- this function is called after script exit, when the state is closed
102lanes.finally(on_exit)