Clarify why test case ordering is necessary

and delete comment in code about static variables that can no longer be
reproduced.
This commit is contained in:
Håkon Hægland authored and Svenn Tveit committed 2025-09-17 10:06:49 +02:00
1 parent 67a701ef4f
commit b5e7ccc0b1
3 files changed
+26 -59

No files matched your search

+12 -45
View File
@@ -7,56 +7,22 @@ from .pytest_common import pushd
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
# NOTE: Loading the below extension module involves loading a
# a shared library like "simulators.cpython-37m-x86_64-linux-gnu.so"
# It turns out Python cannot unload this module, see:
#
# https://stackoverflow.com/a/8295590/2173773
# https://bugs.python.org/issue34309
#
# This is a problem when we want to create a new instance for each unit
# test. For example, when creating the first instance, static variables in
# in the shared object are initialized. However, when the
# corresponding Python object is later deleted (when the test finishes),
# the shared object is not unloaded and its static variables
# stays the same. So when a second Python instance is created,
# the same address is used for the static variables in the shared library
# i.e. the static variables are referring to the same memory location
# as for the first object (and they are not reinitialized).
#
# Unfortunatly, this leads to undefined behavior since the C++ code
# for flow simulation uses static variable to keep state information
# and since it was not built under the assumption that it would
# used as a shared library. It was assumed (?) that a flow simulation
# was executed from an executable file (not library file) and only
# executed once. To execute another simulation, it was assumed that
# the executable would be restarted from a controlling program like
# the Shell (which would reload and initialize the object into fresh memory).
#
# TODO: Fix the C++ code such that it allows multiple runs whith the same
# object file.
#
# NOTE: The result of the above is that we can only instantiate a
# single simulator object during the unit tests.
# This is not how the unittest module was supposed to be used. Usually one
# would write multiple test_xxx() methods that are independent and
# each method receives a new simulator object (also note that the order
# in which each test_xxx() method is called by unittest is not defined).
# However, as noted above this is not currently possible.
#
test_dir = Path(os.path.dirname(__file__))
cls.data_dir_bo = test_dir.parent.joinpath("test_data/SPE1CASE1a")
cls.data_dir_gw = test_dir.parent.joinpath("test_data/SPE1CASE2")
# IMPORTANT: Since all the python unittests run in the same process we must be
# careful to not call MPI_Init() more than once.
# Tests are run alphabetically, so we need to make sure that
# the the first test calls MPI_Init(), therefore the name of the tests
# have a numeric label like "01" in test_01_blackoil to ensure that they
# are run in a given order.
def test_all(self):
self._bo()
self._gw()
def _bo(self):
# IMPORTANT: This test must be run first since it calls MPI_Init()
def test_01_blackoil(self):
with pushd(self.data_dir_bo):
sim = BlackOilSimulator(args=['--linear-solver=ilu0'], filename="SPE1CASE1.DATA")
sim.setup_mpi(True, False)
sim.setup_mpi(init=True, finalize=False)
sim.step_init()
sim.step()
dt = sim.get_dt()
@@ -79,10 +45,11 @@ class TestBasic(unittest.TestCase):
poro2 = sim.get_porosity()
self.assertAlmostEqual(poro2[0], 0.285, places=7, msg='value of porosity 2')
def _gw(self):
# IMPORTANT: This test must be run last since it calls MPI_Finalize()
def test_99_gaswater(self):
with pushd(self.data_dir_gw):
sim = GasWaterSimulator(args=['--linear-solver=ilu0'], filename="SPE1CASE2_GASWATER.DATA")
sim.setup_mpi(False, True)
sim.setup_mpi(init=False, finalize=True)
sim.step_init()
sim.step()
dt = sim.get_dt() # 31 days = 31 * 24 * 60 * 60 = 2678400 seconds
+11 -10
View File
@@ -7,19 +7,19 @@ from .pytest_common import pushd
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
# NOTE: See comment in test_basic.py for the reason why we are
# only using a single test_all() function instead of splitting
# it up in multiple test functions
test_dir = Path(os.path.dirname(__file__))
cls.data_dir_bo = test_dir.parent.joinpath("test_data/SPE1CASE1a")
cls.data_dir_gw = test_dir.parent.joinpath("test_data/SPE1CASE2")
# IMPORTANT: Since all the python unittests run in the same process we must be
# careful to not call MPI_Init() more than once.
# Tests are run alphabetically, so we need to make sure that
# the the first test calls MPI_Init(), therefore the name of the tests
# have a numeric label like "01" in test_01_bo to ensure that they
# are run in a given order.
def test_all(self):
self._bo()
self._gw()
def _bo(self):
# IMPORTANT:This test must be run first since it calls MPI_Init()
def test_01_blackoil(self):
with pushd(self.data_dir_bo):
sim = BlackOilSimulator("SPE1CASE1.DATA")
sim.setup_mpi(True, False)
@@ -50,7 +50,8 @@ class TestBasic(unittest.TestCase):
T = sim.get_fluidstate_variable(name='T')
self.assertAlmostEqual(T[0], 288.705, places=3, msg='value of temperature')
def _gw(self):
# IMPORTANT: This test must be run last since it calls MPI_Finalize()
def test_99_gaswater(self):
with pushd(self.data_dir_gw):
sim = GasWaterSimulator("SPE1CASE2_GASWATER.DATA")
sim.setup_mpi(False, True)
@@ -67,4 +68,4 @@ class TestBasic(unittest.TestCase):
Sg = sim.get_fluidstate_variable(name='Sg')
self.assertAlmostEqual(Sg[0], 0.708648, places=3, msg='value of gas saturation')
Sw = sim.get_fluidstate_variable(name='Sw')
self.assertAlmostEqual(Sw[0], 0.291351, places=5, msg='value of water saturation')
self.assertAlmostEqual(Sw[0], 0.291351, places=5, msg='value of water saturation')
+3 -4
View File
@@ -7,13 +7,12 @@ from .pytest_common import pushd
class TestBasic(unittest.TestCase):
@classmethod
def setUpClass(cls):
# NOTE: See comment in test_basic.py for the reason why we are
# only using a single test_all() function instead of splitting
# it up in multiple test functions
test_dir = Path(os.path.dirname(__file__))
cls.data_dir = test_dir.parent.joinpath("test_data/SPE1CASE1a")
# IMPORTANT: Tests are run alphabetically, so we need to make sure that
# IMPORTANT: Since all the python unittests run in the same process we must be
# careful to not call MPI_Init() more than once.
# Tests are run alphabetically, so we need to make sure that
# the the first test calls MPI_Init(), therefore the name of the tests
# have a numeric label like "01" in test_01_mpi_init to ensure that they
# are run in a given order.