Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions webapi-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Revision history for webapi-test

## 0.7.1.0 -- 2026-12-03 (zenbridge #2923: narrowing keeps the failure it started from; a step says what it allocated)

Two additions to a run's trace. A `StepRecord` and a `RunTrace` gain a field
each, so code that builds either with its constructor names the new field
(`allocated = Nothing`, `otherFailure = Nothing`); code that reads them is
untouched.

* **A narrowing try counts as failing only when it fails a check the first
failure failed.** QuickCheck takes any smaller case that fails, whichever
check failed. A try that failed another check then replaced the first
failure, and the narrowed case, and the regression case kept from it,
were for another fault than the one the run found.
* What a case failed is the checks that did not hold on its failing
steps, by name; a step that failed on its own call, with no check, is
known by its operation (`checksFailed`). The move is not part of it: a
check tied to a stage is judged after every move while its thing rests
there, so the move it fails after is whichever came next.
* A try that fails differently answers QuickCheck as not failing (a
discard, as a move that did not settle answers), so narrowing goes on
from the last try that failed as the first failure did. Its line reads
`failed`, `taken: false`.
* **The first such try is kept in full**, with its case, as
`RunTrace.otherFailure` (`other_failure` in the JSON): a second finding
of the run. It is not narrowed.
* The limit: two faults that fail the same check still read as one.
* Without a recorder the steps' checks are not kept, and narrowing is
QuickCheck's own, as before.
* **A step's record says what the step allocated** (`StepRecord.allocated`,
`allocated` in the JSON): the bytes the case's thread allocated from the
step before it becoming final to this one becoming final, by GHC's
allocation counter, the figure a case's and a move's budget are spent in
(`Test.WebApi.Budget`, which exports `allocatedSoFar`). A case cut on its
allocation budget now shows where the budget went. None on a step its
case's cut left open.

## 0.7.0.0 -- 2026-11-19 (zenbridge #2715: what a move's host is told, and what it may say)

Four things a scenario's host could not do, none of which names a host's
Expand Down
24 changes: 21 additions & 3 deletions webapi-test/quickcheck-dynamic/Test/WebApi/Budget.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Test.WebApi.Budget
, noBudget
, withCaseBudget
, moveBegins
, allocatedSoFar
) where

import Control.Concurrent (ThreadId, myThreadId)
Expand Down Expand Up @@ -44,6 +45,8 @@ data Armed = Armed
{ budget :: AllocBudget
, caseLeft :: Int64
, setTo :: Int64
, spent :: Int64
-- ^ what the thread had allocated when the counter was last set
}

armedThreads :: IORef (M.Map ThreadId Armed)
Expand All @@ -59,7 +62,7 @@ withCaseBudget b@AllocBudget {perCase, perMove} act
tid <- myThreadId
let left = maybe maxBound id perCase
setAllocationCounter left
atomicModifyIORef' armedThreads (\m -> (M.insert tid Armed {budget = b, caseLeft = left, setTo = left} m, ()))
atomicModifyIORef' armedThreads (\m -> (M.insert tid Armed {budget = b, caseLeft = left, setTo = left, spent = 0} m, ()))
enableAllocationLimit
act `finally` do
disableAllocationLimit
Expand All @@ -73,9 +76,24 @@ moveBegins :: IO ()
moveBegins = do
tid <- myThreadId
armed <- M.lookup tid <$> readIORef armedThreads
forM_ armed $ \Armed {budget = b@AllocBudget {perMove}, caseLeft, setTo} -> do
forM_ armed $ \Armed {budget = b@AllocBudget {perMove}, caseLeft, setTo, spent} -> do
now <- getAllocationCounter
let left = caseLeft - (setTo - now)
next = maybe left (min left) perMove
setAllocationCounter next
atomicModifyIORef' armedThreads (\m -> (M.insert tid Armed {budget = b, caseLeft = left, setTo = next} m, ()))
atomicModifyIORef' armedThreads (\m -> (M.insert tid Armed {budget = b, caseLeft = left, setTo = next, spent = spent + (setTo - now)} m, ()))

