@@ -7,8 +7,11 @@ import Test.Hydra.Prelude
7
7
8
8
import Data.Aeson (Value (.. ))
9
9
import Data.Aeson qualified as Aeson
10
+ import Data.ByteString qualified as BS
10
11
import Data.Text qualified as Text
12
+ import Hydra.Logging (Verbosity (Verbose ), traceWith , withTracer , withTracerOutputTo )
11
13
import Hydra.Persistence (Persistence (.. ), PersistenceIncremental (.. ), createPersistence , createPersistenceIncremental , loadAll )
14
+ import Hydra.PersistenceLog
12
15
import Test.QuickCheck (checkCoverage , cover , elements , oneof , suchThat , (===) )
13
16
import Test.QuickCheck.Gen (listOf )
14
17
import Test.QuickCheck.Monadic (monadicIO , monitor , pick , run )
@@ -35,12 +38,26 @@ spec = do
35
38
pure $ actualResult === Just item
36
39
37
40
describe " PersistenceIncremental" $ do
38
- it " can handle empty files " $ do
41
+ it " can ignore invalid lines and emits warning " $ do
39
42
withTempDir " hydra-persistence" $ \ tmpDir -> do
40
- let fp = tmpDir <> " /data"
41
- writeFileBS fp " "
42
- p <- createPersistenceIncremental fp
43
- loadAll p `shouldReturn` ([] :: [Aeson. Value ])
43
+ let logFile = tmpDir <> " /tracer.log"
44
+ withFile logFile WriteMode $ \ hdl -> do
45
+ withTracerOutputTo hdl " persistence-incremental" $ \ tracer -> do
46
+ let fp = tmpDir <> " /data"
47
+ writeFileBS fp " \" abc\"\n {\" xyz\" : "
48
+ -- traceWith tracer $ FailedToDecodeJson{reason = "show e", filepath = "fp", contents = "show bs"}
49
+ p <- createPersistenceIncremental tracer fp
50
+ loadAll p `shouldReturn` ([Aeson. String " abc" ] :: [Aeson. Value ])
51
+ logs <- readFileBS logFile
52
+ logs `shouldSatisfy` BS. isInfixOf " FailedToDecodeJson"
53
+
54
+ it " can handle empty files" $ do
55
+ withTracer (Verbose " persistence-incremental" ) $ \ tracer -> do
56
+ withTempDir " hydra-persistence" $ \ tmpDir -> do
57
+ let fp = tmpDir <> " /data"
58
+ writeFileBS fp " "
59
+ p <- createPersistenceIncremental tracer fp
60
+ loadAll p `shouldReturn` ([] :: [Aeson. Value ])
44
61
45
62
it " is consistent after multiple append calls in presence of new-lines" $
46
63
checkCoverage $
@@ -50,24 +67,26 @@ spec = do
50
67
monitor (cover 10 (containsNewLine items) " some item contains a new line" )
51
68
52
69
actualResult <- run $
53
- withTempDir " hydra-persistence" $ \ tmpDir -> do
54
- p <- createPersistenceIncremental $ tmpDir <> " /data"
55
- forM_ items $ append p
56
- loadAll p
70
+ withTracer (Verbose " persistence-incremental" ) $ \ tracer -> do
71
+ withTempDir " hydra-persistence" $ \ tmpDir -> do
72
+ p <- createPersistenceIncremental tracer $ tmpDir <> " /data"
73
+ forM_ items $ append p
74
+ loadAll p
57
75
pure $ actualResult === items
58
76
59
77
it " it cannot load from a different thread once having started appending" $
60
78
monadicIO $ do
61
79
items <- pick $ listOf genPersistenceItem
62
80
moreItems <- pick $ listOf genPersistenceItem `suchThat` ((> 2 ) . length )
63
81
pure $
64
- withTempDir " hydra-persistence" $ \ tmpDir -> do
65
- p <- createPersistenceIncremental $ tmpDir <> " /data"
66
- forM_ items $ append p
67
- loadAll p `shouldReturn` items
68
- raceLabelled_
69
- (" forever-load-all" , forever $ threadDelay 0.01 >> loadAll p)
70
- (" append-more-items" , forM_ moreItems $ \ item -> append p item >> threadDelay 0.01 )
82
+ withTracer (Verbose " persistence-incremental" ) $ \ tracer -> do
83
+ withTempDir " hydra-persistence" $ \ tmpDir -> do
84
+ p <- createPersistenceIncremental tracer $ tmpDir <> " /data"
85
+ forM_ items $ append p
86
+ loadAll p `shouldReturn` items
87
+ raceLabelled_
88
+ (" forever-load-all" , forever $ threadDelay 0.01 >> loadAll p)
89
+ (" append-more-items" , forM_ moreItems $ \ item -> append p item >> threadDelay 0.01 )
71
90
72
91
genPersistenceItem :: Gen Aeson. Value
73
92
genPersistenceItem =
0 commit comments