From fb713cdedb16f8eb67523f17b0d66262b4e9dca1 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Mon, 25 Jan 2016 15:14:34 +0300 Subject: Add more tests for socket.try/protect --- test/excepttest.lua | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'test/excepttest.lua') diff --git a/test/excepttest.lua b/test/excepttest.lua index ce9f197..6904545 100644 --- a/test/excepttest.lua +++ b/test/excepttest.lua @@ -1,6 +1,31 @@ local socket = require("socket") -try = socket.newtry(function() - print("finalized!!!") + +local finalizer_called + +local func = socket.protect(function(err, ...) + local try = socket.newtry(function() + finalizer_called = true + error("ignored") + end) + + if err then + return error(err, 0) + else + return try(...) + end end) -try = socket.protect(try) -print(try(nil, "it works")) + +local ret1, ret2, ret3 = func(false, 1, 2, 3) +assert(not finalizer_called, "unexpected finalizer call") +assert(ret1 == 1 and ret2 == 2 and ret3 == 3, "incorrect return values") + +ret1, ret2, ret3 = func(false, false, "error message") +assert(finalizer_called, "finalizer not called") +assert(ret1 == nil and ret2 == "error message" and ret3 == nil, "incorrect return values") + +local err = {key = "value"} +ret1, ret2 = pcall(func, err) +assert(not ret1, "error not rethrown") +assert(ret2 == err, "incorrect error rethrown") + +print("OK") -- cgit v1.2.3-55-g6feb From 9fe38c654f6c62a4f11d993bd79c710af9313fbd Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Wed, 24 Feb 2016 00:48:43 +0100 Subject: Don't swallow errors in finalizers. --- doc/socket.html | 3 +-- src/except.c | 2 +- test/excepttest.lua | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) (limited to 'test/excepttest.lua') diff --git a/doc/socket.html b/doc/socket.html index a43a208..08ef784 100644 --- a/doc/socket.html +++ b/doc/socket.html @@ -167,8 +167,7 @@ is raised.

Finalizer is a function that will be called before -try throws the exception. It will be called -in protected mode. +try throws the exception.

diff --git a/src/except.c b/src/except.c index def35a0..309f8f0 100644 --- a/src/except.c +++ b/src/except.c @@ -49,7 +49,7 @@ static void wrap(lua_State *L) { static int finalize(lua_State *L) { if (!lua_toboolean(L, 1)) { lua_pushvalue(L, lua_upvalueindex(1)); - lua_pcall(L, 0, 0, 0); + lua_call(L, 0, 0); lua_settop(L, 2); wrap(L); lua_error(L); diff --git a/test/excepttest.lua b/test/excepttest.lua index 6904545..80c9cb8 100644 --- a/test/excepttest.lua +++ b/test/excepttest.lua @@ -5,7 +5,6 @@ local finalizer_called local func = socket.protect(function(err, ...) local try = socket.newtry(function() finalizer_called = true - error("ignored") end) if err then -- cgit v1.2.3-55-g6feb