diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/basic.lua | 6 | ||||
-rw-r--r-- | tests/finalizer.lua | 70 |
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 | ||
486 | local nameof_type, nameof_name = lanes.nameof(print) | 486 | local nameof_type, nameof_name = lanes.nameof(print) |
487 | PRINT("name of " .. nameof_type .. " print = '" .. nameof_name .. "'") | 487 | PRINT("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 |
490 | io.stderr:write "Done! :)\n" | 490 | lanes.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 | ||
11 | local lanes = require "lanes" | 11 | local lanes = require "lanes" |
12 | lanes.configure{with_timers=false} | 12 | lanes.configure{with_timers=false} |
13 | local finally = lanes.finally | ||
13 | 14 | ||
14 | local FN= "finalizer-test.tmp" | 15 | local FN = "finalizer-test.tmp" |
15 | 16 | ||
16 | local cleanup | 17 | local cleanup |
17 | 18 | ||
18 | local which= os.time() % 2 -- 0/1 | 19 | local function lane(error_) |
19 | |||
20 | local 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 |
41 | end | 42 | end |
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 | -- |
47 | cleanup= function(err) | 48 | cleanup = 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" ) |
66 | end | 67 | end |
67 | 68 | ||
68 | local lgen = lanes.gen("*", lane) | 69 | -- we need error_trace_level above "minimal" to get a stack trace out of h:join() |
70 | local lgen = lanes.gen("*", {error_trace_level = "basic"}, lane) | ||
71 | |||
72 | local do_test = function(error_) | ||
69 | 73 | ||
70 | io.stderr:write "Launching the lane!\n" | 74 | io.stderr:write "======================== Launching the lane! =======================\n" |
71 | 75 | ||
72 | local h= lgen() | 76 | local h = lgen(error_) |
73 | 77 | ||
74 | local _,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) |
75 | if 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 | ||
79 | end | 84 | end |
80 | 85 | ||
81 | local f= io.open(FN,"r") | 86 | do_test(nil) |
82 | if f then | 87 | do_test("An error") |
83 | error( "CLEANUP DID NOT WORK: "..FN.." still exists!" ) | 88 | |
89 | local 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" | ||
84 | end | 99 | end |
85 | 100 | ||
86 | io.stderr:write "Finished!\n" | 101 | -- this function is called after script exit, when the state is closed |
102 | lanes.finally(on_exit) | ||