From b4b455139a811af1d176c300b8ebe0e84cf29412 Mon Sep 17 00:00:00 2001
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Date: Wed, 20 Mar 2019 18:09:22 +0300
Subject: [PATCH] box: allow box.session{.exists, .fd} without args

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
---
 src/box/lua/session.c         | 19 +++++++++++++------
 test/box-tap/session.test.lua |  6 +++---
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/box/lua/session.c b/src/box/lua/session.c
index a4ddb201ca..c0ac4917b3 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 857bc643b0..5d49655331 100755
--- a/test/box-tap/session.test.lua
+++ b/test/box-tap/session.test.lua
@@ -15,15 +15,14 @@ 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
 ---
 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(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")
@@ -169,6 +168,7 @@ conn = net.box.connect(uri)
 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")
-- 
GitLab