Skip to content

Commit 2e76991

Browse files
authored
Merge pull request #436 from IntersectMBO/dcoutts/lockstep-io-sim-tweak-generation-2
QC Lockstep tests: tweak test case generation
2 parents 3f5bda8 + f279f5e commit 2e76991

File tree

14 files changed

+391
-215
lines changed

14 files changed

+391
-215
lines changed

cabal.project

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ import: cabal.project.debug
2424
-- possible to have conditionals on package flags in a project file. Otherwise,
2525
-- we could add a conditional on (+serialblockio) to remove this import automatically.
2626
import: cabal.project.blockio-uring
27+
28+
source-repository-package
29+
type: git
30+
location: https://github.com/well-typed/quickcheck-lockstep.git
31+
tag: 8a724c0a7b2328a7444490f18dc7e401badcc8b8

lsm-tree.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,10 @@ test-suite lsm-tree-test
371371
Test.Database.LSMTree.Model.Monoidal
372372
Test.Database.LSMTree.Model.Normal
373373
Test.Database.LSMTree.Monoidal
374-
Test.Database.LSMTree.Normal.Examples
375374
Test.Database.LSMTree.Normal.StateMachine
376375
Test.Database.LSMTree.Normal.StateMachine.DL
377376
Test.Database.LSMTree.Normal.StateMachine.Op
377+
Test.Database.LSMTree.Normal.UnitTests
378378
Test.System.Posix.Fcntl.NoCache
379379
Test.Util.FS
380380
Test.Util.Orphans

prototypes/ScheduledMergesTestQLS.hs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ instance InLockstep Model where
305305

306306
modelNextState = runModel
307307

308-
arbitraryWithVars findVars _model =
309-
case findVars (Proxy :: Proxy (LSM RealWorld)) of
308+
arbitraryWithVars ctx _model =
309+
case findVars ctx (Proxy :: Proxy (LSM RealWorld)) of
310310
[] -> return (Some ANew)
311311
vars ->
312312
frequency $
@@ -321,7 +321,7 @@ instance InLockstep Model where
321321
AInsert <$> elements vars
322322
<*> fmap Left (elements kvars) -- key var
323323
<*> arbitrarySizedNatural) -- value
324-
| let kvars = findVars (Proxy :: Proxy Key)
324+
| let kvars = findVars ctx (Proxy :: Proxy Key)
325325
, not (null kvars)
326326
]
327327
-- deletes of arbitrary keys:
@@ -333,7 +333,7 @@ instance InLockstep Model where
333333
++ [ (1, fmap Some $
334334
ADelete <$> elements vars
335335
<*> fmap Left (elements kvars)) -- key var
336-
| let kvars = findVars (Proxy :: Proxy Key)
336+
| let kvars = findVars ctx (Proxy :: Proxy Key)
337337
, not (null kvars)
338338
]
339339
-- lookup of arbitrary keys:
@@ -345,7 +345,7 @@ instance InLockstep Model where
345345
++ [ (3, fmap Some $
346346
ALookup <$> elements vars
347347
<*> fmap Left (elements kvars)) -- key var
348-
| let kvars = findVars (Proxy :: Proxy Key)
348+
| let kvars = findVars ctx (Proxy :: Proxy Key)
349349
, not (null kvars)
350350
]
351351
++ [ (1, fmap Some $
@@ -355,19 +355,19 @@ instance InLockstep Model where
355355
ADuplicate <$> elements vars)
356356
]
357357

