1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
module UnitTests.Distribution.Client.Store (tests) where
--import Control.Monad
--import Control.Concurrent (forkIO, threadDelay)
--import Control.Concurrent.MVar
import qualified Data.Set as Set
import System.FilePath
import System.Directory
--import System.Random
import Distribution.Package (UnitId, mkUnitId)
import Distribution.Compiler (CompilerId(..), CompilerFlavor(..))
import Distribution.Version (mkVersion)
import Distribution.Verbosity (Verbosity, silent)
import Distribution.Simple.Utils (withTempDirectory)
import Distribution.Client.Store
import Distribution.Client.RebuildMonad
import Test.Tasty
import Test.Tasty.HUnit
tests :: [TestTree]
tests =
[ testCase "list content empty" testListEmpty
, testCase "install serial" testInstallSerial
--, testCase "install parallel" testInstallParallel
--TODO: figure out some way to do a parallel test, see issue below
]
testListEmpty :: Assertion
testListEmpty =
withTempDirectory verbosity "." "store-" $ \tmp -> do
let storeDirLayout = defaultStoreDirLayout (tmp </> "store")
assertStoreEntryExists storeDirLayout compid unitid False
assertStoreContent tmp storeDirLayout compid Set.empty
where
compid = CompilerId GHC (mkVersion [1,0])
unitid = mkUnitId "foo-1.0-xyz"
testInstallSerial :: Assertion
testInstallSerial =
withTempDirectory verbosity "." "store-" $ \tmp -> do
let storeDirLayout = defaultStoreDirLayout (tmp </> "store")
copyFiles file content dir = do
-- we copy into a prefix inside the tmp dir and return the prefix
let destprefix = dir </> "prefix"
createDirectory destprefix
writeFile (destprefix </> file) content
return (destprefix,[])
assertNewStoreEntry tmp storeDirLayout compid unitid1
(copyFiles "file1" "content-foo") (return ())
UseNewStoreEntry
assertNewStoreEntry tmp storeDirLayout compid unitid1
(copyFiles "file1" "content-foo") (return ())
UseExistingStoreEntry
assertNewStoreEntry tmp storeDirLayout compid unitid2
(copyFiles "file2" "content-bar") (return ())
UseNewStoreEntry
let pkgDir :: UnitId -> FilePath
pkgDir = storePackageDirectory storeDirLayout compid
assertFileEqual (pkgDir unitid1 </> "file1") "content-foo"
assertFileEqual (pkgDir unitid2 </> "file2") "content-bar"
where
compid = CompilerId GHC (mkVersion [1,0])
unitid1 = mkUnitId "foo-1.0-xyz"
unitid2 = mkUnitId "bar-2.0-xyz"
{-
-- unfortunately a parallel test like the one below is thwarted by the normal
-- process-internal file locking. If that locking were not in place then we
-- ought to get the blocking behaviour, but due to the normal Handle locking
-- it just fails instead.
testInstallParallel :: Assertion
testInstallParallel =
withTempDirectory verbosity "." "store-" $ \tmp -> do
let storeDirLayout = defaultStoreDirLayout (tmp </> "store")
sync1 <- newEmptyMVar
sync2 <- newEmptyMVar
outv <- newEmptyMVar
regv <- newMVar (0 :: Int)
sequence_
[ do forkIO $ do
let copyFiles dir = do
delay <- randomRIO (1,100000)
writeFile (dir </> "file") (show n)
putMVar sync1 ()
readMVar sync2
threadDelay delay
register = do
modifyMVar_ regv (return . (+1))
threadDelay 200000
o <- newStoreEntry verbosity storeDirLayout
compid unitid
copyFiles register
putMVar outv (n, o)
| n <- [0..9 :: Int] ]
replicateM_ 10 (takeMVar sync1)
-- all threads are in the copyFiles action concurrently, release them:
putMVar sync2 ()
outcomes <- replicateM 10 (takeMVar outv)
regcount <- readMVar regv
let regcount' = length [ () | (_, UseNewStoreEntry) <- outcomes ]
assertEqual "num registrations" 1 regcount
assertEqual "num registrations" 1 regcount'
assertStoreContent tmp storeDirLayout compid (Set.singleton unitid)
let pkgDir :: UnitId -> FilePath
pkgDir = storePackageDirectory storeDirLayout compid
case [ n | (n, UseNewStoreEntry) <- outcomes ] of
[n] -> assertFileEqual (pkgDir unitid </> "file") (show n)
_ -> assertFailure "impossible"
where
compid = CompilerId GHC (mkVersion [1,0])
unitid = mkUnitId "foo-1.0-xyz"
-}
-------------
-- Utils
assertNewStoreEntry :: FilePath -> StoreDirLayout
-> CompilerId -> UnitId
-> (FilePath -> IO (FilePath,[FilePath])) -> IO ()
-> NewStoreEntryOutcome
-> Assertion
assertNewStoreEntry tmp storeDirLayout compid unitid
copyFiles register expectedOutcome = do
entries <- runRebuild tmp $ getStoreEntries storeDirLayout compid
outcome <- newStoreEntry verbosity storeDirLayout
compid unitid
copyFiles register
assertEqual "newStoreEntry outcome" expectedOutcome outcome
assertStoreEntryExists storeDirLayout compid unitid True
let expected = Set.insert unitid entries
assertStoreContent tmp storeDirLayout compid expected
assertStoreEntryExists :: StoreDirLayout
-> CompilerId -> UnitId -> Bool
-> Assertion
assertStoreEntryExists storeDirLayout compid unitid expected = do
actual <- doesStoreEntryExist storeDirLayout compid unitid
assertEqual "store entry exists" expected actual
assertStoreContent :: FilePath -> StoreDirLayout
-> CompilerId -> Set.Set UnitId
-> Assertion
assertStoreContent tmp storeDirLayout compid expected = do
actual <- runRebuild tmp $ getStoreEntries storeDirLayout compid
assertEqual "store content" actual expected
assertFileEqual :: FilePath -> String -> Assertion
assertFileEqual path expected = do
exists <- doesFileExist path
assertBool ("file does not exist:\n" ++ path) exists
actual <- readFile path
assertEqual ("file content for:\n" ++ path) expected actual
verbosity :: Verbosity
verbosity = silent
|