forked from haskell/cabal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for terminating "cabal run" on unix
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
cabal-testsuite/PackageTests/NewBuild/CmdRun/Terminate/Main.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Control.Concurrent (killThread, threadDelay, myThreadId) | ||
import Control.Exception (finally) | ||
import qualified System.Posix.Signals as Signal | ||
import System.Exit (exitFailure) | ||
|
||
main = do | ||
writeFile "exe.run" "up and running" | ||
mainThreadId <- myThreadId | ||
Signal.installHandler Signal.sigTERM (Signal.Catch $ killThread mainThreadId) Nothing | ||
sleep | ||
`finally` putStrLn "exiting" | ||
where | ||
sleep = do | ||
putStrLn "about to sleep" | ||
threadDelay 10000000 -- 10s | ||
putStrLn "done sleeping" |
9 changes: 9 additions & 0 deletions
9
cabal-testsuite/PackageTests/NewBuild/CmdRun/Terminate/RunKill.cabal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: RunKill | ||
version: 1.0 | ||
build-type: Simple | ||
cabal-version: >= 1.10 | ||
|
||
executable exe | ||
default-language: Haskell2010 | ||
build-depends: base, process, unix | ||
main-is: Main.hs |
2 changes: 2 additions & 0 deletions
2
cabal-testsuite/PackageTests/NewBuild/CmdRun/Terminate/cabal.project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
packages: . | ||
|
61 changes: 61 additions & 0 deletions
61
cabal-testsuite/PackageTests/NewBuild/CmdRun/Terminate/cabal.test.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Test.Cabal.Prelude | ||
import qualified System.Process as Process | ||
import Control.Concurrent (threadDelay) | ||
import System.Directory (removeFile) | ||
import Control.Exception (catch, throwIO) | ||
import System.IO.Error (isDoesNotExistError) | ||
|
||
{- | ||
This test verifies that 'cabal run' terminates its | ||
child when it is killed. More generally, while we | ||
use the same code path for all child processes, this | ||
ensure that cabal-install cleans up after all children. | ||
(That might change if 'cabal run' is changed to exec(3) | ||
without forking in the future.) | ||
-} | ||
|
||
main :: IO () | ||
main = cabalTest $ do | ||
skipIfWindows -- test project relies on Posix | ||
|
||
dir <- fmap testCurrentDir getTestEnv | ||
let runFile = dir </> "exe.run" | ||
liftIO $ removeFile runFile `catchNoExist` return () | ||
|
||
cabal_raw_action ["v2-build", "exe"] (\_ -> return ()) | ||
r <- fails $ cabal_raw_action ["v2-run", "exe"] $ \cabalHandle -> do | ||
-- wait for "cabal run" to have started "exe" | ||
waitFile total runFile | ||
-- then kill "cabal run" | ||
Process.terminateProcess cabalHandle | ||
|
||
-- "exe" should exit, and should have been interrupted before | ||
-- finishing its sleep | ||
assertOutputContains "exiting" r | ||
assertOutputDoesNotContain "done sleeping" r | ||
|
||
where | ||
catchNoExist action handle = | ||
action `catch` | ||
(\e -> if isDoesNotExistError e then handle else throwIO e) | ||
waitFile totalWait f | ||
| totalWait <= 0 = error "waitFile timed out" | ||
| otherwise = readFile f `catchNoExist` do | ||
threadDelay delta | ||
waitFile (totalWait - delta) f | ||
delta = 50000 -- 0.05s | ||
total = 10000000 -- 10s | ||
|
||
cabal_raw_action :: [String] -> (Process.ProcessHandle -> IO ()) -> TestM Result | ||
cabal_raw_action args action = do | ||
configured_prog <- requireProgramM cabalProgram | ||
env <- getTestEnv | ||
r <- liftIO $ runAction (testVerbosity env) | ||
(Just (testCurrentDir env)) | ||
(testEnvironment env) | ||
(programPath configured_prog) | ||
args | ||
Nothing | ||
action | ||
recordLog r | ||
requireSuccess r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters