From: Artem Starshov via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: Alexander Turenko <alexander.turenko@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH 1/3] test: add separated module for proccess operations with timeout Date: Wed, 27 Jan 2021 02:37:06 +0300 [thread overview] Message-ID: <42e2bcfb1f7cf6b227c72c1da2ade8653d953fbc.1611702978.git.artemreyt@tarantool.org> (raw) In-Reply-To: <cover.1611702978.git.artemreyt@tarantool.org> Functions with timeout transferred to module test/app-tap/lua/process_timeout.lua for further using in other tests. (from test/app-tap/gh-4983-tnt-e-assert-false-hangs.test.lua) Follows up #4983 --- test/app-tap/lua/process_timeout.lua | 59 ++++++++++++++++++++++++++++ test/app-tap/suite.ini | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/app-tap/lua/process_timeout.lua diff --git a/test/app-tap/lua/process_timeout.lua b/test/app-tap/lua/process_timeout.lua new file mode 100644 index 000000000..1ca3636b9 --- /dev/null +++ b/test/app-tap/lua/process_timeout.lua @@ -0,0 +1,59 @@ +local fiber = require('fiber') +local clock = require('clock') +local ffi = require('ffi') +local fio = require('fio') +local errno = require('errno') + +-- For process_is_alive. +ffi.cdef([[ + int kill(pid_t pid, int sig); +]]) + +--- Verify whether a process is alive. +local function process_is_alive(pid) + local rc = ffi.C.kill(pid, 0) + return rc == 0 or errno() ~= errno.ESRCH +end + +--- Returns true if process completed before timeout, false otherwise. +local function wait_process_completion(pid, timeout) + local start_time = clock.monotonic() + local process_completed = false + while clock.monotonic() - start_time < timeout do + if not process_is_alive(pid) then + process_completed = true + break + end + fiber.sleep(0.01) + end + return process_completed +end + +--- Open file on reading with timeout. +local function open_with_timeout(filename, timeout) + local fh + local start_time = clock.monotonic() + while not fh and clock.monotonic() - start_time < timeout do + fh = fio.open(filename, {'O_RDONLY'}) + fiber.sleep(0.01) + end + return fh +end + +--- Try to read from file with timeout with interval. +local function read_with_timeout(fh, timeout, interval) + local data = '' + local start_time = clock.monotonic() + while #data == 0 and clock.monotonic() - start_time < timeout do + data = fh:read() + if #data == 0 then fiber.sleep(interval) end + end + return data +end + +return { + process_is_alive = process_is_alive, + wait_process_completion = wait_process_completion, + open_with_timeout = open_with_timeout, + read_with_timeout = read_with_timeout +} diff --git a/test/app-tap/suite.ini b/test/app-tap/suite.ini index 2f16128f9..6e27f88d6 100644 --- a/test/app-tap/suite.ini +++ b/test/app-tap/suite.ini @@ -1,7 +1,7 @@ [default] core = app description = application server tests (TAP) -lua_libs = lua/require_mod.lua lua/serializer_test.lua +lua_libs = lua/require_mod.lua lua/serializer_test.lua lua/process_timeout.lua is_parallel = True pretest_clean = True use_unix_sockets_iproto = True -- 2.28.0
next prev parent reply other threads:[~2021-01-26 23:37 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-01-26 23:37 [Tarantool-patches] [PATCH 0/3] lua: fix tarantool -e always enters interactive mode Artem Starshov via Tarantool-patches 2021-01-26 23:37 ` Artem Starshov via Tarantool-patches [this message] 2021-01-26 23:37 ` [Tarantool-patches] [PATCH 2/3] core: add setting error injections via env Artem Starshov via Tarantool-patches 2021-01-26 23:37 ` [Tarantool-patches] [PATCH 3/3] lua: fix tarantool -e always enters interactive mode Artem Starshov 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=42e2bcfb1f7cf6b227c72c1da2ade8653d953fbc.1611702978.git.artemreyt@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=alexander.turenko@tarantool.org \ --cc=artemreyt@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 1/3] test: add separated module for proccess operations with timeout' \ /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