Skip to content

Commit 948d3a2

Browse files
authored
Merge pull request IntersectMBO#5332 from input-output-hk/mgalazyn/feature/test-sigint-handling
Test SIGINT handling
2 parents ceb7c6e + d82ad66 commit 948d3a2

File tree

6 files changed

+106
-95
lines changed

6 files changed

+106
-95
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repository cardano-haskell-packages
1313
-- See CONTRIBUTING for information about these, including some Nix commands
1414
-- you need to run if you change them
1515
index-state:
16-
, hackage.haskell.org 2023-05-10T10:34:57Z
16+
, hackage.haskell.org 2023-06-20T00:00:00Z
1717
, cardano-haskell-packages 2023-06-15T12:05:43Z
1818

1919
packages:

cardano-testnet/cardano-testnet.cabal

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ library
5151
, exceptions
5252
, filepath
5353
, hedgehog
54-
, hedgehog-extras ^>= 0.4.5.1
54+
, hedgehog-extras ^>= 0.4.6.0
5555
, mtl
5656
, optparse-applicative-fork
5757
, ouroboros-network ^>= 0.8.1.0
@@ -86,7 +86,7 @@ library
8686
Testnet.Process.Cli
8787
Testnet.Process.Run
8888
Testnet.Runtime
89-
Testnet.Topology
89+
Testnet.Topology
9090

9191
other-modules: Parsers.Babbage
9292
Parsers.Byron
@@ -138,7 +138,7 @@ test-suite cardano-testnet-golden
138138
, exceptions
139139
, filepath
140140
, hedgehog
141-
, hedgehog-extras ^>= 0.4.5.1
141+
, hedgehog-extras ^>= 0.4.6.0
142142
, process
143143
, regex-compat
144144
, tasty
@@ -169,7 +169,6 @@ test-suite cardano-testnet-test
169169
Cardano.Testnet.Test.FoldBlocks
170170
Cardano.Testnet.Test.Misc
171171
Cardano.Testnet.Test.Node.Shutdown
172-
Cardano.Testnet.Test.ShutdownOnSlotSynced
173172

174173
type: exitcode-stdio-1.0
175174

