-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grafting golden tests into the 'Snapshot/Codec' branch of the TestTre…
…e for serialization backwards compatibility testing.
- Loading branch information
1 parent
4ce3c9d
commit 351d4b2
Showing
97 changed files
with
288 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.golden -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
269 changes: 269 additions & 0 deletions
269
test/Test/Database/LSMTree/Internal/Snapshot/Codec/Golden.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE GeneralisedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# OPTIONS_GHC -fspecialize-aggressively #-} | ||
module Test.Database.LSMTree.Internal.Snapshot.Codec.Golden | ||
(goldenFileTests) where | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.ST.Strict (ST, runST) | ||
import Data.Foldable (fold) | ||
import Data.STRef | ||
import Data.Vector (Vector) | ||
import qualified Data.Vector as V | ||
import Database.LSMTree.Common (BloomFilterAlloc (..), | ||
DiskCachePolicy (..), NumEntries (..), TableConfig (..), | ||
WriteBufferAlloc (..), defaultTableConfig) | ||
import Database.LSMTree.Internal.MergingRun (NumRuns (..)) | ||
import Database.LSMTree.Internal.RunNumber (RunNumber (..)) | ||
import Database.LSMTree.Internal.Snapshot | ||
import Database.LSMTree.Internal.Snapshot.Codec | ||
import qualified System.FS.API as FS | ||
import System.FS.API.Types (FsPath, MountPoint (..), fsToFilePath, | ||
mkFsPath, (<.>)) | ||
import System.FS.IO (HandleIO, ioHasFS) | ||
import qualified Test.Tasty as Tasty | ||
import Test.Tasty (TestName, TestTree, testGroup) | ||
import qualified Test.Tasty.Golden as Au | ||
import Text.Printf (printf) | ||
|
||
|
||
-- | | ||
-- Compare the serialization of snapshot metadata with a known reference file. | ||
goldenFileTests :: TestTree | ||
goldenFileTests = handleOutputFiles . testGroup "Golden File Comparisons" $ | ||
[ testCodecSnapshotLabel | ||
, testCodecSnapshotTableType | ||
, testCodecTableConfig | ||
, testCodecSnapLevels | ||
] | ||
|
||
-- | | ||
-- The mount point is defined as the location of the golden file data directory | ||
-- relative to the project root. | ||
goldenDataMountPoint :: MountPoint | ||
goldenDataMountPoint = MountPoint "test/golden-file-data/snapshot-codec" | ||
|
||
-- | | ||
-- Delete output files on test-case success. | ||
-- Change the option here if this is undesireable. | ||
handleOutputFiles :: TestTree -> TestTree | ||
handleOutputFiles = Tasty.localOption Au.OnPass | ||
|
||
-- | | ||
-- | ||
-- Internally, the function will infer the correct filepath names. | ||
-- | ||
-- There are three paths for both the checksum and the snapshot files: | ||
-- 1. The filepath of type @FsPath@ to which data is written. | ||
-- 2. The filepath of type @FilePath@ from which data is read. | ||
-- 3. The filepath of type @FilePath@ against which the data is compared. | ||
-- | ||
-- These file types' bindings have the following infix annotations, respectively: | ||
-- 1. (Fs) for FsPath | ||
-- 2. (Hs) for "Haskell" path | ||
-- 3. (Au) for "Golden file" path | ||
snapshotCodecTest | ||
:: String -- ^ Name of the test | ||
-> SnapshotMetaData -- ^ Data to be serialized | ||
-> TestTree | ||
snapshotCodecTest name datum = | ||
let -- Various paths | ||
checksumFsPath = mkFsPath [name] <.> "checksum" | ||
snapshotFsPath = mkFsPath [name] <.> "snapshot" | ||
snapshotHsPath = fsToFilePath goldenDataMountPoint snapshotFsPath | ||
snapshotAuPath = snapshotHsPath <> ".golden" | ||
|
||
-- IO actions | ||
tidyUpAction :: a -> IO () | ||
tidyUpAction = const $ removeIfExists checksumFsPath | ||
runnerIO :: FS.HasFS IO HandleIO | ||
runnerIO = ioHasFS goldenDataMountPoint | ||
removeIfExists :: FsPath -> IO () | ||
removeIfExists fp = | ||
FS.doesFileExist runnerIO fp >>= (`when` (FS.removeFile runnerIO fp)) | ||
outputAction :: IO () | ||
outputAction = do | ||
removeIfExists snapshotFsPath | ||
removeIfExists checksumFsPath | ||
writeFileSnapshotMetaData | ||
runnerIO | ||
snapshotFsPath | ||
checksumFsPath | ||
datum | ||
|
||
-- File comparison test-cases against authoritative "golden" files | ||
testCaseSnapshot :: TestTree | ||
testCaseSnapshot = Au.goldenVsFile name snapshotAuPath snapshotHsPath outputAction | ||
|
||
-- Be sure to use Tasty's @withResource@ constructor to ensure that the | ||
-- codec serialization occurs only /once/ and extranerous "checksum" files | ||
-- are cleaned up after the test case. | ||
in Tasty.withResource outputAction tidyUpAction $ \tok -> | ||
tok `seq` testCaseSnapshot | ||
|
||
testCodecSnapshotLabel :: TestTree | ||
testCodecSnapshotLabel = | ||
let assembler label = | ||
SnapshotMetaData | ||
label | ||
basicSnapshotTableType | ||
basicTableConfig | ||
basicSnapLevels | ||
in testCodecBuilder "SnapshotLabel" $ assembler <$> enumerateSnapshotLabel | ||
|
||
testCodecSnapshotTableType :: TestTree | ||
testCodecSnapshotTableType = | ||
let assembler tType = | ||
SnapshotMetaData | ||
basicSnapshotLabel | ||
tType | ||
basicTableConfig | ||
basicSnapLevels | ||
in testCodecBuilder "SnapshotTableType" $ assembler <$> enumerateSnapshotTableType | ||
|
||
testCodecTableConfig :: TestTree | ||
testCodecTableConfig = | ||
let assembler tConfig = | ||
SnapshotMetaData | ||
basicSnapshotLabel | ||
basicSnapshotTableType | ||
tConfig | ||
basicSnapLevels | ||
in testCodecBuilder "TableConfig" $ assembler <$> enumerateTableConfig | ||
|
||
testCodecSnapLevels :: TestTree | ||
testCodecSnapLevels = | ||
let assembler levels = | ||
SnapshotMetaData | ||
basicSnapshotLabel | ||
basicSnapshotTableType | ||
basicTableConfig | ||
levels | ||
in testCodecBuilder "SnapLevels" $ assembler <$> enumerateSnapLevels | ||
|
||
testCodecBuilder :: TestName -> [SnapshotMetaData] -> TestTree | ||
testCodecBuilder tName metadata = | ||
let finalizer :: STRef s Word -> SnapshotMetaData -> ST s TestTree | ||
finalizer ref datum = do | ||
curr <- readSTRef ref | ||
modifySTRef' ref succ | ||
pure $ snapshotCodecTest (tName <> printf "-%03d" curr) datum | ||
|
||
testSubtree = runST $ do | ||
counter <- newSTRef 0 | ||
traverse (finalizer counter) metadata | ||
|
||
in testGroup tName testSubtree | ||
|
||
{---------------- | ||
Defaults used when the SnapshotMetaData sub-component is not under test | ||
----------------} | ||
|
||
basicSnapshotLabel :: SnapshotLabel | ||
basicSnapshotLabel = head enumerateSnapshotLabel | ||
|
||
basicSnapshotTableType :: SnapshotTableType | ||
basicSnapshotTableType = minBound | ||
|
||
basicTableConfig :: TableConfig | ||
basicTableConfig = defaultTableConfig | ||
|
||
basicSnapLevels :: SnapLevels RunNumber | ||
basicSnapLevels = head enumerateSnapLevels | ||
|
||
{---------------- | ||
Enumeration of SnapshotMetaData sub-components | ||
----------------} | ||
|
||
enumerateSnapshotLabel :: [SnapshotLabel] | ||
enumerateSnapshotLabel = [ SnapshotLabel "UserProvidedLabel", SnapshotLabel "" ] | ||
|
||
enumerateSnapshotTableType :: [SnapshotTableType] | ||
enumerateSnapshotTableType = [minBound .. maxBound] | ||
|
||
enumerateTableConfig :: [TableConfig] | ||
enumerateTableConfig = | ||
[ TableConfig | ||
policy | ||
ratio | ||
allocs | ||
bloom | ||
fence | ||
cache | ||
merge | ||
| policy <- [minBound .. maxBound] | ||
, ratio <- [minBound .. maxBound] | ||
, allocs <- AllocNumEntries . NumEntries <$> [magicNumber1] | ||
, bloom <- enumerateBloomFilterAlloc | ||
, fence <- [minBound .. maxBound] | ||
, cache <- enumerateDiskCachePolicy | ||
, merge <- [minBound .. maxBound] | ||
] | ||
|
||
enumerateSnapLevels :: [SnapLevels RunNumber] | ||
enumerateSnapLevels = SnapLevels . V.singleton <$> enumerateSnapLevel | ||
|
||
{---------------- | ||
Enumeration of SnapshotMetaData sub-sub-components and so on... | ||
----------------} | ||
|
||
enumerateBloomFilterAlloc :: [BloomFilterAlloc] | ||
enumerateBloomFilterAlloc = | ||
[ AllocFixed magicNumber3 | ||
, AllocRequestFPR pi | ||
, AllocMonkey magicNumber3 . NumEntries $ magicNumber3 `div` 4 | ||
] | ||
|
||
enumerateDiskCachePolicy :: [DiskCachePolicy] | ||
enumerateDiskCachePolicy = | ||
[ DiskCacheAll | ||
, DiskCacheNone | ||
, DiskCacheLevelsAtOrBelow 1 | ||
] | ||
|
||
enumerateSnapLevel :: [SnapLevel RunNumber] | ||
enumerateSnapLevel = SnapLevel <$> enumerateSnapIncomingRun <*> enumerateVectorRunNumber | ||
|
||
enumerateSnapIncomingRun :: [SnapIncomingRun RunNumber] | ||
enumerateSnapIncomingRun = | ||
let | ||
inSnaps = (SnapSingleRun <$> enumerateRunNumbers) <> | ||
[ SnapMergingRun policy numRuns entries credits sState | ||
| policy <- [minBound .. maxBound] | ||
, numRuns <- NumRuns <$> [ magicNumber1 ] | ||
, entries <- NumEntries <$> [ magicNumber2 ] | ||
, credits <- UnspentCredits <$> [ magicNumber1 ] | ||
, sState <- enumerateSnapMergingRunState | ||
] | ||
in fold | ||
[ SnapSingleRun <$> enumerateRunNumbers | ||
, inSnaps | ||
] | ||
|
||
enumerateSnapMergingRunState :: [SnapMergingRunState RunNumber] | ||
enumerateSnapMergingRunState = (SnapCompletedMerge <$> enumerateRunNumbers) <> | ||
[ SnapOngoingMerge runVec credits level | ||
| runVec <- enumerateVectorRunNumber | ||
, credits <- SpentCredits <$> [ magicNumber1] | ||
, level <- [minBound .. maxBound] | ||
] | ||
|
||
enumerateVectorRunNumber :: [Vector RunNumber] | ||
enumerateVectorRunNumber = | ||
[ mempty | ||
, V.fromList [RunNumber magicNumber1] | ||
, V.fromList [RunNumber magicNumber1, RunNumber magicNumber2 ] | ||
] | ||
|
||
enumerateRunNumbers :: [RunNumber] | ||
enumerateRunNumbers = RunNumber <$> [ magicNumber2 ] | ||
|
||
-- Randomly chosen numbers | ||
magicNumber1, magicNumber2, magicNumber3 :: Enum e => e | ||
magicNumber1 = toEnum 42 | ||
magicNumber2 = toEnum 88 | ||
magicNumber3 = toEnum 1024 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+45 Bytes
test/golden-file-data/snapshot-codec/SnapshotLabel-000.snapshot.golden
Binary file not shown.
Binary file added
BIN
+28 Bytes
test/golden-file-data/snapshot-codec/SnapshotLabel-001.snapshot.golden
Binary file not shown.
Binary file added
BIN
+45 Bytes
test/golden-file-data/snapshot-codec/SnapshotTableType-000.snapshot.golden
Binary file not shown.
Binary file added
BIN
+45 Bytes
test/golden-file-data/snapshot-codec/SnapshotTableType-001.snapshot.golden
Binary file not shown.
Binary file added
BIN
+45 Bytes
test/golden-file-data/snapshot-codec/SnapshotTableType-002.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-000.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-001.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-002.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-003.snapshot.golden
Binary file not shown.
Binary file added
BIN
+47 Bytes
test/golden-file-data/snapshot-codec/TableConfig-004.snapshot.golden
Binary file not shown.
Binary file added
BIN
+47 Bytes
test/golden-file-data/snapshot-codec/TableConfig-005.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-006.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-007.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-008.snapshot.golden
Binary file not shown.
Binary file added
BIN
+46 Bytes
test/golden-file-data/snapshot-codec/TableConfig-009.snapshot.golden
Binary file not shown.
Binary file added
BIN
+47 Bytes
test/golden-file-data/snapshot-codec/TableConfig-010.snapshot.golden
Binary file not shown.
Binary file added
BIN
+47 Bytes
test/golden-file-data/snapshot-codec/TableConfig-011.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-012.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-013.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-014.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-015.snapshot.golden
Binary file not shown.
Binary file added
BIN
+53 Bytes
test/golden-file-data/snapshot-codec/TableConfig-016.snapshot.golden
Binary file not shown.
Binary file added
BIN
+53 Bytes
test/golden-file-data/snapshot-codec/TableConfig-017.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-018.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-019.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-020.snapshot.golden
Binary file not shown.
Binary file added
BIN
+52 Bytes
test/golden-file-data/snapshot-codec/TableConfig-021.snapshot.golden
Binary file not shown.
Binary file added
BIN
+53 Bytes
test/golden-file-data/snapshot-codec/TableConfig-022.snapshot.golden
Binary file not shown.
Binary file added
BIN
+53 Bytes
test/golden-file-data/snapshot-codec/TableConfig-023.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-024.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-025.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-026.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-027.snapshot.golden
Binary file not shown.
Binary file added
BIN
+50 Bytes
test/golden-file-data/snapshot-codec/TableConfig-028.snapshot.golden
Binary file not shown.
Binary file added
BIN
+50 Bytes
test/golden-file-data/snapshot-codec/TableConfig-029.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-030.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-031.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-032.snapshot.golden
Binary file not shown.
Binary file added
BIN
+49 Bytes
test/golden-file-data/snapshot-codec/TableConfig-033.snapshot.golden
Binary file not shown.
Binary file added
BIN
+50 Bytes
test/golden-file-data/snapshot-codec/TableConfig-034.snapshot.golden
Binary file not shown.
Binary file added
BIN
+50 Bytes
test/golden-file-data/snapshot-codec/TableConfig-035.snapshot.golden
Binary file not shown.