[Tarantool-patches] [PATCH v4 6/6] test: box/cfunc -- add simple module test

Cyrill Gorcunov gorcunov at gmail.com
Thu Oct 1 16:51:13 MSK 2020


Note that the test is disabled for a while since we
need to update test-run first like

 | diff --git a/pretest_clean.lua b/pretest_clean.lua
 | index 9b5ac9d..b0280c4 100644
 | --- a/pretest_clean.lua
 | +++ b/pretest_clean.lua
 | @@ -272,6 +272,7 @@ local function clean()
 |          package = true,
 |          pickle = true,
 |          popen = true,
 | +        cfunc = true,
 |          pwd = true,
 |          socket = true,
 |          strict = true,

ie need to enable cfunc module.

Part-of #4642

Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 test/box/CMakeLists.txt |   2 +
 test/box/cfunc.result   | 116 ++++++++++++++++++++++++++++++++++++++++
 test/box/cfunc.test.lua |  56 +++++++++++++++++++
 test/box/cfunc1.c       |  46 ++++++++++++++++
 test/box/cfunc2.c       |  46 ++++++++++++++++
 test/box/suite.ini      |   2 +-
 6 files changed, 267 insertions(+), 1 deletion(-)
 create mode 100644 test/box/cfunc.result
 create mode 100644 test/box/cfunc.test.lua
 create mode 100644 test/box/cfunc1.c
 create mode 100644 test/box/cfunc2.c

diff --git a/test/box/CMakeLists.txt b/test/box/CMakeLists.txt
index 06bfbbe9d..2afbeadc3 100644
--- a/test/box/CMakeLists.txt
+++ b/test/box/CMakeLists.txt
@@ -3,3 +3,5 @@ build_module(function1 function1.c)
 build_module(reload1 reload1.c)
 build_module(reload2 reload2.c)
 build_module(tuple_bench tuple_bench.c)
