Skip to content

Commit 07275cd

Browse files
committed
QLS: StaticMaybe
1 parent 172a78b commit 07275cd

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

test/Test/Database/LSMTree/StateMachine.hs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ createSystemTempDirectory prefix = do
473473
hasBlockIO <- ioHasBlockIO hasFS defaultIOCtxParams
474474
pure (tempDir, hasFS, hasBlockIO)
475475

476+
476477
{-------------------------------------------------------------------------------
477478
Key and value types
478479
-------------------------------------------------------------------------------}
@@ -601,13 +602,13 @@ instance ( Show (Class.TableConfig (Table h))
601602
-- Snapshots
602603
CreateSnapshot ::
603604
C k v b
604-
=> Maybe Errors
605+
=> StaticMaybe 'True (Maybe Errors)
605606
-> R.SnapshotLabel -> R.SnapshotName -> Var h (WrapTable (Table h) IO k v b)
606607
-> Act h ()
607608
OpenSnapshot ::
608609
C k v b
609610
=> {-# UNPACK #-} !(PrettyProxy (k, v, b))
610-
-> Maybe Errors
611+
-> StaticMaybe 'True (Maybe Errors)
611612
-> R.SnapshotLabel -> R.SnapshotName
612613
-> Act h (WrapTable (Table h) IO k v b)
613614
DeleteSnapshot :: R.SnapshotName -> Act h ()
@@ -917,8 +918,11 @@ data RealEnv h m = RealEnv {
917918
}
918919

919920
data InjectFaultResult =
920-
-- | No faults were injected.
921-
InjectFaultNone
921+
-- | Fault injection was disabled.
922+
InjectFaultDisabled
923+
String -- ^ Action name
924+
-- | Fault injections were enabled, but no faults injections were generated.
925+
| InjectFaultNone
922926
String -- ^ Action name
923927
-- | Faults were injected, but the action accidentally succeeded, so the
924928
-- action had to be rolled back
@@ -1126,12 +1130,12 @@ runModel lookUp = \case
11261130
. Model.runModelM (Model.retrieveBlobs (getBlobRefs . lookUp $ blobsVar))
11271131
CreateSnapshot merrs label name tableVar ->
11281132
wrap MUnit
1129-
. Model.runModelMWithInjectedErrors merrs
1133+
. Model.runModelMWithInjectedErrors (staticMaybe Nothing id merrs)
11301134
(Model.createSnapshot label name (getTable $ lookUp tableVar))
11311135
(pure ())
11321136
OpenSnapshot _ merrs label name ->
11331137
wrap MTable
1134-
. Model.runModelMWithInjectedErrors merrs
1138+
. Model.runModelMWithInjectedErrors (staticMaybe Nothing id merrs)
11351139
(Model.openSnapshot label name)
11361140
(pure ())
11371141
DeleteSnapshot name ->
@@ -1313,16 +1317,19 @@ runRealWithInjectedErrors ::
13131317
(MonadCatch m, MonadSTM m, PrimMonad m)
13141318
=> String -- ^ Name of the action
13151319
-> RealEnv h m
1316-
-> Maybe Errors
1320+
-> StaticMaybe b (Maybe Errors)
13171321
-> m t-- ^ Action to run
13181322
-> (t -> m ()) -- ^ Rollback if the action *accidentally* succeeded
13191323
-> m (Either Model.Err t)
13201324
runRealWithInjectedErrors s env merrs k rollback =
13211325
case merrs of
1322-
Nothing -> do
1326+
StaticNothing -> do
1327+
modifyMutVar faultsVar (InjectFaultDisabled s :)
1328+
catchErr handlers k
1329+
StaticJust Nothing -> do
13231330
modifyMutVar faultsVar (InjectFaultNone s :)
13241331
catchErr handlers k
1325-
Just errs -> do
1332+
StaticJust (Just errs) -> do
13261333
eith <- catchErr handlers $ FSSim.withErrors errsVar errs k
13271334
case eith of
13281335
Left (Model.ErrFsError _) -> do
@@ -1453,7 +1460,7 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
14531460
genErrors <*> pure label <*> genUsedSnapshotName)
14541461
| not (null usedSnapshotNames)
14551462
-- TODO: generate errors
1456-
, let genErrors = pure Nothing
1463+
, let genErrors = genStaticMaybe $ pure Nothing
14571464
]
14581465

14591466
++ [ (1, fmap Some $ DeleteSnapshot <$> genUsedSnapshotName)
@@ -1481,7 +1488,7 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
14811488
genErrors <*> pure label <*> genUnusedSnapshotName <*> genTableVar)
14821489
| not (null unusedSnapshotNames)
14831490
-- TODO: generate errors
1484-
, let genErrors = pure Nothing
1491+
, let genErrors = genStaticMaybe $ pure Nothing
14851492
]
14861493
++ [ (5, fmap Some $ Duplicate <$> genTableVar)
14871494
| length tableVars <= 5 -- no more than 5 tables at once
@@ -2147,3 +2154,49 @@ tagFinalState actions tagger =
21472154
finalAnnState = stateAfter @(Lockstep state) actions
21482155

21492156
finalTags = tagger $ underlyingState finalAnnState
2157+
2158+
{-------------------------------------------------------------------------------
2159+
StaticMaybe
2160+
-------------------------------------------------------------------------------}
2161+
2162+
type StaticMaybe :: Bool -> Type -> Type
2163+
data StaticMaybe isJust a where
2164+
StaticJust :: a -> StaticMaybe True a
2165+
StaticNothing :: StaticMaybe False a
2166+
2167+
deriving stock instance Eq a => Eq (StaticMaybe isJust a)
2168+
deriving stock instance Show a => Show (StaticMaybe isJust a)
2169+
2170+
staticMaybe :: b -> (a -> b) -> StaticMaybe isJust a -> b
2171+
staticMaybe _ f (StaticJust x) = f x
2172+
staticMaybe z _ StaticNothing = z
2173+
2174+
data SBool b where
2175+
STrue :: SBool True
2176+
SFalse :: SBool False
2177+
2178+
class SBoolI a where
2179+
sbool :: Proxy a -> SBool a
2180+
2181+
instance SBoolI True where
2182+
sbool _ = STrue
2183+
2184+
instance SBoolI False where
2185+
sbool _ = SFalse
2186+
2187+
genStaticMaybe ::
2188+
forall isJust a. SBoolI isJust
2189+
=> Gen a
2190+
-> Gen (StaticMaybe isJust a)
2191+
genStaticMaybe gen = case sbool (Proxy @isJust) of
2192+
STrue -> StaticJust <$> gen
2193+
SFalse -> pure StaticNothing
2194+
2195+
shrinkStaticMaybe :: (a -> [a]) -> StaticMaybe isJust a -> [StaticMaybe isJust a]
2196+
shrinkStaticMaybe shr = \case
2197+
StaticNothing -> []
2198+
StaticJust x -> StaticJust <$> shr x
2199+
2200+
instance (Arbitrary a, SBoolI isJust) => Arbitrary (StaticMaybe isJust a) where
2201+
arbitrary = genStaticMaybe QC.arbitrary
2202+
shrink = shrinkStaticMaybe QC.shrink

0 commit comments

Comments
 (0)