358-
shrinkWithVars _findVars _model (AInsert var (Right k) v) =
358+
shrinkWithVars _ctx _model (AInsert var (Right k) v) =
359359
[ Some $ AInsert var (Right k') v' | (k', v') <- shrink (k, v) ]
360360

361-
shrinkWithVars _findVars _model (AInsert var (Left _kv) v) =
361+
shrinkWithVars _ctx _model (AInsert var (Left _kv) v) =
362362
[ Some $ AInsert var (Right k) v | k <- shrink 100 ]
363363

364-
shrinkWithVars _findVars _model (ADelete var (Right k)) =
364+
shrinkWithVars _ctx _model (ADelete var (Right k)) =
365365
[ Some $ ADelete var (Right k') | k' <- shrink k ]
366366

367-
shrinkWithVars _findVars _model (ADelete var (Left _kv)) =
367+
shrinkWithVars _ctx _model (ADelete var (Left _kv)) =
368368
[ Some $ ADelete var (Right k) | k <- shrink 100 ]
369369

370-
shrinkWithVars _findVars _model _action = []
370+
shrinkWithVars _ctx _model _action = []
371371

372372

373373
instance RunLockstep Model IO where
@@ -423,9 +423,10 @@ runActionIO action lookUp =
423423
tr = nullTracer
424424

425425
runModel :: Action (Lockstep Model) a
426-
-> ModelLookUp Model
427-
-> Model -> (ModelValue Model a, Model)
428-
runModel action lookUp m =
426+
-> ModelVarContext Model
427+
-> Model
428+
-> (ModelValue Model a, Model)
429+
runModel action ctx m =
429430
case action of
430431
ANew -> (MLSM mlsm, m')
431432
where (mlsm, m') = modelNew m
@@ -449,8 +450,8 @@ runModel action lookUp m =
449450
where (mapping, _) = modelDump (lookUpLsMVar var) m
450451
where
451452
lookUpLsMVar :: ModelVar Model (LSM RealWorld) -> ModelLSM
452-
lookUpLsMVar var = case lookUp var of MLSM r -> r
453+
lookUpLsMVar var = case lookupVar ctx var of MLSM r -> r
453454

454455
lookUpKeyVar :: ModelVar Model Key -> Key
455-
lookUpKeyVar var = case lookUp var of MInsert k -> k
456+
lookUpKeyVar var = case lookupVar ctx var of MInsert k -> k
456457

src-extras/Database/LSMTree/Extras/Generators.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ instance Arbitrary LargeRawBytes where
492492

493493
deriving newtype instance SerialiseValue LargeRawBytes
494494

495-
-- | Minimum length of 6 bytes.
495+
-- | Minimum length of 8 bytes.
496496
newtype KeyForIndexCompact =
497497
KeyForIndexCompact { getKeyForIndexCompact :: RawBytes }
498498
deriving stock (Eq, Ord, Show)

src/Database/LSMTree/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ data LSMTreeError =
201201
-- The 'Int' index indicates which 'BlobRef' was invalid. Many may be
202202
-- invalid but only the first is reported.
203203
| ErrBlobRefInvalid Int
204-
deriving stock (Show)
204+
deriving stock (Show, Eq)
205205
deriving anyclass (Exception)
206206

207207
{-------------------------------------------------------------------------------

src/Database/LSMTree/Internal/Lookup.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ type ResolveSerialisedValue = SerialisedValue -> SerialisedValue -> SerialisedVa
145145

146146
-- | An 'IOOp' read/wrote fewer or more bytes than expected
147147
data ByteCountDiscrepancy = ByteCountDiscrepancy {
148-
expected :: ByteCount
149-
, actual :: ByteCount
148+
expected :: !ByteCount
149+
, actual :: !ByteCount
150150
}
151-
deriving stock (Show)
151+
deriving stock (Show, Eq)
152152
deriving anyclass (Exception)
153153

154154
{-# SPECIALIZE lookupsIO ::

src/Database/LSMTree/Internal/Paths.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import Control.DeepSeq (NFData (..))
3131
import qualified Data.ByteString.Char8 as BS
3232
import Data.Foldable (toList)
3333
import qualified Data.Map as Map
34+
import Data.Maybe (fromMaybe)
35+
import Data.String (IsString (..))
3436
import Data.Traversable (for)
3537
import Database.LSMTree.Internal.RunNumber
3638
import Database.LSMTree.Internal.UniqCounter
@@ -70,6 +72,12 @@ newtype SnapshotName = MkSnapshotName FilePath
7072
instance Show SnapshotName where
7173
showsPrec d (MkSnapshotName p) = showsPrec d p
7274

75+
-- | This instance uses 'mkSnapshotName', so all the restrictions on snap shot names apply here too. An invalid snapshot name will lead to an error.
76+
instance IsString SnapshotName where
77+
fromString s = fromMaybe bad (mkSnapshotName s)
78+
where
79+
bad = error ("SnapshotName.fromString: invalid name " ++ show s)
80+
7381
-- | Create snapshot name.
7482
--
7583
-- The name may consist of lowercase characters, digits, dashes @-@ and underscores @_@.

test/Database/LSMTree/Model/Normal/Session.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ module Database.LSMTree.Model.Normal.Session (
5858
, rangeLookup
5959
-- ** Cursor
6060
, Cursor
61+
, CursorID
62+
, cursorID
6163
, newCursor
6264
, closeCursor
6365
, readCursor

test/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import qualified Test.Database.LSMTree.Internal.Vector
3131
import qualified Test.Database.LSMTree.Model.Monoidal
3232
import qualified Test.Database.LSMTree.Model.Normal
3333
import qualified Test.Database.LSMTree.Monoidal
34-
import qualified Test.Database.LSMTree.Normal.Examples
3534
import qualified Test.Database.LSMTree.Normal.StateMachine
3635
import qualified Test.Database.LSMTree.Normal.StateMachine.DL
36+
import qualified Test.Database.LSMTree.Normal.UnitTests
3737
import qualified Test.System.Posix.Fcntl.NoCache
3838
import Test.Tasty
3939

@@ -67,7 +67,7 @@ main = defaultMain $ testGroup "lsm-tree"
6767
, Test.Database.LSMTree.Model.Normal.tests
6868
, Test.Database.LSMTree.Model.Monoidal.tests
6969
, Test.Database.LSMTree.Monoidal.tests
70-
, Test.Database.LSMTree.Normal.Examples.tests
70+
, Test.Database.LSMTree.Normal.UnitTests.tests
7171
, Test.Database.LSMTree.Normal.StateMachine.tests
7272
, Test.Database.LSMTree.Normal.StateMachine.DL.tests
7373
, Test.System.Posix.Fcntl.NoCache.tests

test/Test/Database/LSMTree/Internal/RunReaders.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,20 @@ instance InLockstep ReadersState where
184184

185185
modelNextState :: forall a.
186186
LockstepAction ReadersState a
187-
-> ModelLookUp ReadersState
187+
-> ModelVarContext ReadersState
188188
-> ReadersState
189189
-> (ReadersVal a, ReadersState)
190-
modelNextState action lookUp (ReadersState mock) =
191-
ReadersState <$> runMock lookUp action mock
190+
modelNextState action _ctx (ReadersState mock) =
191+
ReadersState <$> runMock action mock
192192

193193
usedVars :: LockstepAction ReadersState a -> [AnyGVar (ModelOp ReadersState)]
194194
usedVars = const []
195195

196196
arbitraryWithVars ::
197-
ModelFindVariables ReadersState
197+
ModelVarContext ReadersState
198198
-> ReadersState
199199
-> Gen (Any (LockstepAction ReadersState))
200-
arbitraryWithVars _ (ReadersState mock)
200+
arbitraryWithVars _ctx (ReadersState mock)
201201
| isEmpty mock = do
202202
-- It's not allowed to keep using a drained RunReaders,
203203
-- we can only create a new one.
@@ -226,11 +226,11 @@ instance InLockstep ReadersState where
226226
]
227227

228228
shrinkWithVars ::
229-
ModelFindVariables ReadersState
229+
ModelVarContext ReadersState
230230
-> ReadersState
231231
-> LockstepAction ReadersState a
232232
-> [Any (LockstepAction ReadersState)]
233-
shrinkWithVars _ _ = \case
233+
shrinkWithVars _ctx _st = \case
234234
New k wb wbs -> [ Some (New k' wb' wbs')
235235
| (k', wb', wbs') <- shrink (k, wb, wbs)
236236
]
@@ -267,11 +267,10 @@ instance InLockstep ReadersState where
267267
]
268268

269269
runMock ::
270-
lookUp
271-
-> Action (Lockstep ReadersState) a
270+
Action (Lockstep ReadersState) a
272271
-> MockReaders
273272
-> (ReadersVal a, MockReaders)
274-
runMock _ = \case
273+
runMock = \case
275274
New k wb wbs -> const $ wrap MUnit (Right (), newMock (coerce k) (toList wb <> wbs))
276275
PeekKey -> \m -> wrap MKey (peekKeyMock m, m)
277276
Pop n -> wrap wrapPop . popMock n

0 commit comments

Comments
 (0)