-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use c_unlink instead of removeFile * Freshen up LazyHClose tests * Avoid directory package in builder tests * Do not depend on directory package * Review suggestions
- Loading branch information
Showing
4 changed files
with
96 additions
and
96 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,67 +1,64 @@ | ||
import qualified Data.ByteString as S | ||
import qualified Data.ByteString.Char8 as S8 | ||
module Main (main) where | ||
|
||
import Control.Monad (void, forM_) | ||
import Data.ByteString.Internal (toForeignPtr) | ||
import Foreign.C.String (withCString) | ||
import Foreign.ForeignPtr (finalizeForeignPtr) | ||
import System.IO (openFile, openTempFile, hClose, hPutStrLn, IOMode(..)) | ||
import System.Posix.Internals (c_unlink) | ||
|
||
import qualified Data.ByteString as S | ||
import qualified Data.ByteString.Char8 as S8 | ||
import qualified Data.ByteString.Lazy as L | ||
import qualified Data.ByteString.Lazy.Char8 as L8 | ||
|
||
import Control.Monad | ||
import System.Directory | ||
import System.Mem | ||
import System.IO | ||
|
||
import Data.ByteString.Internal | ||
import Foreign.ForeignPtr | ||
|
||
main :: IO () | ||
main = do | ||
writeFile "a" "x" | ||
let n = 1000 | ||
(fn, h) <- openTempFile "." "lazy-hclose-test.tmp" | ||
hPutStrLn h "x" | ||
hClose h | ||
|
||
------------------------------------------------------------------------ | ||
-- readFile tests | ||
|
||
print "Testing resource leaks for Strict.readFile" | ||
putStrLn "Testing resource leaks for Strict.readFile" | ||
forM_ [1..n] $ const $ do | ||
r <- S.readFile "a" | ||
S.writeFile "b" (S8.pack "abc") | ||
renameFile "b" "a" | ||
r <- S.readFile fn | ||
appendFile fn "" -- will fail, if fn has not been closed yet | ||
|
||
print "Testing resource leaks for Lazy.readFile" | ||
putStrLn "Testing resource leaks for Lazy.readFile" | ||
forM_ [1..n] $ const $ do | ||
r <- L.readFile "a" | ||
L.length r `seq` return () -- force the input, and done with 'r' now. | ||
L.writeFile "b" (L8.pack "abc") -- but we still need the finalizers to run | ||
renameFile "b" "a" | ||
r <- L.readFile fn | ||
L.length r `seq` return () | ||
appendFile fn "" -- will fail, if fn has not been closed yet | ||
|
||
-- manage the resources explicitly. | ||
print "Testing resource leaks when converting lazy to strict" | ||
putStrLn "Testing resource leaks when converting lazy to strict" | ||
forM_ [1..n] $ const $ do | ||
let release c = finalizeForeignPtr fp where (fp,_,_) = toForeignPtr c | ||
r <- L.readFile "a" | ||
mapM_ release (L.toChunks r) -- should close it. | ||
L.writeFile "b" (L8.pack "abc") | ||
renameFile "b" "a" | ||
r <- L.readFile fn | ||
mapM_ release (L.toChunks r) | ||
appendFile fn "" -- will fail, if fn has not been closed yet | ||
|
||
------------------------------------------------------------------------ | ||
-- hGetContents tests | ||
|
||
-- works now | ||
print "Testing strict hGetContents" | ||
putStrLn "Testing strict hGetContents" | ||
forM_ [1..n] $ const $ do | ||
h <- openFile "a" ReadMode | ||
r <- S.hGetContents h -- should be strict, and hClosed. | ||
h <- openFile fn ReadMode | ||
r <- S.hGetContents h | ||
S.last r `seq` return () | ||
S.writeFile "b" (S8.pack "abc") | ||
renameFile "b" "a" | ||
appendFile fn "" -- will fail, if fn has not been closed yet | ||
|
||
-- works now | ||
print "Testing lazy hGetContents" | ||
putStrLn "Testing lazy hGetContents" | ||
forM_ [1..n] $ const $ do | ||
h <- openFile "a" ReadMode | ||
r <- L.hGetContents h -- should be strict, and hClosed. | ||
h <- openFile fn ReadMode | ||
r <- L.hGetContents h | ||
L.last r `seq` return () | ||
L.writeFile "b" (L8.pack "abc") | ||
renameFile "b" "a" | ||
appendFile fn "" -- will fail, if fn has not been closed yet | ||
|
||
removeFile "a" | ||
removeFile fn | ||
|
||
n = 1000 | ||
removeFile :: String -> IO () | ||
removeFile fn = void $ withCString fn c_unlink |
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
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
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