Skip to content

Commit

Permalink
pytest: Temporary Working Directory (#424)
Browse files Browse the repository at this point in the history
* Fix: pytest Wait 1 Sec

Simetimes our tests are so quick that they
finish below 1second. That does not work with
`UniqueStrings` in AMReX' `UtilCreateCleanDirectory`,
which can only do one name per second:
https://github.com/AMReX-Codes/amrex/blob/23.08/Src/Base/AMReX_Utility.cpp#L186-L199

If the target exist, the behavior of `std::rename` is
implementation dependent. We see crashes of this
on macOS.
https://en.cppreference.com/w/cpp/io/c/rename

* Use Temporary Working Directory

Avoid clean-up needs by using a cwd that is unique per test.

* All the yielded results in the cwd context

* Set CWD of runner
  • Loading branch information
ax3l authored Aug 30, 2023
1 parent 00861a0 commit 4a52d2f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
42 changes: 24 additions & 18 deletions tests/python/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

import os

import pytest

import amrex.space3d as amr
Expand All @@ -12,23 +14,27 @@
else:
print("NO mpi4py load")

# base path for input files
basepath = os.getcwd()


@pytest.fixture(autouse=True, scope="function")
def amrex_init():
amr.initialize(
[
# print AMReX status messages
"amrex.verbose=2",
# throw exceptions and create core dumps instead of
# AMReX backtrace files: allows to attach to
# debuggers
"amrex.throw_exception=1",
"amrex.signal_handling=0",
# abort GPU runs if out-of-memory instead of swapping to host RAM
"amrex.abort_on_out_of_gpu_memory=1",
# do not rely on implicit host-device memory transfers
"amrex.the_arena_is_managed=0",
]
)
yield
amr.finalize()
def amrex_init(tmpdir):
with tmpdir.as_cwd():
amr.initialize(
[
# print AMReX status messages
"amrex.verbose=2",
# throw exceptions and create core dumps instead of
# AMReX backtrace files: allows to attach to
# debuggers
"amrex.throw_exception=1",
"amrex.signal_handling=0",
# abort GPU runs if out-of-memory instead of swapping to host RAM
"amrex.abort_on_out_of_gpu_memory=1",
# do not rely on implicit host-device memory transfers
"amrex.the_arena_is_managed=0",
]
)
yield
amr.finalize()
3 changes: 2 additions & 1 deletion tests/python/test_charge_deposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import math

from conftest import basepath
import matplotlib.pyplot as plt
import numpy as np

Expand All @@ -24,7 +25,7 @@ def test_charge_deposition(save_png=True):
sim = impactx.ImpactX()

sim.n_cell = [16, 24, 32]
sim.load_inputs_file("examples/fodo/input_fodo.in")
sim.load_inputs_file(basepath + "/examples/fodo/input_fodo.in")
sim.space_charge = True
sim.slice_step_diagnostics = False

Expand Down
5 changes: 3 additions & 2 deletions tests/python/test_impactx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
# -*- coding: utf-8 -*-

from conftest import basepath
import numpy as np
import pytest

Expand All @@ -20,7 +21,7 @@ def test_impactx_fodo_file():
"""
sim = ImpactX()

sim.load_inputs_file("examples/fodo/input_fodo.in")
sim.load_inputs_file(basepath + "/examples/fodo/input_fodo.in")

sim.init_grids()
sim.init_beam_distribution_from_inputs()
Expand Down Expand Up @@ -221,7 +222,7 @@ def test_impactx_no_elements():
"""
sim = ImpactX()

sim.load_inputs_file("examples/fodo/input_fodo.in")
sim.load_inputs_file(basepath + "/examples/fodo/input_fodo.in")

sim.init_grids()
sim.init_beam_distribution_from_inputs()
Expand Down

0 comments on commit 4a52d2f

Please sign in to comment.