-- | The bytes this thread has allocated, as a figure that only grows while
-- a case plays on it (zenbridge #2923): under a budget, what the case had
-- spent when its counter was last set and what it spent since; on a thread
-- playing no budgeted case, GHC's own counter, which nothing sets. A
-- recorder reads it at each step, so a step's record says what the step
-- allocated. Read it on the thread that plays the case.
allocatedSoFar :: IO Int64
allocatedSoFar = do
tid <- myThreadId
armed <- M.lookup tid <$> readIORef armedThreads
now <- getAllocationCounter
pure $ case armed of
Just Armed {setTo, spent} -> spent + (setTo - now)
Nothing -> negate now
24 changes: 21 additions & 3 deletions webapi-test/quickcheck-dynamic/Test/WebApi/DynamicLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import Test.QuickCheck.Monadic
import Test.QuickCheck.Monadic qualified as QC
import Data.Reflection
import Data.IORef
import Test.WebApi.Trace (Recorder, ReplayOutcome (..), ReplayRecord (..), StepRecord (..), beginCaseWith, beginReplay, noteReplay, recordedProperty, stepsFailure, takeSteps)
import Test.WebApi.Trace (Recorder, ReplayOutcome (..), ReplayRecord (..), StepRecord (..), beginCaseWith, beginReplay, failsDifferently, noteReplay, recordedProperty, stepsFailure, takeSteps)
import Test.WebApi.Prediction (MoveInconclusive (..))
import Control.Exception (catch)
import Control.Exception (SomeAsyncException, SomeException, catch, fromException, throwIO, try)
import Data.List (find)
import Test.WebApi.Case (CaseItem, CaseRecord (..))
import Test.WebApi.Replay (caseItemsOf, forAllDLRecorded, narrowThroughProgram, replayTest, webApiReplayHooks)
Expand Down Expand Up @@ -155,7 +155,7 @@ propApiWithCase rec kept webapiRunner s =
-- so QuickCheck discards it (the trace says it was inconclusive)
-- each play of the case has a context of its own: the move it is in is
-- never another play's
monadic (\m -> ioProperty (newWebApiSessionsCxt rec >>= \cxt -> webapiRunner (runReaderT m cxt) `catch` inconclusive)) $ do
monadic (\m -> ioProperty (newWebApiSessionsCxt rec >>= \cxt -> sameFailure (webapiRunner (runReaderT m cxt)) `catch` inconclusive)) $ do
mapM_ (\r -> QC.run (liftIO (beginCaseWith r kept))) rec
-- the host is told a play begins, before its first step
QC.run (lift caseBegins)
Expand All @@ -169,6 +169,24 @@ propApiWithCase rec kept webapiRunner s =
WebApiGlobalStateModel {appMoveHost = MoveHost {caseBegins}} = reflect (Proxy @s)
inconclusive MoveInconclusive {move = mv, why} =
pure (QC.counterexample ("move " ++ T.unpack mv ++ " is inconclusive: " ++ T.unpack why) (QC.property QC.Discard))
-- zenbridge #2923: narrowing keeps the failure it started from. QuickCheck takes any smaller case that fails,
-- whichever check failed, so a try that fails a check the first failure did not fail would replace it, and the
-- narrowed case (and the regression case kept from it) would be for another fault than the one the run found.
-- Such a try answers as not failing (a discard, as a move that did not settle answers), whether it failed by
-- its property or by an exception of a step; its recorder notes it failed, not taken, and keeps the first
-- such in full ("Test.WebApi.Trace".'failsDifferently'). Without a recorder the steps' checks are not kept,
-- and narrowing is QuickCheck's own. An asynchronous exception (the case's budget, its thread cancelled) and a
-- move that did not settle are left as they are.
sameFailure :: IO Property -> IO Property
sameFailure play = case rec of
Nothing -> play
Just r -> try play >>= \case
Left e
| Just (_ :: SomeAsyncException) <- fromException e -> throwIO e
| Just (_ :: MoveInconclusive) <- fromException e -> throwIO e
| otherwise -> failsDifferently r >>= \differs -> if differs then pure notTaken else throwIO (e :: SomeException)
Right p -> failsDifferently r >>= \differs -> pure (if differs then notTaken else p)
notTaken = QC.counterexample "a narrowing try that failed differently from the first failure: not taken" (QC.property QC.Discard)

-- runWebApiTest :: forall r apps. WebApiGlobalStateModel apps -> (forall (s :: Type). Reifies s (WebApiGlobalStateModel apps) => Proxy s -> r) -> r
-- runWebApiTest gstate runner = reify gstate (\ps -> runner ps)
Expand Down
5 changes: 3 additions & 2 deletions webapi-test/quickcheck-dynamic/Test/WebApi/StateModel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ recordPerformed rec st (MkWebApiAction act) lkp res andThen = do
stageBefore <- liftIO (stageOfGuarded lkp st)
let
engineStep k op i shown = StepRecord
{ n = 0, kind = k, info = i, operation = op, method = Nothing, expect = Nothing, request = Nothing, inMove = Nothing, inconclusive = Nothing, evidence = []
{ n = 0, kind = k, info = i, operation = op, method = Nothing, expect = Nothing, request = Nothing, inMove = Nothing, inconclusive = Nothing, evidence = [], allocated = Nothing
, answer = Just AnswerRecord
{ httpStatus = Nothing
, body = Nothing
Expand All @@ -1314,7 +1314,7 @@ recordPerformed rec st (MkWebApiAction act) lkp res andThen = do
let (req, meth) = requestRecordOf creq
pure StepRecord
{ n = 0, kind = KindCall, info = Nothing, operation = T.pack (getOpIdFromRequest creq), method = Just meth
, expect = Nothing, request = Just req, inMove = Nothing, inconclusive = Nothing, evidence = []
, expect = Nothing, request = Just req, inMove = Nothing, inconclusive = Nothing, evidence = [], allocated = Nothing
, answer = Just AnswerRecord {httpStatus = H.statusCode . fst <$> raw, body = bodyMay >>= bodyText bodyClip, value = Nothing, failure = either (Just . shownText) (const Nothing) res}
, checks = [], labels = [], model = Nothing
, stage = StageRecord {before = stageBefore, after = Nothing}
Expand Down Expand Up @@ -1375,6 +1375,7 @@ callRecord lkp creq i expectation res stageBefore = do
, inMove = Nothing
, inconclusive = Nothing
, evidence = []
, allocated = Nothing
, answer = Just AnswerRecord
{ httpStatus = (H.statusCode . fst <$> raw) <|> either errorStatus (const Nothing) res
, body = bodyMay >>= bodyText bodyClip
Expand Down
Loading
Loading