Skip to content

Commit c930db4

Browse files
committed
prototype lockstep tests: only supply to tables with union
We will however still supply union credits to tables with a union debt that has already been fully paid off (tree merge got completed). This is difficult to avoid, since the model doesn't know how much work it is to complete a particular tree/union merge.
1 parent 545d583 commit c930db4

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

prototypes/ScheduledMergesTestQLS.hs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ tests :: TestTree
2626
tests =
2727
testProperty "ScheduledMerges vs model" $ mapSize (*10) prop_LSM -- still <10s
2828

29+
-- TODO: add tagging, e.g. how often ASupplyUnion makes progress or completes a
30+
-- union merge.
2931
prop_LSM :: Actions (Lockstep Model) -> Property
3032
prop_LSM = Lockstep.runActions (Proxy :: Proxy Model)
3133

@@ -35,9 +37,27 @@ prop_LSM = Lockstep.runActions (Proxy :: Proxy Model)
3537

3638
type ModelLSM = Int
3739

38-
newtype Model = Model { mlsms :: Map ModelLSM (Map Key (Value, Maybe Blob)) }
40+
newtype Model = Model { mlsms :: Map ModelLSM Table }
3941
deriving stock (Show)
4042

43+
data Table = Table {
44+
tableContent :: !(Map Key (Value, Maybe Blob))
45+
, tableHasUnion :: !Bool
46+
}
47+
deriving stock Show
48+
49+
emptyTable :: Table
50+
emptyTable = Table Map.empty False
51+
52+
onContent ::
53+
(Map Key (Value, Maybe Blob) -> Map Key (Value, Maybe Blob))
54+
-> ModelLSM
55+
-> Model
56+
-> Model
57+
onContent f k (Model m) = Model (Map.adjust f' k m)
58+
where
59+
f' t = t { tableContent = f (tableContent t) }
60+
4161
type ModelOp r = Model -> (r, Model)
4262

4363
initModel :: Model
@@ -57,24 +77,24 @@ modelSupplyUnion :: ModelLSM -> NonNegative Credit -> ModelOp ()
5777
modelDump :: ModelLSM -> ModelOp (Map Key (Value, Maybe Blob))
5878

5979
modelNew Model {mlsms} =
60-
(mlsm, Model { mlsms = Map.insert mlsm Map.empty mlsms })
80+
(mlsm, Model { mlsms = Map.insert mlsm emptyTable mlsms })
6181
where
6282
mlsm = Map.size mlsms
6383

64-
modelInsert mlsm k v b Model {mlsms} =
65-
((), Model { mlsms = Map.adjust (Map.insert k (v, b)) mlsm mlsms })
84+
modelInsert mlsm k v b model =
85+
((), onContent (Map.insert k (v, b)) mlsm model)
6686

67-
modelDelete mlsm k Model {mlsms} =
68-
((), Model { mlsms = Map.adjust (Map.delete k) mlsm mlsms })
87+
modelDelete mlsm k model =
88+
((), onContent (Map.delete k) mlsm model)
6989

70-
modelMupsert mlsm k v Model {mlsms} =
71-
((), Model { mlsms = Map.adjust (Map.insertWith resolveValueAndBlob k (v, Nothing)) mlsm mlsms })
90+
modelMupsert mlsm k v model =
91+
((), onContent (Map.insertWith resolveValueAndBlob k (v, Nothing)) mlsm model)
7292

7393
modelLookup mlsm k model@Model {mlsms} =
7494
(result, model)
7595
where
7696
Just mval = Map.lookup mlsm mlsms
77-
result = case Map.lookup k mval of
97+
result = case Map.lookup k (tableContent mval) of
7898
Nothing -> NotFound
7999
Just (v, mb) -> Found v mb
80100

@@ -87,8 +107,9 @@ modelDuplicate mlsm Model {mlsms} =
87107
modelUnions inputs Model {mlsms} =
88108
(mlsm', Model { mlsms = Map.insert mlsm' mval' mlsms })
89109
where
90-
mvals = map (\i -> fromJust (Map.lookup i mlsms)) inputs
91-
mval' = Map.unionsWith resolveValueAndBlob mvals
110+
contents = map (\i -> tableContent (fromJust (Map.lookup i mlsms))) inputs
111+
hasUnion = True
112+
mval' = Table (Map.unionsWith resolveValueAndBlob contents) hasUnion
92113
mlsm' = Map.size mlsms
93114

94115
modelSupplyUnion _mlsm _credit model =
@@ -97,7 +118,7 @@ modelSupplyUnion _mlsm _credit model =
97118
modelDump mlsm model@Model {mlsms} =
98119
(mval, model)
99120
where
100-
Just mval = Map.lookup mlsm mlsms
121+
Just (Table mval _) = Map.lookup mlsm mlsms
101122

102123
instance StateModel (Lockstep Model) where
103124
data Action (Lockstep Model) a where
@@ -185,7 +206,7 @@ instance InLockstep Model where
185206

186207
modelNextState = runModel
187208

188-
arbitraryWithVars ctx _model =
209+
arbitraryWithVars ctx model =
189210
case findVars ctx (Proxy :: Proxy (LSM RealWorld)) of
190211
[] -> return (Some ANew)
191212
vars ->
@@ -247,13 +268,20 @@ instance InLockstep Model where
247268
ADuplicate <$> elements vars)
248269
]
249270
++ [ (1, fmap Some $ do
250-
len <- elements [1..5]
271+
-- bias towards binary, only go high when many tables exist
272+
len <- elements ([2, 2] ++ take (length vars) [1..5])
251273
AUnions <$> vectorOf len (elements vars))
252274
]
253-
-- TODO: only supply to tables with unions?
254-
++ [ (1, fmap Some $
255-
ASupplyUnion <$> elements vars
275+
-- only supply to tables with unions
276+
++ [ (2, fmap Some $
277+
ASupplyUnion <$> elements varsWithUnion
256278
<*> arbitrary)
279+
| let hasUnion v = let MLSM m = lookupVar ctx v in
280+
case Map.lookup m (mlsms model) of
281+
Nothing -> False
282+
Just t -> tableHasUnion t
283+
, let varsWithUnion = filter hasUnion vars
284+
, not (null varsWithUnion)
257285
]
258286
++ [ (1, fmap Some $
259287
ADump <$> elements vars)

0 commit comments

Comments
 (0)