mirror of
https://github.com/anoma/juvix.git
synced 2024-12-12 14:28:08 +03:00
2f4a3f809b
## Overview This PR makes the compiler pipeline thread-safe so that the test suite can be run in parallel. This is achieved by: * Removing use of `{get, set, with}CurrentDir` functions. * Adding locking around shared file resources like the the global-project and internal build directory. NB: **Locking is disabled for the main compiler target**, as it is single threaded they are not required. ## Run test suite in parallel To run the test suite in parallel you must add `--ta '+RTS -N -RTS'` to your stack test arguments. For example: ``` stack test --fast --ta '+RTS -N -RTS' ``` The `-N` instructs the Haskell runtime to choose the number of threads to use based on how many processors there are on your machine. You can use `-Nn` to see the number of threads to `n`. These flags are already [set in the Makefile](e6dca22cfd/Makefile (L26)
) when you or CI uses `stack test`. ## Locking The Haskell package [filelock](https://hackage.haskell.org/package/filelock) is used for locking. File locks are used instead of MVars because Juvix code does not control when new threads are created, they are created by the test suite. This means that MVars created by Juvix code will have no effect, because they are created independently on each test-suite thread. Additionally the resources we're locking live on the filesystem and so can be conveniently tagged by path. ### FileLock The filelock library is wrapped in a FileLock effect:e6dca22cfd/src/Juvix/Data/Effect/FileLock/Base.hs (L6-L8)
There is an [IO interpreter](e6dca22cfd/src/Juvix/Data/Effect/FileLock/IO.hs (L8)
) that uses filelock and an [no-op interpreter](e6dca22cfd/src/Juvix/Data/Effect/FileLock/Permissive.hs (L7)
) that just runs actions unconditionally. ### TaggedLock To make the file locks simpler to use a TaggedLock effect is introduced:e6dca22cfd/src/Juvix/Data/Effect/TaggedLock/Base.hs (L5-L11)
And convenience function:e6dca22cfd/src/Juvix/Data/Effect/TaggedLock.hs (L28)
This allows an action to be locked, tagged by a directory that may or may not exist. For example in the following code, an action is performed on a directory `root` that may delete the directory before repopulating the files. So the lockfile cannot be stored in the `root` itself.e6dca22cfd/src/Juvix/Extra/Files.hs (L55-L60)
## Pipeline As noted above, we only use locking in the test suite. The main app target pipeline is single threaded and so locking is unnecessary. So the interpretation of locks is parameterised so that locking can be disablede6dca22cfd/src/Juvix/Compiler/Pipeline/Run.hs (L64)
88 lines
2.5 KiB
Haskell
88 lines
2.5 KiB
Haskell
module Format where
|
|
|
|
import Base
|
|
import Juvix.Compiler.Concrete qualified as Concrete
|
|
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping qualified as Scoper
|
|
import Juvix.Compiler.Concrete.Translation.FromSource qualified as Parser
|
|
import Juvix.Compiler.Pipeline.Setup
|
|
import Juvix.Data.Effect.TaggedLock
|
|
import Juvix.Formatter
|
|
|
|
data PosTest = PosTest
|
|
{ _name :: String,
|
|
_dir :: Path Abs Dir,
|
|
_file :: Path Abs File,
|
|
_expectedFile :: Maybe (Path Abs File),
|
|
_force :: Bool
|
|
}
|
|
|
|
makeLenses ''PosTest
|
|
|
|
root :: Path Abs Dir
|
|
root = relToProject $(mkRelDir "tests/positive")
|
|
|
|
posTest :: String -> Path Rel Dir -> Path Rel File -> Maybe (Path Rel File) -> Bool -> PosTest
|
|
posTest _name rdir rfile efile _force =
|
|
let _dir = root <//> rdir
|
|
_file = _dir <//> rfile
|
|
_expectedFile = (_dir <//>) <$> efile
|
|
in PosTest {..}
|
|
|
|
testDescr :: PosTest -> TestDescr
|
|
testDescr PosTest {..} =
|
|
TestDescr
|
|
{ _testName = _name,
|
|
_testRoot = _dir,
|
|
_testAssertion = Steps $ \step -> do
|
|
entryPoint <- defaultEntryPointIO' LockModeExclusive _dir _file
|
|
let maybeFile = entryPoint ^? entryPointModulePaths . _head
|
|
f <- fromMaybeM (assertFailure "Not a module") (return maybeFile)
|
|
|
|
original :: Text <- readFile (toFilePath f)
|
|
|
|
step "Parsing"
|
|
p :: Parser.ParserResult <- snd <$> runIOExclusive entryPoint upToParsing
|
|
|
|
step "Scoping"
|
|
s :: Scoper.ScoperResult <-
|
|
snd
|
|
<$> runIOExclusive
|
|
entryPoint
|
|
( do
|
|
void (entrySetup defaultDependenciesConfig)
|
|
Concrete.fromParsed p
|
|
)
|
|
|
|
let formatted = formatScoperResult' _force original s
|
|
case _expectedFile of
|
|
Nothing -> do
|
|
step "Format"
|
|
assertEqDiffText "check: pretty . scope . parse = id" original formatted
|
|
Just eFile -> do
|
|
step "Checking against expected output file"
|
|
expFile :: Text <- readFile (toFilePath eFile)
|
|
assertEqDiffText "Compare to expected output" formatted expFile
|
|
}
|
|
|
|
allTests :: TestTree
|
|
allTests =
|
|
testGroup
|
|
"Format positive tests"
|
|
(map (mkTest . testDescr) tests)
|
|
|
|
tests :: [PosTest]
|
|
tests =
|
|
[ posTest
|
|
"Format"
|
|
$(mkRelDir ".")
|
|
$(mkRelFile "Format.juvix")
|
|
Nothing
|
|
False,
|
|
posTest
|
|
"TrailingWhitespace"
|
|
$(mkRelDir ".")
|
|
$(mkRelFile "LocalModWithAxiom.juvix")
|
|
(Just $(mkRelFile "LocalModWithAxiom.juvix.formatted"))
|
|
True
|
|
]
|