Skip to content

Commit a3c5cd0

Browse files
committed
Cover supplying 0 union credits as a unit test and remove QLS coverage
Same principle as previous patches. Keep the precious coverage space in the QLS tests for the non-trivial cases, and cover the trivial case in a unit test.
1 parent 0a0de7f commit a3c5cd0

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/Database/LSMTree/Internal.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,4 +1649,9 @@ supplyUnionCredits t credits = do
16491649
RW.withWriteAccess (tableContent tEnv) $ \tableContent ->
16501650
case tableUnionLevel tableContent of
16511651
NoUnion -> pure (tableContent, credits) -- all leftovers
1652-
Union{} -> error "supplyUnionCredits: not yet implemented"
1652+
Union{}
1653+
| credits <= UnionCredits 0 -> pure (tableContent, UnionCredits 0)
1654+
--TODO: remove this 0 special case once the general case covers it.
1655+
-- We do not need to optimise the 0 case. It is just here to
1656+
-- simplify test coverage.
1657+
| otherwise -> error "supplyUnionCredits: not yet implemented"

test/Database/LSMTree/Model/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ supplyUnionCredits ::
846846
supplyUnionCredits t@Table{..} c@(UnionCredits credits)
847847
| credits <= 0 = do
848848
_ <- guardTableIsOpen t
849-
pure c
849+
pure (UnionCredits 0) -- always 0, not negative
850850
| otherwise = do
851851
(updc, table) <- guardTableIsOpen t
852852
when (isUnionDescendant == IsUnionDescendant) $

test/Test/Database/LSMTree/StateMachine.hs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,15 +1999,10 @@ arbitraryActionWithVars _ label ctx (ModelState st _stats) =
19991999
, isJust (eqT @h @ModelIO.Table)
20002000
]
20012001
where
2002-
-- TODO: tweak distribution once table unions are implemented
2003-
genUnionCredits = QC.frequency [
2004-
-- The typical, interesting case is to supply a positive number of
2005-
-- union credits.
2006-
(9, R.UnionCredits . QC.getPositive <$> QC.arbitrary)
2007-
-- Supplying 0 or less credits is a no-op, so we generate it only
2008-
-- rarely.
2009-
, (1, R.UnionCredits <$> QC.arbitrary)
2010-
]
2002+
-- The typical, interesting case is to supply a positive number of
2003+
-- union credits. Supplying 0 or less credits is a no-op. We cover
2004+
-- it in UnitTests so we don't have to cover it here.
2005+
genUnionCredits = R.UnionCredits . QC.getPositive <$> QC.arbitrary
20112006

20122007
-- TODO: tweak distribution once table unions are implemented
20132008
genPortion = Portion <$> QC.elements [1, 2, 3]

test/Test/Database/LSMTree/UnitTests.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tests =
3535
, testCase "unit_snapshots" unit_snapshots
3636
, testCase "unit_unions_1" unit_unions_1
3737
, testCase "unit_union_credits" unit_union_credits
38+
, testCase "unit_union_credit_0" unit_union_credit_0
3839
]
3940

4041
unit_blobs :: (String -> IO ()) -> Assertion
@@ -191,6 +192,27 @@ unit_union_credits =
191192
UnionCredits leftover <- supplyUnionCredits table (UnionCredits 42)
192193
leftover @?= 42
193194

195+
-- | Supplying zero or negative credits to union tables works, but does nothing.
196+
unit_union_credit_0 :: Assertion
197+
unit_union_credit_0 =
198+
withTempIOHasBlockIO "test" $ \hfs hbio ->
199+
withSession nullTracer hfs hbio (FS.mkFsPath []) $ \sess ->
200+
withTable @_ @Key1 @Value1 @Blob1 sess defaultTableConfig $ \table -> do
201+
inserts table [(Key1 17, Value1 42, Nothing)]
202+
203+
bracket (table `union` table) close $ \table' -> do
204+
-- Supplying 0 credits works and returns 0 leftovers.
205+
UnionCredits leftover <- supplyUnionCredits table' (UnionCredits 0)
206+
leftover @?= 0
207+
208+
-- Supplying negative credits also works and returns 0 leftovers.
209+
UnionCredits leftover' <- supplyUnionCredits table' (UnionCredits (-42))
210+
leftover' @?= 0
211+
212+
-- And the table is still vaguely cromulent
213+
r <- lookups table' [Key1 17]
214+
V.map ignoreBlobRef r @?= [Found (Value1 42)]
215+
194216
ignoreBlobRef :: Functor f => f (BlobRef m b) -> f ()
195217
ignoreBlobRef = fmap (const ())
196218

0 commit comments

Comments
 (0)