[Tarantool-patches] [PATCH v21 6/6] test: add box.lib test
Cyrill Gorcunov
gorcunov at gmail.com
Mon Apr 12 01:36:49 MSK 2021
On Sun, Apr 11, 2021 at 05:43:02PM +0200, Vladislav Shpilevoy wrote:
> Thanks for the patch!
>
> See below 4 comments, my diff in the end of the email, and on
> the branch in a separate commit.
Vlad, here is a diff on top. Please take a look. I didn't push
out anything yet, because need to squash them first.
---
Subject: [PATCH] merged test
Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
test/box/lib.result | 87 ++++++++++++++++++++++++++++++++++++-------
test/box/lib.test.lua | 49 ++++++++++++++++++------
2 files changed, 110 insertions(+), 26 deletions(-)
diff --git a/test/box/lib.result b/test/box/lib.result
index 58acb9c06..5067e9b36 100644
--- a/test/box/lib.result
+++ b/test/box/lib.result
@@ -356,10 +356,19 @@ box.func['cfunc.cfunc_add']:call({1, 2})
| - 3
| ...
+-- Now we have 2 refs for low level module (when function
+-- is loaded for first time from box.schema.func it takes
+-- two refs: one for module itself and one for for the function,
+-- this is because there is no explicit "load" procedure for
+-- box.schema.func, next loads from the same module via box.schema.func
+-- interface grabs the module from cache and add only one reference).
+--
+-- So we have 2 refs for box.schema.func and one for box.lib
+-- interface which grabs the same module.
old_module = box.lib.load('cfunc')
| ---
| ...
-assert(old_module['debug_refs'] == 3) -- box.lib + 2 box.schema.func
+assert(old_module['debug_refs'] == 3)
| ---
| - true
| ...
@@ -392,11 +401,12 @@ box.func['cfunc.cfunc_add']:call({1, 2})
| - 13
| ...
--- The box.lib instance should carry own
--- references to the module and old
--- function. And reloading must not
--- affect old functions. Thus one for
--- box.lib and one for box.lib function.
+-- The box.lib instance should carry own references to
+-- the module and old function. And reloading must not
+-- affect old functions. Thus one for box.lib _module_ and
+-- one for box.lib _function_. The reloaded box.schema.func
+-- will carry own two references for reloaded module and
+-- bound function.
assert(old_module['debug_refs'] == 2)
| ---
| - true
@@ -414,10 +424,9 @@ old_module:unload()
| - true
| ...
--- Same time the reload should update
--- low level module cache, thus two
--- for box and box function plus one
--- new box.lib.
+-- Same time the reload should update low level module cache,
+-- thus we load a new instance from updated cache entry which
+-- has 2 references already and thus we add one more reference.
new_module = box.lib.load('cfunc')
| ---
| ...
@@ -432,10 +441,6 @@ box.func['cfunc.cfunc_add']:call({1, 2})
| - 13
| ...
--- Cleanup.
-_ = pcall(fio.unlink(cfunc_path))
- | ---
- | ...
new_module:unload()
| ---
| - true
@@ -443,3 +448,57 @@ new_module:unload()
box.schema.func.drop('cfunc.cfunc_add')
| ---
| ...
+
+-- Now lets try to figure out if __gc works as expected.
+module1 = box.lib.load('cfunc')
+ | ---
+ | ...
+module2 = box.lib.load('cfunc')
+ | ---
+ | ...
+assert(module1['debug_module_ptr'] == module2['debug_module_ptr'])
+ | ---
+ | - true
+ | ...
+assert(module1['debug_refs'] == 2)
+ | ---
+ | - true
+ | ...
+cfunc_add = module2:load('cfunc_add')
+ | ---
+ | ...
+assert(module1['debug_refs'] == 3)
+ | ---
+ | - true
+ | ...
+module2 = nil
+ | ---
+ | ...
+collectgarbage('collect')
+ | ---
+ | - 0
+ | ...
+assert(module1['debug_refs'] == 2)
+ | ---
+ | - true
+ | ...
+cfunc_add = nil
+ | ---
+ | ...
+collectgarbage('collect')
+ | ---
+ | - 0
+ | ...
+assert(module1['debug_refs'] == 1)
+ | ---
+ | - true
+ | ...
+module1:unload()
+ | ---
+ | - true
+ | ...
+
+-- Cleanup.
+_ = pcall(fio.unlink(cfunc_path))
+ | ---
+ | ...
diff --git a/test/box/lib.test.lua b/test/box/lib.test.lua
index e0e4b9e4d..93cea90b6 100644
--- a/test/box/lib.test.lua
+++ b/test/box/lib.test.lua
@@ -139,8 +139,17 @@ box.schema.func.create('cfunc.cfunc_add', {language = "C"})
box.schema.user.grant('guest', 'execute', 'function', 'cfunc.cfunc_add')
box.func['cfunc.cfunc_add']:call({1, 2})
+-- Now we have 2 refs for low level module (when function
+-- is loaded for first time from box.schema.func it takes
+-- two refs: one for module itself and one for for the function,
+-- this is because there is no explicit "load" procedure for
+-- box.schema.func, next loads from the same module via box.schema.func
+-- interface grabs the module from cache and add only one reference).
+--
+-- So we have 2 refs for box.schema.func and one for box.lib
+-- interface which grabs the same module.
old_module = box.lib.load('cfunc')
-assert(old_module['debug_refs'] == 3) -- box.lib + 2 box.schema.func
+assert(old_module['debug_refs'] == 3)
old_func = old_module:load('cfunc_add')
assert(old_module['debug_refs'] == 4) -- plus function instance
old_func(1, 2)
@@ -152,27 +161,43 @@ fio.symlink(cfunc4_path, cfunc_path)
box.schema.func.reload("cfunc")
box.func['cfunc.cfunc_add']:call({1, 2})
--- The box.lib instance should carry own
--- references to the module and old
--- function. And reloading must not
--- affect old functions. Thus one for
--- box.lib and one for box.lib function.
+-- The box.lib instance should carry own references to
+-- the module and old function. And reloading must not
+-- affect old functions. Thus one for box.lib _module_ and
+-- one for box.lib _function_. The reloaded box.schema.func
+-- will carry own two references for reloaded module and
+-- bound function.
assert(old_module['debug_refs'] == 2)
old_func(1, 2)
old_func:unload()
old_module:unload()
--- Same time the reload should update
--- low level module cache, thus two
--- for box and box function plus one
--- new box.lib.
+-- Same time the reload should update low level module cache,
+-- thus we load a new instance from updated cache entry which
+-- has 2 references already and thus we add one more reference.
new_module = box.lib.load('cfunc')
assert(new_module['debug_refs'] == 3)
-- Box function should carry own module.
box.func['cfunc.cfunc_add']:call({1, 2})
--- Cleanup.
-_ = pcall(fio.unlink(cfunc_path))
new_module:unload()
box.schema.func.drop('cfunc.cfunc_add')
+
+-- Now lets try to figure out if __gc works as expected.
+module1 = box.lib.load('cfunc')
+module2 = box.lib.load('cfunc')
+assert(module1['debug_module_ptr'] == module2['debug_module_ptr'])
+assert(module1['debug_refs'] == 2)
+cfunc_add = module2:load('cfunc_add')
+assert(module1['debug_refs'] == 3)
+module2 = nil
+collectgarbage('collect')
+assert(module1['debug_refs'] == 2)
+cfunc_add = nil
+collectgarbage('collect')
+assert(module1['debug_refs'] == 1)
+module1:unload()
+
+-- Cleanup.
+_ = pcall(fio.unlink(cfunc_path))
--
2.30.2
More information about the Tarantool-patches
mailing list