1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-26 00:53:23 +03:00
juvix/test/Examples/Positive.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

88 lines
2.2 KiB
Haskell

module Examples.Positive where
import Base
import Compilation.Base
data PosTest = PosTest
{ _name :: String,
_dir :: Path Abs Dir,
_file :: Path Abs File,
_expectedFile :: Path Abs File,
_stdin :: Text
}
makeLenses ''PosTest
srcRoot :: Path Abs Dir
srcRoot = relToProject $(mkRelDir "examples/")
expectedRoot :: Path Abs Dir
expectedRoot = relToProject $(mkRelDir "tests/examplesExpected")
toTestDescr :: PosTest -> TestDescr
toTestDescr PosTest {..} =
TestDescr
{ _testRoot = _dir,
_testName = _name,
_testAssertion = Steps $ compileAssertion _dir 3 (CompileOnly _stdin) _file _expectedFile
}
allTests :: TestTree
allTests =
testGroup
"Compile and test examples"
(map (mkTest . toTestDescr) tests)
posTest :: String -> Path Rel Dir -> Path Rel File -> Path Rel File -> Text -> PosTest
posTest _name rdir rfile routfile _stdin =
let _dir = srcRoot <//> rdir
_file = _dir <//> rfile
_expectedFile = expectedRoot <//> routfile
in PosTest {..}
tests :: [PosTest]
tests =
[ posTest
"Fibonacci"
$(mkRelDir "milestone/Fibonacci")
$(mkRelFile "Fibonacci.juvix")
$(mkRelFile "Fibonacci/expected.golden")
"25\n",
posTest
"Hello World"
$(mkRelDir "milestone/HelloWorld")
$(mkRelFile "HelloWorld.juvix")
$(mkRelFile "HelloWorld/expected.golden")
"",
posTest
"Collatz calculator"
$(mkRelDir "milestone/Collatz")
$(mkRelFile "Collatz.juvix")
$(mkRelFile "Collatz/expected.golden")
"123\n",
posTest
"Pascal's triangle"
$(mkRelDir "milestone/PascalsTriangle")
$(mkRelFile "PascalsTriangle.juvix")
$(mkRelFile "PascalsTriangle/expected.golden")
"",
posTest
"Demo"
$(mkRelDir "demo")
$(mkRelFile "Demo.juvix")
$(mkRelFile "Demo/expected.golden")
"",
posTest
"TicTacToe"
$(mkRelDir "milestone/TicTacToe")
$(mkRelFile "CLI/TicTacToe.juvix")
$(mkRelFile "TicTacToe/expected.golden")
"aaa\n0\n10\n1\n2\n3\n3\n4\n5\n6\n7\n8\n9\n",
posTest
"Towers of Hanoi"
$(mkRelDir "milestone/Hanoi")
$(mkRelFile "Hanoi.juvix")
$(mkRelFile "Hanoi/expected.golden")
""
]