diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2025-09-15 21:10:39 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2025-09-19 19:44:28 +0200 |
commit | 89ed9a3a6c0ada2b1c252a5e24283b84eb0fa4c8 (patch) | |
tree | 5ba8a271690d43bab73fd4c86ff2702ca8afce45 | |
parent | 77fdc01b6dfa6f497ed80a46c51e227ca9bdaeed (diff) |
Add timeout argument for the ‘expect’ command
Changelog: Added `timeout` argument for the `expect` command
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | src/Parser/Statement.hs | 1 | ||||
-rw-r--r-- | src/Run.hs | 10 | ||||
-rw-r--r-- | src/Test.hs | 2 | ||||
-rw-r--r-- | test/asset/run/callstack.et | 7 | ||||
-rw-r--r-- | test/script/run.et | 12 |
6 files changed, 28 insertions, 9 deletions
@@ -261,7 +261,7 @@ send <string> to <process> Send line with `<string>` to the standard input of `<process>`. ``` -expect <regex> from <process> [capture <var1> [, <var2> ... ]] +expect <regex> from <process> [timeout <timeout>] [capture <var1> [, <var2> ... ]] ``` Check whether `<process>` produces line matching `<regex>` on standard output, and if this does not happen within current timeout, the test fails. Output lines produced before starting this command and not matched by some previous `expect` are accepted as well. @@ -274,6 +274,9 @@ The regular expression can contain capture groups – parts enclosed in parenthe In that case the expect command has to have the `capture` clause with matching number of variable names. Results of the captures are then assigned to the newly created variables as strings. +If the `timeout` clause is used, the current timeout value is multiplied by the given `<timeout>` for this `expect` call. +Timeout of zero can be used to expect a matching output line to have been already produced in the past. + ``` flush [from <proc>] [matching <regex>] ``` diff --git a/src/Parser/Statement.hs b/src/Parser/Statement.hs index 474fa03..571a38e 100644 --- a/src/Parser/Statement.hs +++ b/src/Parser/Statement.hs @@ -434,6 +434,7 @@ testExpect = command "expect" $ Expect <$> cmdLine <*> (fromExprParam <$> paramOrContext "from") <*> param "" + <*> (maybe 1 fromExprParam <$> param "timeout") <*> param "capture" <*> innerBlockFunList @@ -202,8 +202,8 @@ runStep = \case outProc OutputChildStdin p line send p line - Expect line p expr captures inner -> do - expect line p expr captures $ runStep . inner + Expect line p expr timeout captures inner -> do + expect line p expr timeout captures $ runStep . inner Flush p regex -> do atomicallyTest $ flushProcessOutput p regex @@ -318,9 +318,9 @@ exprFailed desc stack pname = do outLine (OutputMatchFail stack) (Just prompt) $ desc <> " failed" throwError Failed -expect :: SourceLine -> Process -> Traced Regex -> [TypedVarName Text] -> ([ Text ] -> TestRun ()) -> TestRun () -expect sline p (Traced trace re) tvars inner = do - timeout <- getCurrentTimeout +expect :: SourceLine -> Process -> Traced Regex -> Scientific -> [TypedVarName Text] -> ([ Text ] -> TestRun ()) -> TestRun () +expect sline p (Traced trace re) etimeout tvars inner = do + timeout <- (etimeout *) <$> getCurrentTimeout delay <- liftIO $ registerDelay $ ceiling $ 1000000 * timeout mbmatch <- atomicallyTest $ (Nothing <$ (check =<< readTVar delay)) <|> do line <- readTVar (procOutput p) diff --git a/src/Test.hs b/src/Test.hs index 18933b1..1481b2b 100644 --- a/src/Test.hs +++ b/src/Test.hs @@ -48,7 +48,7 @@ data TestStep a where Spawn :: TypedVarName Process -> Either Network Node -> [ Text ] -> (Process -> TestStep a) -> TestStep a SpawnShell :: Maybe (TypedVarName Process) -> Node -> ShellScript -> (Process -> TestStep a) -> TestStep a Send :: Process -> Text -> TestStep () - Expect :: SourceLine -> Process -> Traced Regex -> [ TypedVarName Text ] -> ([ Text ] -> TestStep a) -> TestStep a + Expect :: SourceLine -> Process -> Traced Regex -> Scientific -> [ TypedVarName Text ] -> ([ Text ] -> TestStep a) -> TestStep a Flush :: Process -> Maybe Regex -> TestStep () Guard :: CallStack -> Bool -> TestStep () DisconnectNode :: Node -> TestStep a -> TestStep a diff --git a/test/asset/run/callstack.et b/test/asset/run/callstack.et index 954b9ad..f500fb6 100644 --- a/test/asset/run/callstack.et +++ b/test/asset/run/callstack.et @@ -1,3 +1,8 @@ -test A: +test AG: let x = 1 guard (x == 0) + +test AE: + spawn as p + let x = 2 + expect /$x/ from p timeout 0.0 diff --git a/test/script/run.et b/test/script/run.et index dc2b812..f7e4f69 100644 --- a/test/script/run.et +++ b/test/script/run.et @@ -111,10 +111,20 @@ test CallStack: send "load ${scripts.path}/callstack.et" expect /load-done/ - send "run A" + send "run AG" expect /match-fail guard failed/ expect /match-fail-line .*\/callstack.et:3:5: .*/ expect /match-fail-var x 1/ local: expect /(run-.*)/ capture done guard (done == "run-failed") + flush + + send "run AE" + expect /match-fail expect failed/ + expect /match-fail-line .*\/callstack.et:8:5: .*/ + expect /match-fail-var x 2/ + local: + expect /(run-.*)/ capture done + guard (done == "run-failed") + flush |