+build_module(cfunc1 cfunc1.c)
+build_module(cfunc2 cfunc2.c)
diff --git a/test/box/cfunc.result b/test/box/cfunc.result
new file mode 100644
index 000000000..beb07d775
--- /dev/null
+++ b/test/box/cfunc.result
@@ -0,0 +1,116 @@
+-- test-run result file version 2
+build_path = os.getenv("BUILDDIR")
+ | ---
+ | ...
+package.cpath = build_path..'/test/box/?.so;'..build_path..'/test/box/?.dylib;'..package.cpath
+ | ---
+ | ...
+
+cfunc = require('cfunc')
+ | ---
+ | ...
+fio = require('fio')
+ | ---
+ | ...
+
+cfunc_path = fio.pathjoin(build_path, "test/box/cfunc.so")
+ | ---
+ | ...
+cfunc1_path = fio.pathjoin(build_path, "test/box/cfunc1.so")
+ | ---
+ | ...
+cfunc2_path = fio.pathjoin(build_path, "test/box/cfunc2.so")
+ | ---
+ | ...
+
+_ = pcall(fio.unlink(cfunc_path))
+ | ---
+ | ...
+fio.symlink(cfunc1_path, cfunc_path)
+ | ---
+ | - true
+ | ...
+
+--
+-- Both of them are sitting in cfunc.so
+cfunc.create('cfunc.cfunc_sole_call')
+ | ---
+ | ...
+cfunc.create('cfunc.cfunc_fetch_array')
+ | ---
+ | ...
+
+--
+-- Make sure they are registered fine
+cfunc.exists('cfunc.cfunc_sole_call')
+ | ---
+ | - true
+ | ...
+cfunc.exists('cfunc.cfunc_fetch_array')
+ | ---
+ | - true
+ | ...
+
+--
+-- And they are listed
+cfunc.list()
+ | ---
+ | - - cfunc.cfunc_sole_call
+ |   - cfunc.cfunc_fetch_array
+ | ...
+
+--
+-- Make sure they all collable
+cfunc.call('cfunc.cfunc_sole_call')
+ | ---
+ | ...
+cfunc.call('cfunc.cfunc_fetch_array', {1,2,3,4})
+ | ---
+ | ...
+-- This one should issue an error
+cfunc.call('cfunc.cfunc_fetch_array', {1,2,4})
+ | ---
+ | - error: invalid argument 4 != 3
+ | ...
+
+--
+-- Clean old function references and reload new one.
+_ = pcall(fio.unlink(cfunc_path))
+ | ---
+ | ...
+fio.symlink(cfunc2_path, cfunc_path)
+ | ---
+ | - true
+ | ...
+
+cfunc.reload('cfunc.cfunc_fetch_array')
+ | ---
+ | ...
+
+cfunc.call('cfunc.cfunc_sole_call')
+ | ---
+ | ...
+cfunc.call('cfunc.cfunc_fetch_array', {2,4,6})
+ | ---
+ | ...
+
+--
+-- Clean it up
+cfunc.drop('cfunc.cfunc_sole_call')
+ | ---
+ | ...
+cfunc.drop('cfunc.cfunc_fetch_array')
+ | ---
+ | ...
+
+--
+-- List should be empty
+cfunc.list()
+ | ---
+ | ...
+
+--
+-- Cleanup the generated symlink
+_ = pcall(fio.unlink(cfunc_path))
+ | ---
+ | ...
diff --git a/test/box/cfunc.test.lua b/test/box/cfunc.test.lua
new file mode 100644
index 000000000..01e5fbebb
--- /dev/null
+++ b/test/box/cfunc.test.lua
@@ -0,0 +1,56 @@
+build_path = os.getenv("BUILDDIR")
+package.cpath = build_path..'/test/box/?.so;'..build_path..'/test/box/?.dylib;'..package.cpath
+
+cfunc = require('cfunc')
+fio = require('fio')
+
+cfunc_path = fio.pathjoin(build_path, "test/box/cfunc.so")
+cfunc1_path = fio.pathjoin(build_path, "test/box/cfunc1.so")
+cfunc2_path = fio.pathjoin(build_path, "test/box/cfunc2.so")
+
+_ = pcall(fio.unlink(cfunc_path))
+fio.symlink(cfunc1_path, cfunc_path)
+
+--
+-- Both of them are sitting in cfunc.so
+cfunc.create('cfunc.cfunc_sole_call')
+cfunc.create('cfunc.cfunc_fetch_array')
+
+--
+-- Make sure they are registered fine
+cfunc.exists('cfunc.cfunc_sole_call')
+cfunc.exists('cfunc.cfunc_fetch_array')
+
+--
+-- And they are listed
+cfunc.list()
+
+--
+-- Make sure they all collable
+cfunc.call('cfunc.cfunc_sole_call')
+cfunc.call('cfunc.cfunc_fetch_array', {1,2,3,4})
+-- This one should issue an error
+cfunc.call('cfunc.cfunc_fetch_array', {1,2,4})
+
+--
+-- Clean old function references and reload new one.
+_ = pcall(fio.unlink(cfunc_path))
+fio.symlink(cfunc2_path, cfunc_path)
+
+cfunc.reload('cfunc.cfunc_fetch_array')
+
+cfunc.call('cfunc.cfunc_sole_call')
+cfunc.call('cfunc.cfunc_fetch_array', {2,4,6})
+
+--
+-- Clean it up
+cfunc.drop('cfunc.cfunc_sole_call')
+cfunc.drop('cfunc.cfunc_fetch_array')
+
+--
+-- List should be empty
+cfunc.list()
+
+--
+-- Cleanup the generated symlink
+_ = pcall(fio.unlink(cfunc_path))
diff --git a/test/box/cfunc1.c b/test/box/cfunc1.c
new file mode 100644
index 000000000..4334f28da
--- /dev/null
+++ b/test/box/cfunc1.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <msgpuck.h>
+
+#include "module.h"
+
+/*
+ * Just make sure we've been called.
+ */
+int
+cfunc_sole_call(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	say_info("-- cfunc_sole_call -  called --");
+	printf("ok - cfunc_sole_call\n");
+	return 0;
+}
+
+/*
+ * Fetch N-array
+ */
+int
+cfunc_fetch_array(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	int arg_count = mp_decode_array(&args);
+	int field_count = mp_decode_array(&args);
+
+	(void)arg_count;
+	(void)field_count;
+
+	say_info("-- cfunc_fetch_array -  called --");
+
+	for (int i = 0; i < field_count; i++) {
+		int val = mp_decode_uint(&args);
+		int needed = (i+1);
+		if (val != needed) {
+			char res[128];
+			snprintf(res, sizeof(res), "%s %d != %d",
+				 "invalid argument", val, needed);
+			return box_error_set(__FILE__, __LINE__,
+					     ER_PROC_C, "%s", res);
+		}
+	}
+
+	printf("ok - cfunc_fetch_array\n");
+	return 0;
+}
diff --git a/test/box/cfunc2.c b/test/box/cfunc2.c
new file mode 100644
index 000000000..30ac48010
--- /dev/null
+++ b/test/box/cfunc2.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <msgpuck.h>
+
+#include "module.h"
+
+/*
+ * Just make sure we've been called.
+ */
+int
+cfunc_sole_call(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	say_info("-- cfunc_sole_call -  called --");
+	printf("ok - cfunc_sole_call\n");
+	return 0;
+}
+
+/*
+ * Fetch N-array
+ */
+int
+cfunc_fetch_array(box_function_ctx_t *ctx, const char *args, const char *args_end)
+{
+	int arg_count = mp_decode_array(&args);
+	int field_count = mp_decode_array(&args);
+
+	(void)arg_count;
+	(void)field_count;
+
+	say_info("-- cfunc_fetch_array -  called --");
+
+	for (int i = 0; i < field_count; i++) {
+		int val = mp_decode_uint(&args);
+		int needed = 2 * (i+1);
+		if (val != needed) {
+			char res[128];
+			snprintf(res, sizeof(res), "%s %d != %d",
+				 "invalid argument", val, needed);
+			return box_error_set(__FILE__, __LINE__,
+					     ER_PROC_C, "%s", res);
+		}
+	}
+
+	printf("ok - cfunc_fetch_array\n");
+	return 0;
+}
diff --git a/test/box/suite.ini b/test/box/suite.ini
index 793b8bf76..54130f43f 100644
--- a/test/box/suite.ini
+++ b/test/box/suite.ini
@@ -2,7 +2,7 @@
 core = tarantool
 description = Database tests
 script = box.lua
-disabled = rtree_errinj.test.lua tuple_bench.test.lua
+disabled = rtree_errinj.test.lua tuple_bench.test.lua cfunc.test.lua
 long_run = huge_field_map_long.test.lua
 config = engine.cfg
 release_disabled = errinj.test.lua errinj_index.test.lua rtree_errinj.test.lua upsert_errinj.test.lua iproto_stress.test.lua gh-4648-func-load-unload.test.lua
-- 
2.26.2



More information about the Tarantool-patches mailing list