Tarantool development patches archive
 help / color / mirror / Atom feed
From: Cyrill Gorcunov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tml <tarantool-patches@dev.tarantool.org>
Subject: Re: [Tarantool-patches] [PATCH v21 6/6] test: add box.lib test
Date: Mon, 12 Apr 2021 01:36:49 +0300	[thread overview]
Message-ID: <YHN6AbWsDLApYqdm@grain> (raw)
In-Reply-To: <edaea916-5afa-9a5a-7b65-30df8859c493@tarantool.org>

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@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


  parent reply	other threads:[~2021-04-11 22:36 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-08 16:41 [Tarantool-patches] [PATCH v21 0/6] box: implement box.lib Lua module Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 1/6] box/func: fix modules functions restore Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:31   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 15:02     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 2/6] box/func: module_reload -- drop redundant argument Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 3/6] box/module_cache: introduce modules subsystem Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:54   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 14:59     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 4/6] box/schema.func: switch to new module api Cyrill Gorcunov via Tarantool-patches
2021-04-09 23:55   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-10 15:00     ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 5/6] box: implement box.lib module Cyrill Gorcunov via Tarantool-patches
2021-04-11 15:38   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-11 22:38     ` Cyrill Gorcunov via Tarantool-patches
2021-04-12 22:08       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-12 22:34         ` Cyrill Gorcunov via Tarantool-patches
2021-04-08 16:41 ` [Tarantool-patches] [PATCH v21 6/6] test: add box.lib test Cyrill Gorcunov via Tarantool-patches
2021-04-08 18:53   ` Cyrill Gorcunov via Tarantool-patches
2021-04-11 15:43     ` Vladislav Shpilevoy via Tarantool-patches
2021-04-11 21:56       ` Cyrill Gorcunov via Tarantool-patches
2021-04-11 22:36       ` Cyrill Gorcunov via Tarantool-patches [this message]
2021-04-12 22:08         ` Vladislav Shpilevoy via Tarantool-patches
2021-04-13  7:10           ` Cyrill Gorcunov via Tarantool-patches
2021-04-13 21:53 ` [Tarantool-patches] [PATCH v21 0/6] box: implement box.lib Lua module Vladislav Shpilevoy via Tarantool-patches
2021-04-14  8:07 ` Kirill Yukhin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YHN6AbWsDLApYqdm@grain \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=gorcunov@gmail.com \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v21 6/6] test: add box.lib test' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox