From 25cdcf1fac682dd76222c1f6c69c402d1c4090b8 Mon Sep 17 00:00:00 2001 From: "G. Paul Ziemba" Date: Thu, 12 Jan 2023 22:27:57 -0800 Subject: [PATCH] topotests/lib/lutil.py: luSetWaitType("strict"|"nostrict") This change alters the behavior of existing test code. The default mode (before any call to luSetWaitType()) is now "strict". The historical behavior of luCommand(op="wait) is to ignore failures to match the specified regexp in the specified time. In those cases, no result was logged and no error was signaled. This change introduces a new "strict" mode for luCommand(op="wait): in "strict" wait mode, each invocation of luCommand(op="wait) generates an explicit, logged failure result when it fails to match the specified regexp in the specified time. These failures signal an error for the test. Calling luSetWaitType("nostrict") restores the historical behavior. Calling luSetWaitType("strict") (re)enables the new strict behavior. Individual calls to luCommand() may also specify op="wait-nostrict" to override any default and use the historical behavior. Individual calls to luCommand() may also specify op="wait-strict" to override any default and use the new behavior. Signed-off-by: G. Paul Ziemba --- tests/topotests/lib/lutil.py | 45 +++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/tests/topotests/lib/lutil.py b/tests/topotests/lib/lutil.py index 5c1fa24a7b..62c21e9ce4 100644 --- a/tests/topotests/lib/lutil.py +++ b/tests/topotests/lib/lutil.py @@ -50,6 +50,7 @@ class lUtil: l_line = 0 l_dotall_experiment = False l_last_nl = None + l_wait_strict = 1 fout = "" fsum = "" @@ -189,7 +190,7 @@ Total %-4d %-4d %d\n\ self.log("unable to read: " + tstFile) sys.exit(1) - def command(self, target, command, regexp, op, result, returnJson, startt=None): + def command(self, target, command, regexp, op, result, returnJson, startt=None, force_result=False): global net if op == "jsoncmp_pass" or op == "jsoncmp_fail": returnJson = True @@ -292,7 +293,7 @@ Total %-4d %-4d %d\n\ 9, ) if startt != None: - if js != None or ret is not False: + if js != None or ret is not False or force_result is not False: delta = time.time() - startt self.result(target, success, "%s +%4.2f secs" % (result, delta)) elif op == "pass" or op == "fail": @@ -322,12 +323,23 @@ Total %-4d %-4d %d\n\ n = 0 startt = time.time() + if (op == "wait-strict") or ((op == "wait") and self.l_wait_strict): + strict = True + else: + strict = False + # Calculate the amount of `sleep`s we are going to peform. wait_count = int(math.ceil(wait / wait_time)) + 1 + force_result = False while wait_count > 0: n += 1 - found = self.command(target, command, regexp, op, result, returnJson, startt) + + # log a failure on last iteration if we don't get desired regexp + if strict and (wait_count == 1): + force_result = True + + found = self.command(target, command, regexp, op, result, returnJson, startt, force_result) if found is not False: break @@ -378,12 +390,14 @@ def luCommand( returnJson=False, wait_time=0.5, ): - if op != "wait": - return LUtil.command(target, command, regexp, op, result, returnJson) - else: + waitops = ["wait", "wait-strict", "wait-nostrict"] + + if op in waitops: return LUtil.wait( target, command, regexp, op, result, time, returnJson, wait_time ) + else: + return LUtil.command(target, command, regexp, op, result, returnJson) def luLast(usenl=False): @@ -454,6 +468,25 @@ def luShowFail(): if printed > 0: logger.error("See %s for details of errors" % LUtil.fout_name) +# +# Sets default wait type for luCommand(op="wait) (may be overridden by +# specifying luCommand(op="wait-strict") or luCommand(op="wait-nostrict")). +# +# "nostrict" is the historical default behavior, which is to ignore +# failures to match the specified regexp in the specified time. +# +# "strict" means that failure to match the specified regexp in the +# specified time yields an explicit, logged failure result +# +def luSetWaitType(waittype): + if waittype == "strict": + LUtil.l_wait_strict = 1 + else: + if waittype == "nostrict": + LUtil.l_wait_strict = 0 + else: + raise ValueError('waittype must be one of "strict" or "nostrict"') + # for testing if __name__ == "__main__": -- 2.39.5