Skip to content

Commit

Permalink
[cram] Add locks to the Cram stanza (#4397)
Browse files Browse the repository at this point in the history
* Add locks to Cram stanza
* Add timing-based tests for the locks
* Reference the new option in the docs
* Add changelog entry

Signed-off-by: Ulysse Gérard <[email protected]>
  • Loading branch information
voodoos committed Apr 12, 2021
1 parent e84ba52 commit 9d81999
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
------------------

- Add the possibility to use `locks` with the cram tests stanza (#4480, @voodoos)

2.8.5 (28/03/2021)
------------------

Expand Down
2 changes: 2 additions & 0 deletions doc/tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,8 @@ The ``cram`` stanza accepts the following fields:
- ``alias`` - alias that can be used to run the test. In addition to the user
alias, every test ``foo.t`` is attached to the ``@runtest`` alias and gets its
own ``@foo`` alias to make it convenient to run individually.
- ``(locks (<lock-names>))`` specify that the tests must be run while
holding the following locks. See the :ref:`locks` section for more details.
- ``deps`` - dependencies of the test
A single test may be configured by more than one ``cram`` stanza. In such cases,
Expand Down
17 changes: 15 additions & 2 deletions src/dune_rules/cram_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ type effective =
; alias : Alias.Name.Set.t
; deps : unit Build.t list
; enabled_if : Blang.t list
; locks : Path.Set.t
; packages : Package.Name.Set.t
}

let empty_effective =
{ loc = Loc.none
; alias = Alias.Name.Set.singleton Alias.Name.runtest
; enabled_if = [ Blang.true_ ]
; locks = Path.Set.empty
; deps = []
; packages = Package.Name.Set.empty
}
Expand Down Expand Up @@ -96,9 +98,10 @@ let test_rule ~sctx ~expander ~dir (spec : effective)
in
action
in
let locks = Path.Set.to_list spec.locks in
let cram = Build.with_no_targets cram in
List.iter aliases ~f:(fun alias ->
Alias_rules.add sctx ~alias ~stamp ~loc cram ~locks:[]) )
Alias_rules.add sctx ~alias ~stamp ~loc cram ~locks) )

let rules ~sctx ~expander ~dir tests =
let stanzas =
Expand Down Expand Up @@ -175,7 +178,17 @@ let rules ~sctx ~expander ~dir tests =
| Some (p : Package.t) ->
Package.Name.Set.add acc.packages (Package.Id.name p.id)
in
{ acc with enabled_if; deps; alias; packages })
let locks =
(* Locks must be relative to the cram stanza directory and not
the individual tests direcories *)
List.fold_left ~init:acc.locks
~f:(fun acc lock ->
Expander.Static.expand_str expander lock
|> Path.relative (Path.build dir)
|> Path.Set.add acc)
spec.locks
in
{ acc with enabled_if; locks; deps; alias; packages })
in
let test_rule () = test_rule ~sctx ~expander ~dir effective test in
match !Clflags.only_packages with
Expand Down
8 changes: 7 additions & 1 deletion src/dune_rules/cram_stanza.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type t =
; alias : Alias.Name.t option
; deps : Dep_conf.t Bindings.t option
; enabled_if : Blang.t
; locks : String_with_vars.t list
; package : Package.t option
}

Expand All @@ -38,9 +39,14 @@ let decode =
and+ alias = field_o "alias" Alias.Name.decode
and+ deps = field_o "deps" (Bindings.decode Dep_conf.decode)
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:None ()
and+ locks =
field "locks"
(Dune_lang.Syntax.since Stanza.syntax (2, 9)
>>> repeat String_with_vars.decode)
~default:[]
and+ package =
Stanza_common.Pkg.field_opt
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 8))
()
in
{ loc; alias; deps; enabled_if; applies_to; package })
{ loc; alias; deps; enabled_if; locks; applies_to; package })
3 changes: 2 additions & 1 deletion src/dune_rules/cram_stanza.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ type applies_to =
| Files_matching_in_this_dir of Predicate_lang.Glob.t

type t =
{ loc : Loc.t
{ loc : Loc.t (* ; dir : Path.t *)
; applies_to : applies_to
; alias : Alias.Name.t option
; deps : Dep_conf.t Bindings.t option
; enabled_if : Blang.t
; locks : String_with_vars.t list
; package : Package.t option
}

Expand Down
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/tests-locks.t/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 2.9)

(cram enable)
9 changes: 9 additions & 0 deletions test/blackbox-tests/test-cases/tests-locks.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
These tests are run with locks. They should not end together (<> expected)
$ dune build --root=. -j 2 --diff-command=diff @all-tests 2>&1 |
> grep "^> *" | uniq -c | [ $(wc -l) -eq 1 ] && echo '=' || echo '<>'
<>

These tests are run without locks. They should end together (= expected)
$ dune build --root=. -j 2 --diff-command=diff @all-tests-nolocks 2>&1 |
> grep "^> *" | uniq -c | [ $(wc -l) -eq 1 ] && echo '=' || echo '<>'
=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ sleep 1 && date +%s
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(cram
(applies_to :whole_subtree)
(alias all-tests-nolocks))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ sleep 1 && date +%s
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/tests-locks.t/tests/a.t
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ sleep 1 && date +%s
4 changes: 4 additions & 0 deletions test/blackbox-tests/test-cases/tests-locks.t/tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(cram
(applies_to :whole_subtree)
(locks a)
(alias all-tests))
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/tests-locks.t/tests/sub/b.t
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ sleep 1 && date +%s

0 comments on commit 9d81999

Please sign in to comment.