Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LineBuffering in unit tests #2723

Merged
merged 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/core-integration/src/Cardano/Wallet/BenchShared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ import System.Exit
( ExitCode (..), die )
import System.FilePath
( (</>) )
import System.IO
( BufferMode (..), hSetBuffering, stderr, stdout )
import Test.Utils.Startup
( withNoBuffering )
import UnliftIO.Concurrent
( threadDelay )
import UnliftIO.Exception
Expand All @@ -121,12 +121,9 @@ execBenchWithNode
-> (Trace IO Text -> cfg -> CardanoNodeConn -> IO ())
-- ^ Action to run
-> IO ExitCode
execBenchWithNode networkConfig action = do
execBenchWithNode networkConfig action = withNoBuffering $ do
args <- getRestoreBenchArgs

hSetBuffering stdout NoBuffering
hSetBuffering stderr NoBuffering

(_logCfg, tr') <- initBenchmarkLogging "bench-restore" Info
let tr = if argQuiet args then nullTracer else tr'
installSignalHandlers (return ())
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/Api/Server/TlsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import Test.Hspec
( Spec, describe, it, shouldBe, shouldThrow )
import Test.Utils.Paths
( getTestData )
import Test.Utils.Windows
import Test.Utils.Platform
( pendingOnWine )
import UnliftIO.Async
( async, link )
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/Api/ServerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import Test.QuickCheck.Monadic
( PropertyM, assert, monadicIO, monitor, run )
import Test.QuickCheck.Property
( counterexample, property )
import Test.Utils.Windows
import Test.Utils.Platform
( skipOnWindows )
import UnliftIO.Async
( concurrently_, race_ )
Expand Down
2 changes: 1 addition & 1 deletion lib/core/test/unit/Cardano/Wallet/DB/MVarSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Test.Hspec
( Spec, before, describe )
import Test.QuickCheck
( Arbitrary (..) )
import Test.Utils.Darwin
import Test.Utils.Platform
( pendingOnMacOS )

import qualified Cardano.Wallet.DB.MVar as MVar
Expand Down
11 changes: 8 additions & 3 deletions lib/core/test/unit/Main.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module Main where

import Prelude

import Cardano.Startup
( withUtf8Encoding )
import Prelude
import qualified Spec
import Test.Hspec.Runner
( defaultConfig, hspecWith )
import Test.Utils.Startup
( withLineBuffering )

import qualified Spec

main :: IO ()
main = withUtf8Encoding $ hspecWith defaultConfig Spec.spec
main = withLineBuffering $ withUtf8Encoding $ hspecWith defaultConfig Spec.spec
2 changes: 1 addition & 1 deletion lib/core/test/unit/Network/Wai/Middleware/LoggingSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ import Test.QuickCheck
( Arbitrary (..), choose, counterexample, property, withMaxSuccess )
import Test.QuickCheck.Monadic
( assert, monadicIO, monitor )
import Test.Utils.Darwin
import Test.Utils.Platform
( pendingOnMacOS )
import UnliftIO.Async
( Async, async, cancel, mapConcurrently, replicateConcurrently_ )
Expand Down
2 changes: 1 addition & 1 deletion lib/launcher/test/unit/Cardano/LauncherSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import Test.Hspec
, shouldReturn
, shouldSatisfy
)
import Test.Utils.Windows
import Test.Utils.Platform
( isWindows, pendingOnWine, skipOnWindows )
import UnliftIO.Async
( async, race_, waitAnyCancel )
Expand Down
4 changes: 2 additions & 2 deletions lib/launcher/test/unit/Cardano/StartupSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import Test.Hspec.Core.Spec
( ResultStatus (..) )
import Test.Hspec.Expectations
( Expectation, HasCallStack )
import Test.Utils.Platform
( nullFileName, pendingOnWindows )
import Test.Utils.Trace
( captureLogging )
import Test.Utils.Windows
( nullFileName, pendingOnWindows )
import UnliftIO.Async
( race )
import UnliftIO.Concurrent
Expand Down
12 changes: 5 additions & 7 deletions lib/shelley/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ import System.Environment
( setEnv )
import System.FilePath
( (</>) )
import System.IO
( BufferMode (..), hSetBuffering, stderr, stdout )
import Test.Hspec
( hspec )
import Test.Hspec.Core.Spec
Expand All @@ -134,6 +132,8 @@ import Test.Integration.Framework.Context
( Context (..), PoolGarbageCollectionEvent (..) )
import Test.Utils.Paths
( getTestData, inNixBuild )
import Test.Utils.Startup
( withLineBuffering )
import UnliftIO.Async
( race )
import UnliftIO.Exception
Expand Down Expand Up @@ -223,16 +223,14 @@ withTestsSetup :: (FilePath -> (Tracer IO TestsLog, Tracers IO) -> IO a) -> IO a
withTestsSetup action = do
-- Handle SIGTERM properly
installSignalHandlersNoLogging
-- Flush test output as soon as a line is printed
hSetBuffering stdout LineBuffering
hSetBuffering stderr LineBuffering
-- Stop cardano-cli complaining about file permissions
setDefaultFilePermissions
-- Enables small test-specific workarounds, like timing out faster if wallet
-- deletion fails.
setEnv "CARDANO_WALLET_TEST_INTEGRATION" "1"
-- Set UTF-8, regardless of user locale
withUtf8Encoding $
-- Flush test output as soon as a line is printed.
-- Set UTF-8, regardless of user locale.
withLineBuffering $ withUtf8Encoding $
-- This temporary directory will contain logs, and all other data
-- produced by the integration tests.
withSystemTempDir stdoutTextTracer "test" $ \testDir ->
Expand Down
2 changes: 1 addition & 1 deletion lib/shelley/test/unit/Cardano/Wallet/Shelley/LaunchSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Options.Applicative
( ParserResult (..), defaultPrefs, execParserPure, info )
import Test.Hspec
( Spec, describe, it, shouldSatisfy )
import Test.Utils.Windows
import Test.Utils.Platform
( isWindows )

