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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
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.Directory
import System.FilePath
-- import System.Random
import Distribution.Package (UnitId, mkUnitId)
import Distribution.Simple.Compiler (AbiTag (..), Compiler (..), CompilerFlavor (..), CompilerId (..))
import Distribution.Simple.Utils (withTempDirectory)
import Distribution.Verbosity (Verbosity, silent)
import Distribution.Version (mkVersion)
import Distribution.Client.RebuildMonad
import Distribution.Client.Store
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 compiler unitid False
assertStoreContent tmp storeDirLayout compiler Set.empty
where
compiler :: Compiler
compiler =
Compiler
{ compilerId = CompilerId GHC (mkVersion [1, 0])
, compilerAbiTag = NoAbiTag
, compilerCompat = []
, compilerLanguages = []
, compilerExtensions = []
, compilerProperties = mempty
}
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
compiler
unitid1
(copyFiles "file1" "content-foo")
(return ())
UseNewStoreEntry
assertNewStoreEntry
tmp
storeDirLayout
compiler
unitid1
(copyFiles "file1" "content-foo")
(return ())
UseExistingStoreEntry
assertNewStoreEntry
tmp
storeDirLayout
compiler
unitid2
(copyFiles "file2" "content-bar")
(return ())
UseNewStoreEntry
let pkgDir :: UnitId -> FilePath
pkgDir = storePackageDirectory storeDirLayout compiler
assertFileEqual (pkgDir unitid1 </> "file1") "content-foo"
assertFileEqual (pkgDir unitid2 </> "file2") "content-bar"
where
compiler :: Compiler
compiler =
Compiler
{ compilerId = CompilerId GHC (mkVersion [1, 0])
, compilerAbiTag = NoAbiTag
, compilerCompat = []
, compilerLanguages = []
, compilerExtensions = []
, compilerProperties = mempty
}
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
-> Compiler
-> UnitId
-> (FilePath -> IO (FilePath, [FilePath]))
-> IO ()
-> NewStoreEntryOutcome
-> Assertion
assertNewStoreEntry
tmp
storeDirLayout
compiler
unitid
copyFiles
register
expectedOutcome = do
entries <- runRebuild tmp $ getStoreEntries storeDirLayout compiler
outcome <-
newStoreEntry
verbosity
storeDirLayout
compiler
unitid
copyFiles
register
assertEqual "newStoreEntry outcome" expectedOutcome outcome
assertStoreEntryExists storeDirLayout compiler unitid True
let expected = Set.insert unitid entries
assertStoreContent tmp storeDirLayout compiler expected
assertStoreEntryExists
:: StoreDirLayout
-> Compiler
-> UnitId
-> Bool
-> Assertion
assertStoreEntryExists storeDirLayout compiler unitid expected = do
actual <- doesStoreEntryExist storeDirLayout compiler unitid
assertEqual "store entry exists" expected actual
assertStoreContent
:: FilePath
-> StoreDirLayout
-> Compiler
-> Set.Set UnitId
-> Assertion
assertStoreContent tmp storeDirLayout compiler expected = do
actual <- runRebuild tmp $ getStoreEntries storeDirLayout compiler
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
|