Skip to content

Commit f7280e5

Browse files
committed
Remove MonadTest constraint from startNode and startLedgerNewEpochStateLogging
1 parent 5d352bf commit f7280e5

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

cardano-testnet/src/Testnet/Runtime.hs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ import GHC.Stack
4343
import qualified GHC.Stack as GHC
4444
import Network.Socket (HostAddress, PortNumber)
4545
import Prettyprinter (unAnnotate)
46+
import RIO (runRIO)
4647
import qualified System.Directory as IO
4748
import System.FilePath
4849
import qualified System.IO as IO
4950
import qualified System.Process as IO
5051

5152
import Testnet.Filepath
5253
import qualified Testnet.Ping as Ping
53-
import Testnet.Process.Run
54+
import Testnet.Process.Run (ProcessError (..), initiateProcess)
55+
import Testnet.Process.RunIO (procNode, liftIOAnnotated)
5456
import Testnet.Types (TestnetNode (..), TestnetRuntime (configurationFile),
5557
showIpv4Address, testnetSprockets)
5658

57-
import Hedgehog (MonadTest)
58-
import qualified Hedgehog as H
5959
import Hedgehog.Extras.Stock.IO.Network.Sprocket (Sprocket (..))
6060
import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as H
61-
import qualified Hedgehog.Extras.Test.Base as H
6261
import qualified Hedgehog.Extras.Test.Concurrent as H
6362

6463
data NodeStartFailure
@@ -105,7 +104,6 @@ startNode
105104
=> MonadResource m
106105
=> MonadCatch m
107106
=> MonadFail m
108-
=> MonadTest m
109107
=> TmpAbsolutePath
110108
-- ^ The temporary absolute path
111109
-> String
@@ -151,14 +149,14 @@ startNode tp node ipv4 port _testnetMagic nodeCmd = GHC.withFrozenCallStack $ do
151149
, "--port", show port
152150
, "--host-addr", showIpv4Address ipv4
153151
]
154-
155-
nodeProcess <- newExceptT . fmap (first ExecutableRelatedFailure) . try $ procNode completeNodeCmd
152+
nodeProcess <- newExceptT . fmap (first ExecutableRelatedFailure) . try $ runRIO () $ procNode completeNodeCmd
156153

157154
-- The port number if it is obtained using 'H.randomPort', it is firstly bound to and then closed. The closing
158155
-- and release in the operating system is done asynchronously and can be slow. Here we wait until the port
159-
-- is out of CLOSING state.
160-
H.note_ $ "Waiting for port " <> show port <> " to be available before starting node"
161-
H.assertM $ Ping.waitForPortClosed 30 0.1 port
156+
157+
isClosed <- liftIOAnnotated $ Ping.waitForPortClosed 30 0.1 port
158+
unless isClosed $
159+
throwString $ "Port is still in use after 30 seconds before starting node: " <> show port
162160

163161
(Just stdIn, _, _, hProcess, _)
164162
<- firstExceptT ProcessRelatedFailure $ initiateProcess
@@ -175,18 +173,18 @@ startNode tp node ipv4 port _testnetMagic nodeCmd = GHC.withFrozenCallStack $ do
175173
>>= hoistMaybe (NodeExecutableError $ "startNode:" <+> pretty node <+> "'s process did not start.")
176174

177175
-- We then log the pid in the temp dir structure.
178-
liftIO $ IO.writeFile nodePidFile $ show pid
176+
liftIOAnnotated $ IO.writeFile nodePidFile $ show pid
179177

180178
-- Wait for socket to be created
181179
eSprocketError <-
182-
H.evalIO $
180+
liftIOAnnotated $
183181
Ping.waitForSprocket
184182
120 -- timeout
185183
0.2 -- check interval
186184
sprocket
187185

188186
-- If we do have anything on stderr, fail.
189-
stdErrContents <- liftIO $ IO.readFile nodeStderrFile
187+
stdErrContents <- liftIOAnnotated $ IO.readFile nodeStderrFile
190188
unless (null stdErrContents) $
191189
throwError $ mkNodeNonEmptyStderrError stdErrContents
192190

@@ -282,7 +280,6 @@ startLedgerNewEpochStateLogging
282280
:: HasCallStack
283281
=> MonadCatch m
284282
=> MonadResource m
285-
=> MonadTest m
286283
=> TestnetRuntime
287284
-> FilePath -- ^ tmp workspace directory
288285
-> m ()
@@ -292,29 +289,25 @@ startLedgerNewEpochStateLogging testnetRuntime tmpWorkspace = withFrozenCallStac
292289
logFile = logDir </> "ledger-epoch-state.log"
293290
diffFile = logDir </> "ledger-epoch-state-diffs.log"
294291

295-
H.evalIO (IO.doesDirectoryExist logDir) >>= \case
292+
liftIOAnnotated $ IO.doesDirectoryExist logDir >>= \case
296293
True -> pure ()
297294
False -> do
298-
H.note_ $ "Log directory does not exist: " <> logDir <> " - cannot start logging epoch states"
299-
H.failure
300-
301-
H.evalIO (IO.doesFileExist logFile) >>= \case
302-
True -> do
303-
H.note_ $ "Epoch states logging to " <> logFile <> " is already started."
304-
False -> do
305-
H.evalIO $ appendFile logFile ""
306-
socketPath <- H.noteM $ H.sprocketSystemName <$> H.headM (testnetSprockets testnetRuntime)
307-
308-
_ <- H.asyncRegister_ . runExceptT $
309-
foldEpochState
310-
(configurationFile testnetRuntime)
311-
(Api.File socketPath)
312-
Api.QuickValidation
313-
(EpochNo maxBound)
314-
Nothing
315-
(handler logFile diffFile)
316-
317-
H.note_ $ "Started logging epoch states to: " <> logFile <> "\nEpoch state diffs are logged to: " <> diffFile
295+
throwString $ "Log directory does not exist: " <> logDir <> " - cannot start logging epoch states"
296+
297+
liftIOAnnotated $ IO.doesFileExist logFile >>= \case
298+
True -> return ()
299+
False -> liftIO $ appendFile logFile ""
300+
301+
let socketPath = H.sprocketSystemName $ head (testnetSprockets testnetRuntime)
302+
let act = runExceptT $
303+
foldEpochState
304+
(configurationFile testnetRuntime)
305+
(Api.File socketPath)
306+
Api.QuickValidation
307+
(EpochNo maxBound)
308+
Nothing
309+
(handler logFile diffFile)
310+
void $ asyncRegister_ act
318311
where
319312
handler :: FilePath -- ^ log file
320313
-> FilePath -- ^ diff file

0 commit comments

Comments
 (0)