spec :: Spec
Expand Down
4 changes: 2 additions & 2 deletions lib/test-utils/cardano-wallet-test-utils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ library
exposed-modules:
Test.Hspec.Extra
Test.QuickCheck.Extra
Test.Utils.Darwin
Test.Utils.FilePath
Test.Utils.Laws
Test.Utils.Laws.PartialOrd
Test.Utils.Paths
Test.Utils.Roundtrip
Test.Utils.Resource
Test.Utils.Platform
Test.Utils.Startup
Test.Utils.StaticServer
Test.Utils.Time
Test.Utils.Trace
Test.Utils.Windows

test-suite unit
default-language:
Expand Down
4 changes: 2 additions & 2 deletions lib/test-utils/src/Test/Hspec/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import Test.Hspec
)
import Test.HUnit.Lang
( HUnitFailure (..), assertFailure, formatFailureReason )
import Test.Utils.Platform
( isWindows )
import Test.Utils.Resource
( unBracket )
import Test.Utils.Windows
( isWindows )
import UnliftIO.Async
( race )
import UnliftIO.Concurrent
Expand Down
27 changes: 0 additions & 27 deletions lib/test-utils/src/Test/Utils/Darwin.hs
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
-- |
-- Copyright: © 2018-2021 IOHK
-- License: Apache-2.0
--
-- Utility function for making test suites pass on Darwin/macOS.

module Test.Utils.Darwin
( pendingOnMacOS
) where

import Prelude

import Control.Monad
( when )
import System.Info
( os )
import Test.Hspec.Core.Spec
( pendingWith )
import Test.Hspec.Expectations
( Expectation, HasCallStack )

-- | Mark test pending if running on macOS
pendingOnMacOS :: HasCallStack => String -> Expectation
pendingOnMacOS reason = when isDarwin $ pendingWith reason

isDarwin :: Bool
isDarwin = os == "darwin"
2 changes: 1 addition & 1 deletion lib/test-utils/src/Test/Utils/FilePath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import System.FilePath.Windows
( makeValid )
import Test.QuickCheck
( Arbitrary (..), elements, listOf1, scale )
import Test.Utils.Windows
import Test.Utils.Platform
( isWindows )

-- | A file or directory name. The 'Arbitrary' instance will generate values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Copyright: © 2018-2020 IOHK
-- Copyright: © 2018-2021 IOHK
-- License: Apache-2.0
--
-- Utility function for making test suites pass on Windows.
-- Utility function for making test suites pass on difficult platforms.

module Test.Utils.Windows
( skipOnWindows
module Test.Utils.Platform
( -- * Skipping tests
skipOnWindows
, pendingOnWindows
, pendingOnWine
, pendingOnMacOS

-- * OS detection
, whenWindows
, isWindows
, nullFileName
, isMacOS
, getIsWine

-- * Cross-platform compatibility
, nullFileName
) where

import Prelude
Expand Down Expand Up @@ -45,11 +52,16 @@ pendingOnWine reason = whenWindows $ do
wine <- getIsWine
when wine $ pendingWith reason

whenWindows :: IO () -> IO ()
whenWindows = when isWindows
-- | Mark test pending if running on macOS
pendingOnMacOS :: HasCallStack => String -> Expectation
pendingOnMacOS reason = when isMacOS $ pendingWith reason

isWindows :: Bool
isWindows, isMacOS :: Bool
isWindows = os == "mingw32"
isMacOS = os == "darwin"

whenWindows :: IO () -> IO ()
whenWindows = when isWindows

-- | Use the presence of @winepath.exe@ to detect when running tests under Wine.
getIsWine :: IO Bool
Expand Down
39 changes: 39 additions & 0 deletions lib/test-utils/src/Test/Utils/Startup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Test.Utils.Startup
( withLineBuffering
, withNoBuffering
) where

import Prelude

import Control.Monad
( void )
import Control.Monad.IO.Unlift
( MonadUnliftIO )
import UnliftIO.Exception
( IOException, bracket, tryJust )
import UnliftIO.IO
( BufferMode (..), hGetBuffering, hSetBuffering, stderr, stdout )

withLineBuffering, withNoBuffering :: MonadUnliftIO m => m a -> m a
withLineBuffering = withBuffering LineBuffering
withNoBuffering = withBuffering NoBuffering

withBuffering :: MonadUnliftIO m => BufferMode -> m a -> m a
withBuffering mode = bracket before after . const
where
before = do
prev <- (,) <$> getBuf stdout <*> getBuf stderr
setBuf stdout (Just mode)
setBuf stderr (Just mode)
pure prev
after (prevOut, prevErr) = do
setBuf stdout prevOut
setBuf stderr prevErr

getBuf = tryErr . hGetBuffering
setBuf h = maybe (pure ()) (void . tryErr . hSetBuffering h)

-- Swallow any IO errors
tryErr = fmap (either (const Nothing) Just) . tryJust isAlright
isAlright :: IOException -> Maybe ()
isAlright = const (Just ())