[Tarantool-patches] [PATCH 1/2] errinj: provide 'get' method in Lua

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Wed Nov 27 02:29:40 MSK 2019


Error injections are used to simulate an error. They are
represented as a flag, or a number, and are used in Lua tests. But
they don't have any feedback. That makes impossible to use the
injections to check that something has happened. Something very
needed to be checked, and impossible to check in a different way.

More certainly, the patch is motivated by a necessity to count
loaded dynamic libraries to ensure, that they are loaded and
unloaded when expected. This is impossible to do in a platform
independent way. But an error injection as a debug-only counter
would solve the problem.

Needed for #4648
---
 src/box/lua/error.cc     | 37 ++++++++++++++++++++-------
 test/box/errinj.result   | 55 ++++++++++++++++++++++++++++++++++++++++
 test/box/errinj.test.lua | 17 +++++++++++++
 3 files changed, 100 insertions(+), 9 deletions(-)

diff --git a/src/box/lua/error.cc b/src/box/lua/error.cc
index 230d51dec..fc53a40f4 100644
--- a/src/box/lua/error.cc
+++ b/src/box/lua/error.cc
@@ -184,26 +184,44 @@ lbox_errinj_set(struct lua_State *L)
 	return 1;
 }
 
-static inline int
-lbox_errinj_cb(struct errinj *e, void *cb_ctx)
+static int
+lbox_errinj_push_value(struct lua_State *L, const struct errinj *e)
 {
-	struct lua_State *L = (struct lua_State*)cb_ctx;
-	lua_pushstring(L, e->name);
-	lua_newtable(L);
-	lua_pushstring(L, "state");
 	switch (e->type) {
 	case ERRINJ_BOOL:
 		lua_pushboolean(L, e->bparam);
-		break;
+		return 1;
 	case ERRINJ_INT:
 		luaL_pushint64(L, e->iparam);
-		break;
+		return 1;
 	case ERRINJ_DOUBLE:
 		lua_pushnumber(L, e->dparam);
-		break;
+		return 1;
 	default:
 		unreachable();
+		return 0;
 	}
+}
+
+static int
+lbox_errinj_get(struct lua_State *L)
+{
+	char *name = (char*)luaL_checkstring(L, 1);
+	struct errinj *e = errinj_by_name(name);
+	if (e != NULL)
+		return lbox_errinj_push_value(L, e);
+	lua_pushfstring(L, "error: can't find error injection '%s'", name);
+	return 1;
+}
+
+static inline int
+lbox_errinj_cb(struct errinj *e, void *cb_ctx)
+{
+	struct lua_State *L = (struct lua_State*)cb_ctx;
+	lua_pushstring(L, e->name);
+	lua_newtable(L);
+	lua_pushstring(L, "state");
+	lbox_errinj_push_value(L, e);
 	lua_settable(L, -3);
 	lua_settable(L, -3);
 	return 0;
@@ -259,6 +277,7 @@ box_lua_error_init(struct lua_State *L) {
 	static const struct luaL_Reg errinjlib[] = {
 		{"info", lbox_errinj_info},
 		{"set", lbox_errinj_set},
+		{"get", lbox_errinj_get},
 		{NULL, NULL}
 	};
 	/* box.error.injection is not set by register_module */
diff --git a/test/box/errinj.result b/test/box/errinj.result
index a148346e8..40683730c 100644
--- a/test/box/errinj.result
+++ b/test/box/errinj.result
@@ -1724,3 +1724,58 @@ box.schema.user.drop('testg')
 box.space.testg:drop()
 ---
 ...
+--
+-- Errinj:get().
+--
+box.error.injection.get('bad name')
+---
+- 'error: can''t find error injection ''bad name'''
+...
+box.error.injection.set('ERRINJ_WAL_IO', true)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_WAL_IO')
+---
+- true
+...
+box.error.injection.set('ERRINJ_WAL_IO', false)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_WAL_IO')
+---
+- false
+...
+box.error.injection.set('ERRINJ_TUPLE_FORMAT_COUNT', 20)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_TUPLE_FORMAT_COUNT')
+---
+- 20
+...
+box.error.injection.set('ERRINJ_TUPLE_FORMAT_COUNT', -1)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_TUPLE_FORMAT_COUNT')
+---
+- -1
+...
+box.error.injection.set('ERRINJ_RELAY_TIMEOUT', 0.5)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_RELAY_TIMEOUT')
+---
+- 0.5
+...
+box.error.injection.set('ERRINJ_RELAY_TIMEOUT', 0)
+---
+- ok
+...
+box.error.injection.get('ERRINJ_RELAY_TIMEOUT')
+---
+- 0
+...
diff --git a/test/box/errinj.test.lua b/test/box/errinj.test.lua
index 31dd9665b..03c088677 100644
--- a/test/box/errinj.test.lua
+++ b/test/box/errinj.test.lua
@@ -603,3 +603,20 @@ box.session.su('admin')
 box.error.injection.set('ERRINJ_WAL_IO', false)
 box.schema.user.drop('testg')
 box.space.testg:drop()
+
+--
+-- Errinj:get().
+--
+box.error.injection.get('bad name')
+box.error.injection.set('ERRINJ_WAL_IO', true)
+box.error.injection.get('ERRINJ_WAL_IO')
+box.error.injection.set('ERRINJ_WAL_IO', false)
+box.error.injection.get('ERRINJ_WAL_IO')
+box.error.injection.set('ERRINJ_TUPLE_FORMAT_COUNT', 20)
+box.error.injection.get('ERRINJ_TUPLE_FORMAT_COUNT')
+box.error.injection.set('ERRINJ_TUPLE_FORMAT_COUNT', -1)
+box.error.injection.get('ERRINJ_TUPLE_FORMAT_COUNT')
+box.error.injection.set('ERRINJ_RELAY_TIMEOUT', 0.5)
+box.error.injection.get('ERRINJ_RELAY_TIMEOUT')
+box.error.injection.set('ERRINJ_RELAY_TIMEOUT', 0)
+box.error.injection.get('ERRINJ_RELAY_TIMEOUT')
-- 
2.21.0 (Apple Git-122.2)



More information about the Tarantool-patches mailing list