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 9B9724E2C32; Sat, 10 Jun 2023 11:08:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9B9724E2C32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1686384539; bh=BA9TbR2K7vYLzZ6F0Au5QCdlzFoU3+2WjBhV9d/j+MM=; 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=PoLWjI4NaraRxshrA6fvbtoAIVvlpyPnR6ijts4MLnANo30P+nvWcAlDg+COfdvTz dDRdqzi4FrszA98Q7kzVSBgor8gv+xN+9rcBDYao4NU8pAtCE1gTkpC7Vx0tB1AUGP mE9z4465tzytbXgZjW9mQlQs1pLXPGKeVJXjnjWU= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [94.100.177.149]) (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 2222D4E2C32 for ; Sat, 10 Jun 2023 11:07:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2222D4E2C32 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1q7tdA-0002gO-UK; Sat, 10 Jun 2023 11:07:29 +0300 To: Igor Munkin , Maxim Kokryashkin , Sergey Bronnikov Date: Sat, 10 Jun 2023 11:03:11 +0300 Message-Id: <72942e4bf8b454a0e7e9259f87eeb826288f91d6.1686383897.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD965EFE7F2AC62B27AB26260F83F0A83137ADF745094576C38182A05F538085040489E5A9802B5826121645CFB663CFF806AA7CFA7A76E072509A941715BF853C9 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78E8764B5BC580342EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637993F4D0870F024C68638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8474677E3FF4F8EF1011D27683596AE94117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC587F3D2152687E5CA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735204B6963042765DA4BCB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6B1CFA6D474D4A6A4089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5225D233BF53EB2BD601F57A5D00D2A37EB912330C541849FF87CCE6106E1FC07E67D4AC08A07B9B065B78C30F681404D9C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF277DAC3A5C2A048CBD961443604B38973C6F2F157BB58B2282E66C82E30BC6FBFD9AF20B71F72F3AB5655EA6190D4CDAFA48FC568FF36B16472A7E120921B14F461A413F07889F2102C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojRLo8CqjALXihbc5aQCAQTQ== X-DA7885C5: 7E054FC66DEB94324AAD52CB2238199F88BF2423B8A3E50492C251B6A10C115B262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73930F533AC2B33E986B5CBC55B95A0FE957415B0BC15BA6AAE80FBE9A32752B8C9C2AA642CC12EC09F1FB559BB5D741EB962F61BD320559CF1EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [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 Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 */ -- 2.34.1