[PATCH v1 1/1] box: allow box.session{.exists, .fd} without args

Kirill Shcherbatov kshcherbatov at tarantool.org
Wed Mar 20 18:18:33 MSK 2019


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




More information about the Tarantool-patches mailing list