Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Kaplun <skaplun@tarantool.org>,
	Igor Munkin <imun@tarantool.org>,
	Maxim Kokryashkin <m.kokryashkin@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v4 luajit 3/6] test: introduce utils.h helper for C tests
Date: Wed, 14 Jun 2023 11:49:04 +0300	[thread overview]
Message-ID: <293aa1a0-223c-a8d6-1975-b32d89eff541@tarantool.org> (raw)
In-Reply-To: <72942e4bf8b454a0e7e9259f87eeb826288f91d6.1686383897.git.skaplun@tarantool.org>

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 <README.md> 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: `<ctestname-script.lua>` 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 <limits.h>
> +#include <string.h>
> +
> +#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 <src/luajit.c>.
> +	 */
> +	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 <file> -- 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 */

  reply	other threads:[~2023-06-14  8:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-10  8:03 [Tarantool-patches] [PATCH v4 luajit 0/6] Reworking " Sergey Kaplun via Tarantool-patches
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 1/6] test: fix setting of {DY}LD_LIBRARY_PATH variables Sergey Kaplun via Tarantool-patches
2023-06-14  8:42   ` Sergey Bronnikov via Tarantool-patches
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 2/6] test: introduce module for C tests Sergey Kaplun via Tarantool-patches
2023-06-14  8:47   ` Sergey Bronnikov via Tarantool-patches
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 3/6] test: introduce utils.h helper " Sergey Kaplun via Tarantool-patches
2023-06-14  8:49   ` Sergey Bronnikov via Tarantool-patches [this message]
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 4/6] test: rewrite misclib-getmetrics-capi test in C Sergey Kaplun via Tarantool-patches
2023-06-14  8:49   ` Sergey Bronnikov via Tarantool-patches
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 5/6] test: rewrite misclib-sysprof-capi " Sergey Kaplun via Tarantool-patches
2023-06-14  8:50   ` Sergey Bronnikov via Tarantool-patches
2023-06-10  8:03 ` [Tarantool-patches] [PATCH v4 luajit 6/6] test: rewrite lj-49-bad-lightuserdata " Sergey Kaplun via Tarantool-patches
2023-07-04 17:10 ` [Tarantool-patches] [PATCH v4 luajit 0/6] Reworking C tests Igor Munkin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=293aa1a0-223c-a8d6-1975-b32d89eff541@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v4 luajit 3/6] test: introduce utils.h helper for C tests' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox