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 5744B4AC1B8; Wed, 14 Jun 2023 11:49:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5744B4AC1B8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1686732547; bh=JoH1fsQghBcurWcMXy6zfRIcIKULX2RYHOYkD4HKAsE=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=pAdwcJIfszTLQiWh4MnM8z8oNj01+s3ISGfzceLqZG2S5+VU9ARVr4liD25nazJ00 FzJjcX4aewR673X0BN+FoiP7QcDqyDWz1uSSzUgBSUoapVvRJfF9D8cTs520JYzWGs HGLb+Pk4a9B3+uD96cbYKqnlJ0dHv+UuG1/v0Thw= Received: from smtp38.i.mail.ru (smtp38.i.mail.ru [95.163.41.79]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 3AFED4AC1B8 for ; Wed, 14 Jun 2023 11:49:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3AFED4AC1B8 Received: by smtp38.i.mail.ru with esmtpa (envelope-from ) id 1q9MBd-00Fnzn-AA; Wed, 14 Jun 2023 11:49:05 +0300 Message-ID: <293aa1a0-223c-a8d6-1975-b32d89eff541@tarantool.org> Date: Wed, 14 Jun 2023 11:49:04 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: en-US To: Sergey Kaplun , Igor Munkin , Maxim Kokryashkin Cc: tarantool-patches@dev.tarantool.org References: <72942e4bf8b454a0e7e9259f87eeb826288f91d6.1686383897.git.skaplun@tarantool.org> In-Reply-To: <72942e4bf8b454a0e7e9259f87eeb826288f91d6.1686383897.git.skaplun@tarantool.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D99986233CC4DDC30BBECE192AD7C83A31A99510E8D027E182A05F53808504081CF572BB759865919D30AEEC560F6D9C387C69370E8C9F345038ECB1A282D1B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A8325FA649D0A450EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C0EA1E34B58229798638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80C0A56F45688479FDB229DA08734C53E117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF77088377309FF52A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F4460429728776938767073520437C869540D2AB0FC26CFBAC0749D213D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE902A1BE408319B29FCB1554B277F7060D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE398E9388377045835C0837EA9F3D19764C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637A7EFCB0EB5ACB161EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A59DF2775160240FD6676A1A246E1D29CCDD20A8A2442D116DF87CCE6106E1FC07E67D4AC08A07B9B01F9513A7CA91E555CB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF9DA601C155D3E707ADDCA0DE5281B6E4289B5B3B802C7EFA3AC9870870495274E5D0AE1B90484215513AFAF93E604BF51A17D8D76A542FFE1162A87D8329C768A74DFFEFA5DC0E7F02C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojdUHSxnroE537InpsVryHaA== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A7695DC6273C7812A2EB19D30AEEC560F6D97962F9E9328436E1EBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v4 luajit 3/6] test: introduce utils.h helper for C tests 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: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thanks for the fixes, Sergey! LGTM now. On 6/10/23 11:03, Sergey Kaplun wrote: > This header contains generic init and close functions for tests and > helpers for loading auxiliary Lua script with functions to run inside a > C test. See more information in the file. > > It will be properly used in the next commit. > > Part of tarantool/tarantool#7900 > --- > test/tarantool-c-tests/CMakeLists.txt | 3 + > test/tarantool-c-tests/README.md | 26 +++++++++ > test/tarantool-c-tests/utils.h | 79 +++++++++++++++++++++++++++ > 3 files changed, 108 insertions(+) > create mode 100644 test/tarantool-c-tests/utils.h > > diff --git a/test/tarantool-c-tests/CMakeLists.txt b/test/tarantool-c-tests/CMakeLists.txt > index da128457..17255345 100644 > --- a/test/tarantool-c-tests/CMakeLists.txt > +++ b/test/tarantool-c-tests/CMakeLists.txt > @@ -26,6 +26,9 @@ set_target_properties(libtest PROPERTIES > # lj_arch.h in compiled test are consistent with the LuaJIT library > # to link. > AppendFlags(TESTS_C_FLAGS ${TARGET_C_FLAGS}) > +# Use directory for absolute path to the Lua script helper. So, > +# test binary can be run from any directory. > +AppendFlags(TESTS_C_FLAGS "-D__LJ_TEST_DIR__='\"${CMAKE_CURRENT_SOURCE_DIR}\"'") > > set(CTEST_SRC_SUFFIX ".test.c") > file(GLOB tests "${CMAKE_CURRENT_SOURCE_DIR}/*${CTEST_SRC_SUFFIX}") > diff --git a/test/tarantool-c-tests/README.md b/test/tarantool-c-tests/README.md > index 04ddb729..462534be 100644 > --- a/test/tarantool-c-tests/README.md > +++ b/test/tarantool-c-tests/README.md > @@ -44,3 +44,29 @@ earlier: > * `skip_all("reason")` -- skip the current group of tests. > * `todo("reason")` -- skip the current test marking as TODO. > * `bail_out("reason")` -- exit the entire process due to some emergency. > + > +## Testing with Lua source code > + > +Sometimes we need to test C API for modules, that show some Lua metrics (like > +`luaM_metrics` or sysprof). In these cases, the required Lua script should be > +named like the following: `` and contains a table with a > +bunch of Lua functions named the same as the unit C test, that uses this > +function. > + > +```lua > +local M = {} > +M.base = function() > + -- ... > +end > + > +M.test_simple = function() > + -- ... > +end > + > +return M > +``` > + > +The script is loaded via `utils_load_aux_script(L, script_name)`. It loads the > +file and place the table with functions at the top of Lua stack. Each function > +is get from the table via `utils_get_aux_lfunc(L)` helper in the corresponding > +test. > diff --git a/test/tarantool-c-tests/utils.h b/test/tarantool-c-tests/utils.h > new file mode 100644 > index 00000000..49a5832d > --- /dev/null > +++ b/test/tarantool-c-tests/utils.h > @@ -0,0 +1,79 @@ > +#ifndef TARANTOOL_LUAJIT_TEST_UTILS_H > +#define TARANTOOL_LUAJIT_TEST_UTILS_H > + > +#include > +#include > + > +#include "lauxlib.h" > +#include "lua.h" > +#include "luajit.h" > +#include "lualib.h" > + > +#include "test.h" > + > +#define UTILS_UNUSED __attribute__((unused)) > + > +/* Generic init for our tests. */ > +static lua_State *utils_lua_init(void) > +{ > + lua_State *L = luaL_newstate(); > + if (!L) > + bail_out("Can't init Lua state"); > + /* > + * Don't really need to waste time on the GC during > + * library initialization, so stop the collector. > + * Same approach as in `pmain()` in . > + */ > + lua_gc(L, LUA_GCSTOP, 0); > + luaL_openlibs(L); > + lua_gc(L, LUA_GCRESTART, -1); > + return L; > +} > + > +/* Generic close for our tests. */ > +static void utils_lua_close(lua_State *L) > +{ > + lua_close(L); > +} > + > +/* > + * Load the Lua -- the pair to the C test file. > + * Each file should return the table with functions (named the > + * same as the corresponding unit tests) to call in unit tests. > + * Script file name is given as the second argument. > + * Push the table with those functions on the Lua stack. > + */ > +UTILS_UNUSED static void utils_load_aux_script(lua_State *L, const char *file) > +{ > + /* > + * Format script name. > + * `__LJ_TEST_DIR__` is set via CMake. > + */ > + char script[PATH_MAX] = __LJ_TEST_DIR__; > + char *script_name = script + sizeof(__LJ_TEST_DIR__) - 1; > + /* Replace '\0' with '/'. */ > + *script_name++ = '/'; > + /* Append script filename. */ > + strcpy(script_name, file); > + > + if (luaL_dofile(L, script) != LUA_OK) { > + test_comment("Can't load %s: '%s'", script, > + lua_tostring(L, -1)); > + bail_out("Can't load auxiliary script"); > + } > + > + if (!lua_istable(L, -1)) > + bail_out("Returned value from script is not a table"); > +} > + > +/* > + * Accept a table on top of the Lua stack which containing the > + * function named as the unit test we currently executing. > + */ > +#define utils_get_aux_lfunc(L) do { \ > + lua_getfield((L), -1, __func__); \ > + if (!lua_isfunction((L), -1)) \ > + bail_out("Can't get auxiliary test function"); \ > +} while (0) > + > +#endif /* TARANTOOL_LUAJIT_TEST_UTILS_H */