Skip to content

Commit 81d729e

Browse files
committed
Capture trace output with traceInTVar
1 parent 0e8b108 commit 81d729e

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ changes.
4242
- **BREAKING** Enable handling client recover in all head states.
4343
- See [Issue #1812](https://github.com/cardano-scaling/hydra/issues/1812) and [PR #2217](https://github.com/cardano-scaling/hydra/pull/2217).
4444

45+
- Optimistic approach to statefile corruption by just ignoring invalid JSON
46+
lines [#2253](https://github.com/cardano-scaling/hydra/issues/2253)
47+
4548
## [0.22.4] - 2025-08-05
4649

4750
- Fix API not correctly handling event log rotation. This was evident in not

hydra-node/test/Hydra/PersistenceSpec.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import Test.Hydra.Prelude
77

88
import Data.Aeson (Value (..))
99
import Data.Aeson qualified as Aeson
10-
import Data.ByteString qualified as BS
1110
import Data.Text qualified as Text
12-
import Hydra.Logging (Verbosity (Verbose), traceWith, withTracer, withTracerOutputTo)
11+
import Hydra.Logging (Envelope (..), Verbosity (Verbose), withTracer)
1312
import Hydra.Persistence (Persistence (..), PersistenceIncremental (..), createPersistence, createPersistenceIncremental, loadAll)
1413
import Hydra.PersistenceLog
1514
import Test.QuickCheck (checkCoverage, cover, elements, oneof, suchThat, (===))
1615
import Test.QuickCheck.Gen (listOf)
1716
import Test.QuickCheck.Monadic (monadicIO, monitor, pick, run)
17+
import Test.Util (captureTracer)
1818

1919
spec :: Spec
2020
spec = do
@@ -40,16 +40,15 @@ spec = do
4040
describe "PersistenceIncremental" $ do
4141
it "can ignore invalid lines and emits warning" $ do
4242
withTempDir "hydra-persistence" $ \tmpDir -> do
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"
43+
(tracer, getTraces) <- captureTracer "persistence-incremental"
44+
let fp = tmpDir <> "/data"
45+
writeFileBS fp "\"abc\"\n{\"xyz\": "
46+
p <- createPersistenceIncremental tracer fp
47+
loadAll p `shouldReturn` ([Aeson.String "abc"] :: [Aeson.Value])
48+
traces <- getTraces
49+
let rightMsg [Envelope{message = FailedToDecodeJson{}}] = True
50+
rightMsg _ = False
51+
traces `shouldSatisfy` rightMsg
5352

5453
it "can handle empty files" $ do
5554
withTracer (Verbose "persistence-incremental") $ \tracer -> do

hydra-node/test/Test/Util.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Data.Aeson (encode)
2121
import Data.Aeson qualified as Aeson
2222
import Data.Text qualified as Text
2323
import Hydra.Ledger.Simple (SimpleTx)
24+
import Hydra.Logging (Envelope (..), traceInTVar)
2425
import Hydra.Network (NetworkCallback (..))
2526
import Hydra.Node (HydraNodeLog)
2627
import System.IO.Temp (writeSystemTempFile)
@@ -175,3 +176,13 @@ waitMatch waitNext delay match = do
175176

176177
align _ [] = []
177178
align n (h : q) = h : fmap (Text.replicate n " " <>) q
179+
180+
-- | Create a tracer that captures all messages and a function to retrieve all
181+
-- traces captured.
182+
-- XXX: This is duplicated in MithrilSpec in hydra-cluster, but can't (easily)
183+
-- be moved to the Test Prelude because of the dependency on Hydra.Logging.
184+
captureTracer :: Text -> IO (Tracer IO a, IO [Envelope a])
185+
captureTracer namespace = do
186+
traces <- newLabelledTVarIO "capture-tracer" []
187+
let tracer = traceInTVar traces namespace
188+
pure (tracer, readTVarIO traces)

0 commit comments

Comments
 (0)