Skip to content

Commit

Permalink
Enable parallel execution of tests. Add semaphore
Browse files Browse the repository at this point in the history
for golden files overwritten in multiple tests.
  • Loading branch information
carbolymer committed Mar 8, 2024
1 parent df5c712 commit 104550b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
7 changes: 5 additions & 2 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ test-suite cardano-cli-test
Test.Cli.Pipes
Test.Cli.Shelley.Run.Query

ghc-options: -threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T
ghc-options: -threaded -rtsopts "-with-rtsopts=-N -T"

test-suite cardano-cli-golden
import: project-config
Expand Down Expand Up @@ -341,13 +341,16 @@ test-suite cardano-cli-golden
, filepath
, hedgehog ^>= 1.4
, hedgehog-extras ^>= 0.6.1.0
, lifted-base
, monad-control
, regex-compat
, regex-tdfa
, tasty
, tasty-hedgehog
, text
, time
, transformers
, transformers-base
, unordered-containers
build-tool-depends: cardano-cli:cardano-cli
, tasty-discover:tasty-discover
Expand Down Expand Up @@ -420,4 +423,4 @@ test-suite cardano-cli-golden
Test.Golden.TxView
Test.Golden.Version

ghc-options: -threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T
ghc-options: -threaded -rtsopts "-with-rtsopts=-N -T"
48 changes: 39 additions & 9 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/DRep.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{- HLINT ignore "Use camelCase" -}

module Test.Golden.Governance.DRep where
Expand All @@ -7,7 +8,13 @@ module Test.Golden.Governance.DRep where
#define UNIX
#endif

import Control.Concurrent (newQSem)
import Control.Concurrent.QSem (QSem, waitQSem, signalQSem)
import Control.Exception.Lifted (bracket_)
import Control.Monad
import Control.Monad.Base
import Control.Monad.Trans.Control (MonadBaseControl)
import System.IO.Unsafe (unsafePerformIO)

#ifdef UNIX
import Data.Bits ((.&.))
Expand All @@ -24,6 +31,18 @@ import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H

-- | Semaphore protecting against locked file error, when running properties concurrently.
-- This semaphore protects @"test/cardano-cli-golden/files/golden/governance/drep/drep_retirement_cert"@.
drepRetirementCertSem :: QSem
drepRetirementCertSem = unsafePerformIO $ newQSem 1
{-# NOINLINE drepRetirementCertSem #-}

-- | Semaphore protecting against locked file error, when running properties concurrently.
-- This semaphore protects @"test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"@.
drepRegistrationCertSem :: QSem
drepRegistrationCertSem = unsafePerformIO $ newQSem 1
{-# NOINLINE drepRegistrationCertSem #-}

hprop_golden_governanceDRepKeyGen :: Property
hprop_golden_governanceDRepKeyGen =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down Expand Up @@ -92,8 +111,8 @@ hprop_golden_governance_drep_extended_key_signing =
skeyFile <- noteInputFile "test/cardano-cli-golden/files/input/governance/drep/extended-key-signing/drep.skey"
txBody <- noteInputFile "test/cardano-cli-golden/files/input/governance/drep/extended-key-signing/tx.body"

outGold <- H.note "test/cardano-cli-golden/files/golden/governance/drep/extended-key-signing/tx.signed"
outFile <- H.noteTempFile tempDir "outFile"
outGold <- H.note "test/cardano-cli-golden/files/golden/governance/drep/extended-key-signing/tx.signed"

void $ execCardanoCLI
[ "conway", "transaction", "sign"
Expand All @@ -118,7 +137,8 @@ hprop_golden_governance_drep_retirement_certificate_vkey_file =
, "--out-file", certFile
]

H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile
bracketSem drepRetirementCertSem $
H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile

hprop_golden_governance_drep_retirement_certificate_id_hex :: Property
hprop_golden_governance_drep_retirement_certificate_id_hex =
Expand All @@ -135,7 +155,8 @@ hprop_golden_governance_drep_retirement_certificate_id_hex =
, "--out-file", certFile
]

H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile
bracketSem drepRetirementCertSem $
H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile

hprop_golden_governance_drep_retirement_certificate_id_bech32 :: Property
hprop_golden_governance_drep_retirement_certificate_id_bech32 =
Expand All @@ -152,7 +173,8 @@ hprop_golden_governance_drep_retirement_certificate_id_bech32 =
, "--out-file", certFile
]

H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile
bracketSem drepRetirementCertSem $
H.diffFileVsGoldenFile certFile goldenDRepRetirementCertFile

hprop_golden_governance_drep_metadata_hash :: Property
hprop_golden_governance_drep_metadata_hash = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down Expand Up @@ -187,12 +209,13 @@ hprop_golden_governance_drep_registration_certificate_vkey_file = propertyOnce .
, "--out-file", outFile
]

H.diffFileVsGoldenFile outFile goldenFile
bracketSem drepRegistrationCertSem $
H.diffFileVsGoldenFile outFile goldenFile

hprop_golden_governance_drep_registration_certificate_id_hex :: Property
hprop_golden_governance_drep_registration_certificate_id_hex = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
goldenFile <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"
idFile <- H.readFile "test/cardano-cli-golden/files/input/drep.id.hex"
goldenFile <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"

outFile <- H.noteTempFile tempDir "drep-reg-cert.txt"

Expand All @@ -205,12 +228,13 @@ hprop_golden_governance_drep_registration_certificate_id_hex = propertyOnce . H.
, "--out-file", outFile
]

H.diffFileVsGoldenFile outFile goldenFile
bracketSem drepRegistrationCertSem $
H.diffFileVsGoldenFile outFile goldenFile

hprop_golden_governance_drep_registration_certificate_id_bech32 :: Property
hprop_golden_governance_drep_registration_certificate_id_bech32 = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
goldenFile <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"
idFile <- H.readFile "test/cardano-cli-golden/files/input/drep.id.bech32"
goldenFile <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep_registration_certificate.json"

outFile <- H.noteTempFile tempDir "drep-reg-cert.txt"

Expand All @@ -223,7 +247,8 @@ hprop_golden_governance_drep_registration_certificate_id_bech32 = propertyOnce .
, "--out-file", outFile
]

H.diffFileVsGoldenFile outFile goldenFile
bracketSem drepRegistrationCertSem $
H.diffFileVsGoldenFile outFile goldenFile

hprop_golden_governance_drep_registration_certificate_script_hash :: Property
hprop_golden_governance_drep_registration_certificate_script_hash = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
Expand Down Expand Up @@ -260,3 +285,8 @@ hprop_golden_governance_drep_update_certificate_vkey_file = propertyOnce . H.mod
]

H.diffFileVsGoldenFile outFile goldenFile

-- | Run action acquiring a semaphore, and releasing afterwards. Allows to guard against concurrent access to
-- a block of code
bracketSem :: MonadBaseControl IO m => QSem -> m c -> m c
bracketSem semaphore = bracket_ (liftBase $ waitQSem semaphore) (liftBase $ signalQSem semaphore)

0 comments on commit 104550b

Please sign in to comment.