Introduce a WUKS analogue which can fail fast 93/54093/8
authorVratko Polak <vrpolak@cisco.com>
Thu, 30 Mar 2017 17:59:28 +0000 (19:59 +0200)
committerJamo Luhrsen <jluhrsen@redhat.com>
Fri, 31 Mar 2017 19:34:08 +0000 (19:34 +0000)
WaitUtils.Wait_For_Getter_Failure_Or_Stateless_Validator_Pass

Change-Id: I9267e07bc177d8dc6fed4f0eb9e60d3a913f8462
Signed-off-by: Vratko Polak <vrpolak@cisco.com>
csit/libraries/WaitUtils.robot

index 3ed1e7d7d103a4cd75858619e35f1e623a6ba6ce..aadfb0f4032e0eda4b2cbb2df452f41f1d6a8657 100644 (file)
@@ -8,10 +8,11 @@ Documentation     Robot keyword library (Resource) with several Keywords for mon
 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
 ...
 ...
-...               BuiltIn.Wait_Until_Keyword_Succeeds is useful in avoiding unnecessary sleeps.
-...               But several usage cases need slightly different logic, here are Keywords for that.
+...               BuiltIn.Wait_Until_Keyword_Succeeds has two possible results: Fast pass or fail on timeout.
+...               Generally, keywords in this Resource also allow for some kind of fast failure condition.
+...               This usually requires more than a single keyword to run inside the iteration loop.
+...               This library uses ScalarClosures for plugging in specific (multiple) Keywords.
 ...
-...               This library uses ScalarClosures for plugging in specific Keywords.
 ...               Storing private state in suite variables is easy, but it can lead to hard-to-debug issues,
 ...               so this library tries to support explicit state passing.
 ...               Unfortunately, failing limits type of message to return,
@@ -26,7 +27,7 @@ Documentation     Robot keyword library (Resource) with several Keywords for mon
 ...               Safe Stateful Validator: Take ${state} and ${data} arguments. Return new state, validation status and comment/message.
 ...               TODO: Create a dummy closure for each type to be used as default value?
 ...
-...               TODO: Figure out a way to merge this with FaitForFailure.robot
+...               TODO: Figure out a way to merge this with WaitForFailure.robot
 ...               TODO: Add Keywords that are Safe (return state, success and message)
 ...               so that callers do not need to refresh state explicitly.
 Library           DateTime
@@ -85,6 +86,22 @@ WaitUtils__Is_Deadline_Reachable
     ${time_minimal} =    BuiltIn.Evaluate    int(${sleeps_left}) * ${period_in_seconds}
     BuiltIn.Run_Keyword_If    ${time_minimal} >= ${time_deadline}    BuiltIn.Fail    Not possible to succeed within the deadline. ${message}
 
+Wait_For_Getter_Failure_Or_Stateless_Validator_Pass
+    [Arguments]    ${timeout}=60s    ${period}=1s    ${getter}=${ScalarClosures__fail}    ${stateless_validator}=${ScalarClosures__identity}
+    [Documentation]    Repeatedly run getter and plug its output to validator. If both pass, return validator message.
+    ...    If getter fails, fail. If validator fails, repeat in WUKS fashion (fail when timeout is exceeded).
+    ...    FIXME: Cover this keyword in WaitUtilTest.robot
+    ${timeout_in_seconds}    ${period_in_seconds}    ${date_deadline} =    WaitUtils__Check_Sanity_And_Compute_Derived_Times    timeout=${timeout}    period=${period}
+    ${iterations} =    BuiltIn.Evaluate    ${timeout_in_seconds} / ${period_in_seconds}
+    : FOR    ${i}    IN RANGE    ${iterations}
+    \    ${data} =    ScalarClosures.Run_Keyword_And_Collect_Garbage    ScalarClosures.Run_Closure_As_Is    ${getter}
+    \    ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    ScalarClosures.Run_Keyword_And_Collect_Garbage    ScalarClosures.Run_Closure_After_Replacing_First_Argument    ${stateless_validator}
+    \    ...    ${data}
+    \    BuiltIn.Return_From_Keyword_If    "${status}" == "PASS"    ${message}
+    \    WaitUtils__Is_Deadline_Reachable    date_deadline=${date_deadline}    period_in_seconds=${period_in_seconds}    message=Last validator message: ${message}
+    \    BuiltIn.Sleep    ${period_in_seconds} s
+    BuiltIn.Fail    Logic error, we should have returned before.
+
 Stateless_Assert_Closure_Has_To_Succeed_Consecutively_By_Deadline
     [Arguments]    ${date_deadline}=0    ${period_in_seconds}=1    ${count}=1    ${assertor}=${ScalarClosures__fail}
     [Documentation]    Pass only if ${assertor} passes ${count} times in a row with ${period_in_seconds} between attempts; less standard arguments.