Tarantool development patches archive
 help / color / mirror / Atom feed
* [PATCH v1 1/1] box: allow box.session{.exists, .fd} without args
@ 2019-03-20 15:18 Kirill Shcherbatov
  2019-03-26 16:14 ` Vladimir Davydov
  0 siblings, 1 reply; 2+ messages in thread
From: Kirill Shcherbatov @ 2019-03-20 15:18 UTC (permalink / raw)
  To: tarantool-patches, vdavydov.dev; +Cc: Kirill Shcherbatov

Allow to call box.session.exists() and box.session.fd() without
any arguments. In such case, current session is used.
The box.session.peer() already support such feature, so we need
it to be consistent.

Closes #4021

http://github.com/tarantool/tarantool/tree/kshch/gh-4021-optional-session-id
https://github.com/tarantool/tarantool/issues/4021
---
 src/box/lua/session.c         | 19 +++++++++++++------
 test/box-tap/session.test.lua |  5 +++--
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/box/lua/session.c b/src/box/lua/session.c
index a4ddb201c..c0ac4917b 100644
--- a/src/box/lua/session.c
+++ b/src/box/lua/session.c
@@ -214,11 +214,15 @@ lbox_session_su(struct lua_State *L)
 static int
 lbox_session_exists(struct lua_State *L)
 {
-	if (lua_gettop(L) != 1)
+	if (lua_gettop(L) > 1)
 		luaL_error(L, "session.exists(sid): bad arguments");
 
-	uint64_t sid = luaL_checkint64(L, -1);
-	lua_pushboolean(L, session_find(sid) != NULL);
+	struct session *session;
+	if (lua_gettop(L) == 1)
+		session = session_find(luaL_checkint64(L, -1));
+	else
+		session = current_session();
+	lua_pushboolean(L, session != NULL);
 	return 1;
 }
 
@@ -228,11 +232,14 @@ lbox_session_exists(struct lua_State *L)
 static int
 lbox_session_fd(struct lua_State *L)
 {
-	if (lua_gettop(L) != 1)
+	if (lua_gettop(L) > 1)
 		luaL_error(L, "session.fd(sid): bad arguments");
 
-	uint64_t sid = luaL_checkint64(L, -1);
-	struct session *session = session_find(sid);
+	struct session *session;
+	if (lua_gettop(L) == 1)
+		session = session_find(luaL_checkint64(L, -1));
+	else
+		session = current_session();
 	if (session == NULL)
 		luaL_error(L, "session.fd(): session does not exist");
 	lua_pushinteger(L, session_fd(session));
diff --git a/test/box-tap/session.test.lua b/test/box-tap/session.test.lua
index 857bc643b..b6b08d209 100755
--- a/test/box-tap/session.test.lua
+++ b/test/box-tap/session.test.lua
@@ -15,7 +15,7 @@ session = box.session
 space = box.schema.space.create('tweedledum')
 index = space:create_index('primary', { type = 'hash' })
 
-test:plan(55)
+test:plan(56)
 
 ---
 --- Check that Tarantool creates ADMIN session for #! script
@@ -23,7 +23,7 @@ test:plan(55)
 test:ok(session.exists(session.id()), "session is created")
 test:isnil(session.peer(session.id()), "session.peer")
 local ok, err = pcall(session.exists)
-test:is(err, "session.exists(sid): bad arguments", "exists bad args #1")
+test:ok(ok, "session.exists()")
 ok, err = pcall(session.exists, 1, 2, 3)
 test:is(err, "session.exists(sid): bad arguments", "exists bad args #2")
 test:ok(not session.exists(1234567890), "session doesn't exist")
@@ -166,6 +166,7 @@ test:is(
 )
 local uri = inspector:eval('session', 'box.cfg.listen')[1]
 conn = net.box.connect(uri)
+test:ok(conn:eval("return box.session.fd()"), "get session fd")
 test:ok(conn:eval("return box.session.exists(box.session.id())"), "remote session exist check")
 test:isnt(conn:eval("return box.session.peer(box.session.id())"), nil, "remote session peer check")
 test:ok(conn:eval("return box.session.peer() == box.session.peer(box.session.id())"), "remote session peer check")
-- 
2.21.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v1 1/1] box: allow box.session{.exists, .fd} without args
  2019-03-20 15:18 [PATCH v1 1/1] box: allow box.session{.exists, .fd} without args Kirill Shcherbatov
@ 2019-03-26 16:14 ` Vladimir Davydov
  0 siblings, 0 replies; 2+ messages in thread
