From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id CE514645E6; Mon, 18 Jan 2021 23:40:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CE514645E6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1611002407; bh=sLAsg3UrKVQn1kWWtYd20zsov1/Wq482s08D/xmYP6g=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=FyGaCrKw+K3AnfprDPjgcZ/RnSZhluI7HW7pOvk7BN6HO1BQZJmV5hlp+rGMOKLJK Cjr97Uqqb5OImLelJXf0cOnjxuPJbn3qlInORhQAPziAmq1+sXLWK80e0nc9cy23hE mR3sN2UIWY08m5NlBV5PbWiXlumiHSlYiOTH8BvU= Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 1E64585DEC for ; Mon, 18 Jan 2021 23:37:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1E64585DEC Received: by mail-lf1-f54.google.com with SMTP id o10so25879803lfl.13 for ; Mon, 18 Jan 2021 12:37:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sq/u25qKLTmAZo/FUFLY/ozeYIxgUSbcDEEG/rdk8cQ=; b=PvxKwNMX5/KTwmjoM5pKIMK7gAVzH+xWSsi27visccoZOIdNmN5P96ZaGhyuUrYGaW 8PcuvpTdD5yoH4CveZVRZKAuKt6KIgfb6KVN/YB8sQHPskualsp1hxfpHKjxs1RPK9Ja xvnzyxYBFiB8vpxFmMQ+061xLn8eURNwGgzuVbe9xNws5tuMmPOguZz3jKLwJhZRfSCi k5zss3nw66publ9xTyvHnLqWdEuRHOdX8vHWSTCDBnc6DLteFLmhbIyzVFWlDhxLNgXX tvvrlP+GG50lx7r+mVWAcBeKCrmPLxY4vERjnPhGJLMXEPioOqlrpcqLsaWSmOohSxLE zPZQ== X-Gm-Message-State: AOAM5316guCNZWgP1IL8nJb8Z9i2UdyLMp2DS0sAT2LXse4IQ5wuugUk eKwifpVTmQpeTe4tpa6rGSwOY/tmT+4= X-Google-Smtp-Source: ABdhPJxOkrfMNgtpvgtI0jBQd5TI+s7AxrhbTUzHiH1c4uJMDPbP6yIDkqlynoxdvePB0TxGgeLvyw== X-Received: by 2002:ac2:46f4:: with SMTP id q20mr372534lfo.316.1611002256790; Mon, 18 Jan 2021 12:37:36 -0800 (PST) Received: from grain.localdomain ([5.18.91.94]) by smtp.gmail.com with ESMTPSA id m25sm2019800lfb.144.2021.01.18.12.37.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jan 2021 12:37:35 -0800 (PST) Received: by grain.localdomain (Postfix, from userid 1000) id 709A45609AB; Mon, 18 Jan 2021 23:35:59 +0300 (MSK) To: tml Date: Mon, 18 Jan 2021 23:35:56 +0300 Message-Id: <20210118203556.281700-9-gorcunov@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210118203556.281700-1-gorcunov@gmail.com> References: <20210118203556.281700-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v12 8/8] test: box/cfunc -- add cmod test X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Cyrill Gorcunov via Tarantool-patches Reply-To: Cyrill Gorcunov Cc: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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, | + cmod = true, | pwd = true, | socket = true, | strict = true, ie need to enable cmod module. Part-of #4642 Signed-off-by: Cyrill Gorcunov --- test/box/CMakeLists.txt | 2 + test/box/cfunc1.c | 58 ++++++++++++ test/box/cfunc2.c | 132 ++++++++++++++++++++++++++ test/box/cmod.result | 199 ++++++++++++++++++++++++++++++++++++++++ test/box/cmod.test.lua | 70 ++++++++++++++ test/box/suite.ini | 2 +- 6 files changed, 462 insertions(+), 1 deletion(-) create mode 100644 test/box/cfunc1.c create mode 100644 test/box/cfunc2.c create mode 100644 test/box/cmod.result create mode 100644 test/box/cmod.test.lua 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/cfunc1.c b/test/box/cfunc1.c new file mode 100644 index 000000000..8f6d3990c --- /dev/null +++ b/test/box/cfunc1.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "module.h" + +/* + * Before the reload functions are just declared + * and simply exit with zero. + * + * After the module reload we should provide real + * functionality. + */ + +int +cfunc_nop(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} + +int +cfunc_fetch_evens(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} + +int +cfunc_multireturn(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} + +int +cfunc_args(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} + +int +cfunc_sum(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} diff --git a/test/box/cfunc2.c b/test/box/cfunc2.c new file mode 100644 index 000000000..f6d826183 --- /dev/null +++ b/test/box/cfunc2.c @@ -0,0 +1,132 @@ +#include +#include +#include + +#include "module.h" + +/* + * Just make sure we've been called. + */ +int +cfunc_nop(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + (void)ctx; + (void)args; + (void)args_end; + return 0; +} + +/* + * Fetch first N even numbers (just to make sure the order of + * arguments is not screwed). + */ +int +cfunc_fetch_evens(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + int arg_count = mp_decode_array(&args); + if (arg_count != 1) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "invalid argument count"); + } + int field_count = mp_decode_array(&args); + if (field_count < 1) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "invalid array size"); + } + + 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); + } + } + + return 0; +} + +/* + * Return one element array twice. + */ +int +cfunc_multireturn(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + char tuple_buf[512]; + char *d = tuple_buf; + d = mp_encode_array(d, 1); + d = mp_encode_uint(d, 1); + assert(d <= tuple_buf + sizeof(tuple_buf)); + + box_tuple_format_t *fmt = box_tuple_format_default(); + box_tuple_t *tuple_a = box_tuple_new(fmt, tuple_buf, d); + if (tuple_a == NULL) + return -1; + int rc = box_return_tuple(ctx, tuple_a); + if (rc != 0) + return rc; + return box_return_tuple(ctx, tuple_a); +} + +/* + * Encode int + string pair back. + */ +int +cfunc_args(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + uint32_t arg_count = mp_decode_array(&args); + if (arg_count != 2) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "invalid argument count"); + } + + if (mp_typeof(*args) != MP_UINT) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "tuple field must be uint"); + } + uint32_t num = mp_decode_uint(&args); + + if (mp_typeof(*args) != MP_STR) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "tuple field must be string"); + } + const char *str = args; + uint32_t len = mp_decode_strl(&str); + + char tuple_buf[512]; + char *d = tuple_buf; + d = mp_encode_array(d, 2); + d = mp_encode_uint(d, num); + d = mp_encode_str(d, str, len); + assert(d <= tuple_buf + sizeof(tuple_buf)); + + box_tuple_format_t *fmt = box_tuple_format_default(); + box_tuple_t *tuple = box_tuple_new(fmt, tuple_buf, d); + if (tuple == NULL) + return -1; + + return box_return_tuple(ctx, tuple); +} + +/* + * Sum two integers. + */ +int +cfunc_sum(box_function_ctx_t *ctx, const char *args, const char *args_end) +{ + uint32_t arg_count = mp_decode_array(&args); + if (arg_count != 2) { + return box_error_set(__FILE__, __LINE__, ER_PROC_C, "%s", + "invalid argument count"); + } + uint64_t a = mp_decode_uint(&args); + uint64_t b = mp_decode_uint(&args); + + char res[16]; + char *end = mp_encode_uint(res, a + b); + box_return_mp(ctx, res, end); + return 0; +} diff --git a/test/box/cmod.result b/test/box/cmod.result new file mode 100644 index 000000000..f8558c191 --- /dev/null +++ b/test/box/cmod.result @@ -0,0 +1,199 @@ +-- 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 + | --- + | ... + +cmod = require('cmod') + | --- + | ... +fio = require('fio') + | --- + | ... + +ext = (jit.os == "OSX" and "dylib" or "so") + | --- + | ... + +cfunc_path = fio.pathjoin(build_path, "test/box/cfunc.") .. ext + | --- + | ... +cfunc1_path = fio.pathjoin(build_path, "test/box/cfunc1.") .. ext + | --- + | ... +cfunc2_path = fio.pathjoin(build_path, "test/box/cfunc2.") .. ext + | --- + | ... + +_ = pcall(fio.unlink(cfunc_path)) + | --- + | ... +fio.symlink(cfunc1_path, cfunc_path) + | --- + | - true + | ... + +module, err = cmod.load('non-existing-cfunc') + | --- + | ... +assert(err ~= nil) + | --- + | - true + | ... + +-- +-- They all are sitting in cfunc.so +module, err = cmod.load('cfunc') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... + +cfunc_nop, err = module:load('no-such-func') + | --- + | ... +assert(err ~= nil) + | --- + | - true + | ... +cfunc_nop, err = module:load('cfunc_nop') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... +cfunc_fetch_evens, err = module:load('cfunc_fetch_evens') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... +cfunc_multireturn, err = module:load('cfunc_multireturn') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... +cfunc_args, err = module:load('cfunc_args') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... +cfunc_sum, err = module:load('cfunc_sum') + | --- + | ... +assert(err == nil) + | --- + | - true + | ... + +-- +-- Make sure they all are callable +cfunc_nop() + | --- + | - true + | ... +cfunc_fetch_evens() + | --- + | - true + | ... +cfunc_multireturn() + | --- + | - true + | ... +cfunc_args() + | --- + | - true + | ... + +-- +-- Clean old function references and reload a new one. +_ = pcall(fio.unlink(cfunc_path)) + | --- + | ... +fio.symlink(cfunc2_path, cfunc_path) + | --- + | - true + | ... + +module:reload() + | --- + | - true + | ... + +cfunc_nop() + | --- + | - true + | ... +cfunc_multireturn() + | --- + | - true + | - [1] + | - [1] + | ... +cfunc_fetch_evens({2,4,6}) + | --- + | - true + | ... +cfunc_fetch_evens({1,2,3}) -- error + | --- + | - null + | - invalid argument 1 != 2 + | ... +cfunc_args(1, "hello") + | --- + | - true + | - [1, 'hello'] + | ... +cfunc_sum(1) -- error + | --- + | - null + | - invalid argument count + | ... +cfunc_sum(1,2) + | --- + | - true + | - 3 + | ... + +-- +-- Clean them up +cfunc_nop:unload() + | --- + | - true + | ... +cfunc_multireturn:unload() + | --- + | - true + | ... +cfunc_fetch_evens:unload() + | --- + | - true + | ... +cfunc_args:unload() + | --- + | - true + | ... +cfunc_sum:unload() + | --- + | - true + | ... +module:unload() + | --- + | - true + | ... + +-- +-- Cleanup the generated symlink +_ = pcall(fio.unlink(cfunc_path)) + | --- + | ... diff --git a/test/box/cmod.test.lua b/test/box/cmod.test.lua new file mode 100644 index 000000000..e9837e693 --- /dev/null +++ b/test/box/cmod.test.lua @@ -0,0 +1,70 @@ +build_path = os.getenv("BUILDDIR") +package.cpath = build_path..'/test/box/?.so;'..build_path..'/test/box/?.dylib;'..package.cpath + +cmod = require('cmod') +fio = require('fio') + +ext = (jit.os == "OSX" and "dylib" or "so") + +cfunc_path = fio.pathjoin(build_path, "test/box/cfunc.") .. ext +cfunc1_path = fio.pathjoin(build_path, "test/box/cfunc1.") .. ext +cfunc2_path = fio.pathjoin(build_path, "test/box/cfunc2.") .. ext + +_ = pcall(fio.unlink(cfunc_path)) +fio.symlink(cfunc1_path, cfunc_path) + +module, err = cmod.load('non-existing-cfunc') +assert(err ~= nil) + +-- +-- They all are sitting in cfunc.so +module, err = cmod.load('cfunc') +assert(err == nil) + +cfunc_nop, err = module:load('no-such-func') +assert(err ~= nil) +cfunc_nop, err = module:load('cfunc_nop') +assert(err == nil) +cfunc_fetch_evens, err = module:load('cfunc_fetch_evens') +assert(err == nil) +cfunc_multireturn, err = module:load('cfunc_multireturn') +assert(err == nil) +cfunc_args, err = module:load('cfunc_args') +assert(err == nil) +cfunc_sum, err = module:load('cfunc_sum') +assert(err == nil) + +-- +-- Make sure they all are callable +cfunc_nop() +cfunc_fetch_evens() +cfunc_multireturn() +cfunc_args() + +-- +-- Clean old function references and reload a new one. +_ = pcall(fio.unlink(cfunc_path)) +fio.symlink(cfunc2_path, cfunc_path) + +module:reload() + +cfunc_nop() +cfunc_multireturn() +cfunc_fetch_evens({2,4,6}) +cfunc_fetch_evens({1,2,3}) -- error +cfunc_args(1, "hello") +cfunc_sum(1) -- error +cfunc_sum(1,2) + +-- +-- Clean them up +cfunc_nop:unload() +cfunc_multireturn:unload() +cfunc_fetch_evens:unload() +cfunc_args:unload() +cfunc_sum:unload() +module:unload() + +-- +-- Cleanup the generated symlink +_ = pcall(fio.unlink(cfunc_path)) diff --git a/test/box/suite.ini b/test/box/suite.ini index e700d0b9e..fc16c5951 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 cmod.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.29.2