@@ -184,7 +183,7 @@ test-suite cardano-testnet-test
184183
, directory
185184
, filepath
186185
, hedgehog
187-
, hedgehog-extras ^>= 0.4.5.1
186+
, hedgehog-extras ^>= 0.4.6.0
188187
, process
189188
, tasty
190189
, text

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Node/Shutdown.hs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{-# LANGUAGE DisambiguateRecordFields #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
45
{-# LANGUAGE TypeApplications #-}
56

67
module Cardano.Testnet.Test.Node.Shutdown
78
( hprop_shutdown
9+
, hprop_shutdownOnSlotSynced
10+
, hprop_shutdownOnSigint
811
) where
912

1013
import Cardano.Api
1114
import Control.Monad
1215
import Data.Aeson
16+
import Data.Aeson.Types
1317
import Data.Bifunctor
14-
import qualified Data.ByteString.Lazy as LBS
18+
import qualified Data.ByteString.Lazy.Char8 as LBS
1519
import Data.Functor ((<&>))
1620
import qualified Data.List as L
1721
import Data.Maybe
@@ -34,9 +38,14 @@ import qualified System.Process as IO
3438
import qualified Testnet.Property.Utils as H
3539

3640
import Cardano.Testnet
41+
import Data.Either (isRight)
42+
import GHC.IO.Exception (ExitCode (ExitSuccess, ExitFailure))
43+
import GHC.Stack (callStack)
44+
import System.Process (interruptProcessGroupOf)
3745
import Testnet.Defaults
3846
import Testnet.Process.Run (execCli_, procNode)
3947
import Testnet.Property.Utils
48+
import Testnet.Runtime
4049
import Testnet.Start.Byron
4150
import Testnet.Topology
4251

@@ -160,3 +169,89 @@ hprop_shutdown = H.integrationRetryWorkspace 2 "shutdown" $ \tempAbsBasePath' ->
160169
mExitCode === Just IO.ExitSuccess
161170

162171
return ()
172+
173+
hprop_shutdownOnSlotSynced :: Property
174+
hprop_shutdownOnSlotSynced = H.integrationRetryWorkspace 2 "shutdown-on-slot-synced" $ \tempAbsBasePath' -> do
175+
-- Start a local test net
176+
-- TODO: Move yaml filepath specification into individual node options
177+
conf <- H.noteShowM $ mkConf tempAbsBasePath'
178+
179+
let maxSlot = 1500
180+
slotLen = 0.01
181+
let fastTestnetOptions = CardanoOnlyTestnetOptions $ cardanoDefaultTestnetOptions
182+
{ cardanoEpochLength = 300
183+
, cardanoSlotLength = slotLen
184+
, cardanoNodes =
185+
[ BftTestnetNodeOptions ["--shutdown-on-slot-synced", show maxSlot]
186+
, BftTestnetNodeOptions []
187+
, SpoTestnetNodeOptions
188+
]
189+
}
190+
TestnetRuntime { bftNodes = node:_ } <- Cardano.Testnet.testnet fastTestnetOptions conf
191+
192+
-- Wait for the node to exit
193+
let timeout :: Int
194+
timeout = round (40 + (fromIntegral maxSlot * slotLen))
195+
mExitCodeRunning <- H.waitSecondsForProcess timeout (nodeProcessHandle node)
196+
197+
-- Check results
198+
when (isRight mExitCodeRunning) $ do
199+
H.cat (nodeStdout node)
200+
H.cat (nodeStderr node)
201+
mExitCodeRunning === Right ExitSuccess
202+
203+
logs <- H.readFile (nodeStdout node)
204+
slotTip <- case mapMaybe parseMsg $ reverse $ lines logs of
205+
[] -> H.failMessage callStack "Could not find close DB message."
206+
(Left err):_ -> H.failMessage callStack err
207+
(Right s):_ -> return s
208+
209+
let epsilon = 50
210+
211+
H.assert (maxSlot <= slotTip && slotTip <= maxSlot + epsilon)
212+
213+
hprop_shutdownOnSigint :: Property
214+
hprop_shutdownOnSigint = H.integrationRetryWorkspace 2 "shutdown-on-sigint" $ \tempAbsBasePath' -> do
215+
-- Start a local test net
216+
-- TODO: Move yaml filepath specification into individual node options
217+
conf <- H.noteShowM $ mkConf tempAbsBasePath'
218+
219+
let fastTestnetOptions = CardanoOnlyTestnetOptions $ cardanoDefaultTestnetOptions
220+
{ cardanoEpochLength = 300
221+
, cardanoSlotLength = 0.01
222+
}
223+
TestnetRuntime { bftNodes = node@NodeRuntime{nodeProcessHandle}:_ }
224+
<- Cardano.Testnet.testnet fastTestnetOptions conf
225+
226+
-- send SIGINT
227+
H.evalIO $ interruptProcessGroupOf nodeProcessHandle
228+
229+
-- Wait for the node to exit
230+
mExitCodeRunning <- H.waitSecondsForProcess 5 nodeProcessHandle
231+
232+
-- Check results
233+
when (isRight mExitCodeRunning) $ do
234+
H.cat (nodeStdout node)
235+
H.cat (nodeStderr node)
236+
mExitCodeRunning === Right (ExitFailure 1)
237+
238+
logs <- H.readFile (nodeStdout node)
239+
case mapMaybe parseMsg $ reverse $ lines logs of
240+
[] -> H.failMessage callStack "Could not find close DB message."
241+
(Left err):_ -> H.failMessage callStack err
242+
(Right _):_ -> pure ()
243+
244+
245+
parseMsg :: String -> Maybe (Either String Integer)
246+
parseMsg line = case decode $ LBS.pack line of
247+
Nothing -> Just $ Left $ "Expected JSON formated log message, but got: " ++ line
248+
Just obj -> Right <$> parseMaybe parseTipSlot obj
249+
250+
parseTipSlot :: Object -> Parser Integer
251+
parseTipSlot obj = do
252+
body <- obj .: "data"
253+
tip <- body .: "tip"
254+
kind <- body .: "kind"
255+
if kind == ("TraceOpenEvent.ClosedDB" :: String)
256+
then tip .: "slot"
257+
else mzero

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/ShutdownOnSlotSynced.hs

Lines changed: 0 additions & 83 deletions
This file was deleted.

cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import qualified Cardano.Testnet.Test.Cli.KesPeriodInfo
1515
import qualified Cardano.Testnet.Test.Cli.QuerySlotNumber
1616
import qualified Cardano.Testnet.Test.FoldBlocks
1717
import qualified Cardano.Testnet.Test.Node.Shutdown
18-
import qualified Cardano.Testnet.Test.ShutdownOnSlotSynced
1918
import qualified System.Environment as E
2019
import qualified Test.Tasty as T
2120
import qualified Test.Tasty.Ingredients as T
@@ -28,7 +27,8 @@ tests :: IO TestTree
2827
tests = pure $ T.testGroup "test/Spec.hs"
2928
[ T.testGroup "Spec"
3029
[ H.ignoreOnWindows "Shutdown" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdown
31-
, H.ignoreOnWindows "ShutdownOnSlotSynced" Cardano.Testnet.Test.ShutdownOnSlotSynced.hprop_shutdownOnSlotSynced
30+
, H.ignoreOnWindows "ShutdownOnSigint" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdownOnSigint
31+
, H.ignoreOnWindows "ShutdownOnSlotSynced" Cardano.Testnet.Test.Node.Shutdown.hprop_shutdownOnSlotSynced
3232
-- TODO: This is failing. Disabling until we can figure out why
3333
-- , T.testGroup "Alonzo"
3434
-- [ H.ignoreOnMacAndWindows "leadership-schedule" Cardano.Testnet.Test.Cli.Alonzo.LeadershipSchedule.hprop_leadershipSchedule

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)