From: Vladimir Davydov @ 2019-03-26 16:14 UTC (permalink / raw)
  To: Kirill Shcherbatov; +Cc: tarantool-patches

On Wed, Mar 20, 2019 at 06:18:33PM +0300, Kirill Shcherbatov wrote:
> diff --git a/test/box-tap/session.test.lua b/test/box-tap/session.test.lua
> index 857bc643b..b6b08d209 100755
> --- a/test/box-tap/session.test.lua
> +++ b/test/box-tap/session.test.lua
> @@ -15,7 +15,7 @@ session = box.session
>  space = box.schema.space.create('tweedledum')
>  index = space:create_index('primary', { type = 'hash' })
>  
> -test:plan(55)
> +test:plan(56)
>  
>  ---
>  --- Check that Tarantool creates ADMIN session for #! script
> @@ -23,7 +23,7 @@ test:plan(55)
>  test:ok(session.exists(session.id()), "session is created")
>  test:isnil(session.peer(session.id()), "session.peer")
>  local ok, err = pcall(session.exists)

This patch renders this pcall() redundant

> -test:is(err, "session.exists(sid): bad arguments", "exists bad args #1")
> +test:ok(ok, "session.exists()")
>  ok, err = pcall(session.exists, 1, 2, 3)
>  test:is(err, "session.exists(sid): bad arguments", "exists bad args #2")
>  test:ok(not session.exists(1234567890), "session doesn't exist")
> @@ -166,6 +166,7 @@ test:is(
>  )
>  local uri = inspector:eval('session', 'box.cfg.listen')[1]
>  conn = net.box.connect(uri)
> +test:ok(conn:eval("return box.session.fd()"), "get session fd")

Would be more prudent to check that fd() == fd(id()).

>  test:ok(conn:eval("return box.session.exists(box.session.id())"), "remote session exist check")
>  test:isnt(conn:eval("return box.session.peer(box.session.id())"), nil, "remote session peer check")
>  test:ok(conn:eval("return box.session.peer() == box.session.peer(box.session.id())"), "remote session peer check")

Pushed to the master branch with the following changes:

diff --git a/test/box-tap/session.test.lua b/test/box-tap/session.test.lua
index b6b08d20..5d496553 100755
--- a/test/box-tap/session.test.lua
+++ b/test/box-tap/session.test.lua
@@ -22,8 +22,7 @@ test:plan(56)
 ---
 test:ok(session.exists(session.id()), "session is created")
 test:isnil(session.peer(session.id()), "session.peer")
-local ok, err = pcall(session.exists)
-test:ok(ok, "session.exists()")
+test:ok(session.exists(), "session.exists")
 ok, err = pcall(session.exists, 1, 2, 3)
 test:is(err, "session.exists(sid): bad arguments", "exists bad args #2")
 test:ok(not session.exists(1234567890), "session doesn't exist")
@@ -166,10 +165,10 @@ test:is(
 )
 local uri = inspector:eval('session', 'box.cfg.listen')[1]
 conn = net.box.connect(uri)
-test:ok(conn:eval("return box.session.fd()"), "get session fd")
 test:ok(conn:eval("return box.session.exists(box.session.id())"), "remote session exist check")
 test:isnt(conn:eval("return box.session.peer(box.session.id())"), nil, "remote session peer check")
 test:ok(conn:eval("return box.session.peer() == box.session.peer(box.session.id())"), "remote session peer check")
+test:ok(conn:eval("return box.session.fd() == box.session.fd(box.session.id())"), "remote session fd check")
 
 -- gh-2994 session uid vs session effective uid
 test:is(session.euid(), 1, "session.uid")

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-26 16:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20 15:18 [PATCH v1 1/1] box: allow box.session{.exists, .fd} without args Kirill Shcherbatov
2019-03-26 16:14 ` Vladimir Davydov

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