1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-07 16:22:14 +03:00
juvix/test/Package/Negative.hs
Paul Cadman 2f4a3f809b
Run test suite in parallel (#2507)
## 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 disabled
e6dca22cfd/src/Juvix/Compiler/Pipeline/Run.hs (L64)
2023-11-16 16:19:52 +01:00

119 lines
3.7 KiB
Haskell

module Package.Negative where
import Base
import Juvix.Compiler.Pipeline.Package
import Juvix.Compiler.Pipeline.Package.Loader.EvalEff.IO
import Juvix.Data.Effect.TaggedLock
type FailMsg = String
data NegTest a = NegTest
{ _name :: String,
_relDir :: Path Rel Dir,
_checkErr :: a -> Maybe FailMsg
}
root :: Path Abs Dir
root = relToProject $(mkRelDir "tests/negative/Package")
testDescr :: (Typeable a) => NegTest a -> TestDescr
testDescr NegTest {..} =
let tRoot = root <//> _relDir
in TestDescr
{ _testName = _name,
_testRoot = tRoot,
_testAssertion = Single $ do
res <-
withTempDir'
( runFinal
. resourceToIOFinal
. embedToFinal @IO
. runError
. runFilesIO
. mapError (JuvixError @PackageLoaderError)
. runTaggedLock LockModeExclusive
. runEvalFileEffIO
. readPackage tRoot
. CustomBuildDir
. Abs
)
case mapLeft fromJuvixError res of
Left (Just err) -> whenJust (_checkErr err) assertFailure
Left Nothing -> assertFailure "An error ocurred but it was not when reading the package."
Right {} -> assertFailure "There was no error when reading the package"
}
allTests :: TestTree
allTests =
testGroup
"Package loading negative tests"
( map (mkTest . testDescr) packageErrorTests
)
wrongError :: Maybe FailMsg
wrongError = Just "Incorrect error"
packageErrorTests :: [NegTest PackageLoaderError]
packageErrorTests =
[ NegTest
"package YAML parse error"
$(mkRelDir "YamlParseError")
$ \case
PackageLoaderError _ ErrPackageYamlParseError {} -> Nothing
_ -> wrongError,
NegTest
"lockfile YAML parse error"
$(mkRelDir "InvalidLockfile")
$ \case
PackageLoaderError _ ErrLockfileYamlParseError {} -> Nothing
_ -> wrongError,
NegTest
"package YAML invalid version"
$(mkRelDir "YamlInvalidVersion")
$ \case
PackageLoaderError _ ErrVersionParseError {} -> Nothing
_ -> wrongError,
NegTest
"package YAML duplicate dependencies"
$(mkRelDir "YamlDuplicateDependencies")
$ \case
PackageLoaderError _ ErrDuplicateDependencyError {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix doesn't compile"
$(mkRelDir "PackageJuvixDoesntCompile")
$ \case
PackageLoaderError _ ErrPackageJuvixError {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix no package symbol"
$(mkRelDir "PackageJuvixNoPackageSymbol")
$ \case
PackageLoaderError _ ErrPackageSymbolNotFound {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix package symbol has wrong type1"
$(mkRelDir "PackageJuvixPackageSymbolWrongType1")
$ \case
PackageLoaderError _ ErrPackageTypeError {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix package symbol has wrong type2"
$(mkRelDir "PackageJuvixPackageSymbolWrongType2")
$ \case
PackageLoaderError _ ErrPackageTypeError {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix duplicate dependencies"
$(mkRelDir "PackageJuvixDuplicateDependencies")
$ \case
PackageLoaderError _ ErrDuplicateDependencyError {} -> Nothing
_ -> wrongError,
NegTest
"Package.juvix lockfile YAML parse error"
$(mkRelDir "InvalidLockfile")
$ \case
PackageLoaderError _ ErrLockfileYamlParseError {} -> Nothing
_ -> wrongError
]