<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Petr,</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 2, 2021 at 6:02 AM Petr Vorel <<a href="mailto:pvorel@suse.cz" target="_blank">pvorel@suse.cz</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">to sync with C API. This allows to setup timer after test has started.<br>
It's useful when test length depends on input decided during setup.<br>
<br>
Suggested-by: Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>><br>
Signed-off-by: Petr Vorel <<a href="mailto:pvorel@suse.cz" target="_blank">pvorel@suse.cz</a>><br>
---<br>
New in v3.<br>
<br>
doc/test-writing-guidelines.txt | 16 ++++++++++++----<br>
testcases/lib/tst_test.sh | 23 ++++++++++++++++++-----<br>
2 files changed, 30 insertions(+), 9 deletions(-)<br>
<br>
diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt<br>
index dd1911ceb..50696e14a 100644<br>
--- a/doc/test-writing-guidelines.txt<br>
+++ b/doc/test-writing-guidelines.txt<br>
@@ -2393,8 +2393,8 @@ tst_run<br>
'$TST_TEST_DATA' can be used with '$TST_CNT'. If '$TST_TEST_DATA_IFS' not specified,<br>
space as default value is used. Of course, it's possible to use separate functions.<br>
<br>
-2.3.2 Library environment variables for shell<br>
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
+2.3.2 Library environment variables and functions for shell<br>
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
<br>
Similarily to the C library various checks and preparations can be requested<br>
simply by setting right '$TST_NEEDS_FOO'.<br>
@@ -2415,11 +2415,19 @@ simply by setting right '$TST_NEEDS_FOO'.<br>
| 'TST_TIMEOUT' | Maximum timeout set for the test in sec. Must be int >= 1,<br>
or -1 (special value to disable timeout), default is 300.<br>
Variable is meant be set in tests, not by user.<br>
- It's equivalent of `tst_test.timeout` in C.<br>
+ It's an equivalent of `tst_test.timeout` in C, can be set<br>
+ via 'tst_set_timeout(timeout)' after test has started.<br>
+|=============================================================================<br>
+<br>
+[options="header"]<br>
+|=============================================================================<br>
+| Function name | Action done<br>
+| 'tst_set_timeout(timeout)' | Maximum timeout set for the test in sec.<br>
+ See 'TST_TIMEOUT' variable.<br>
|=============================================================================<br>
<br>
NOTE: Network tests (see testcases/network/README.md) use additional variables<br>
-in 'tst_net.sh'.<br>
+and functions in 'tst_net.sh'.<br>
<br>
Checking for presence of commands<br>
+++++++++++++++++++++++++++++++++<br>
diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh<br>
index 69f007d89..58056e28b 100644<br>
--- a/testcases/lib/tst_test.sh<br>
+++ b/testcases/lib/tst_test.sh<br>
@@ -1,6 +1,6 @@<br>
#!/bin/sh<br>
# SPDX-License-Identifier: GPL-2.0-or-later<br>
-# Copyright (c) Linux Test Project, 2014-2020<br>
+# Copyright (c) Linux Test Project, 2014-2021<br>
# Author: Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>><br>
#<br>
# LTP test library for shell.<br>
@@ -23,6 +23,14 @@ export TST_LIB_LOADED=1<br>
# default trap function<br>
trap "tst_brk TBROK 'test interrupted'" INT<br>
<br>
+_tst_cleanup_timer()<br>
+{<br>
+ if [ -n "$_tst_setup_timer_pid" ]; then<br>
+ kill $_tst_setup_timer_pid 2>/dev/null<br>
+ wait $_tst_setup_timer_pid 2>/dev/null<br>
+ fi<br>
+}<br>
+<br>
_tst_do_exit()<br>
{<br>
local ret=0<br>
@@ -48,10 +56,7 @@ _tst_do_exit()<br>
[ "$TST_TMPDIR_RHOST" = 1 ] && tst_cleanup_rhost<br>
fi<br>
<br>
- if [ -n "$_tst_setup_timer_pid" ]; then<br>
- kill $_tst_setup_timer_pid 2>/dev/null<br>
- wait $_tst_setup_timer_pid 2>/dev/null<br>
- fi<br>
+ _tst_cleanup_timer<br>
<br>
if [ $TST_FAIL -gt 0 ]; then<br>
ret=$((ret|1))<br>
@@ -459,6 +464,8 @@ _tst_setup_timer()<br>
<br>
tst_res TINFO "timeout per run is ${h}h ${m}m ${s}s"<br>
<br>
+ _tst_cleanup_timer<br>
+<br>
sleep $sec && tst_res TBROK "test killed, timeout! If you are running on slow machine, try exporting LTP_TIMEOUT_MUL > 1" && kill -9 -$pid &<br>
<br>
_tst_setup_timer_pid=$!<br>
@@ -492,6 +499,12 @@ tst_require_module()<br>
tst_res TINFO "Found module at '$TST_MODPATH'"<br>
}<br>
<br>
+tst_set_timeout()<br>
+{<br>
+ TST_TIMEOUT="$1"<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Not sure if we should check "$1" is valid again before using it.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default">I guess in most scenarios, the function is invoked by tests, so</div><div class="gmail_default">just needs to guarantee $1 > $TST_TIMEOUT, otherwise, it</div><div class="gmail_default">looks meaningless to reset TST_TIMEOUT?</div><div class="gmail_default">(especially to avoid people set a smaller value by a typo)</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ _tst_setup_timer<br>
+}<br>
+<br>
tst_run()<br>
{<br>
local _tst_i<br>
-- <br>
2.30.1<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>