Peano
Loading...
Searching...
No Matches
test_observer_task_groups.py
Go to the documentation of this file.
1import re
2
3import pytest
4
6
7
8def _generate_observer(tmp_path, task_groups):
9 tmp_path.mkdir(parents=True, exist_ok=True)
11 "MyObserver",
12 ["project", "observers"],
13 "observers",
14 ["project::observers::A", "project::observers::B", "project::observers::C"],
15 [],
16 [],
17 [],
18 task_groups,
19 )
20 observer.generate(peano4.api.output.Overwrite.Always, str(tmp_path))
21 return (tmp_path / "observers" / "MyObserver.cpp").read_text()
22
23
24def _function(code, name):
25 """Body of the function name in the generated code"""
26 start = code.index(f"::{name}(")
27 return code[start : code.index("\n}\n", start)]
28
29
31 default = _generate_observer(tmp_path / "default", None)
32 singletons = _generate_observer(tmp_path / "singletons", [[0], [1], [2]])
33 assert default == singletons
34 assert "spawnAndWait" not in _function(default, "enterCell")
35
36
37@pytest.mark.parametrize("function, order", [("enterCell", [0, 1, 2]), ("leaveCell", [2, 1, 0])])
38def test_task_group_is_spawned_and_waited_for(tmp_path, function, order):
39 body = _function(_generate_observer(tmp_path, [[0, 1], [2]]), function)
40 assert body.count("tarch::multicore::spawnAndWait( parallelActionSets );") == 1
41 assert "std::function<void()> invokeActionSet0 = [&]() -> void {" in body
42 assert "std::function<void()> invokeActionSet1 = [&]() -> void {" in body
43 assert "invokeActionSet2" not in body
44 # Action sets are still invoked in order (reversed in leaveCell), and
45 # action set 2 is invoked after the wait in enterCell, before the group
46 # in leaveCell
47 positions = [body.index(f"_actionSet{index}.") for index in order]
48 assert positions == sorted(positions)
49 wait = body.index("spawnAndWait")
50 assert (body.index("_actionSet2.") > wait) == (function == "enterCell")
51
52
54 with pytest.raises(Exception):
55 _generate_observer(tmp_path, [[1, 0], [2]])
C++ observer of one mesh traversal.
Definition Observer.py:17
test_task_groups_have_to_enumerate_all_action_sets_in_order(tmp_path)
test_task_group_is_spawned_and_waited_for(tmp_path, function, order)
_function(code, name)
Body of the function name in the generated code.
_generate_observer(tmp_path, task_groups)