diff --git a/python/test/pytest_common.py b/python/test/pytest_common.py index 3ae9c21ff8c..b190e4f3502 100644 --- a/python/test/pytest_common.py +++ b/python/test/pytest_common.py @@ -10,3 +10,54 @@ def pushd(path): yield os.chdir(cwd) +ENABLE_ASYNC_ECL_OUTPUT_FLAG = '--enable-async-ecl-output=false' + +def create_black_oil_simulator(*args, **kwargs): + """Create BlackOilSimulator with test-safe default arguments. + + Automatically disables async ECL output to prevent race conditions + with pushd context manager in tests. + """ + from opm.simulators import BlackOilSimulator + + flag_to_add = ENABLE_ASYNC_ECL_OUTPUT_FLAG + # Handle different constructor patterns + if 'args' in kwargs: + # Add our flag to existing args + if flag_to_add not in kwargs['args']: + kwargs['args'].append(flag_to_add) + elif len(args) >= 4: + # Constructor with deck, state, schedule, summary_config + # Add args parameter + if len(args) == 4: + args = args + ([flag_to_add],) + elif len(args) == 5: + # Args already provided, add our flag + args_list = list(args[4]) if args[4] else [] + if flag_to_add not in args_list: + args_list.append(flag_to_add) + args = args[:4] + (args_list,) + else: + # Constructor with filename - add args parameter + kwargs['args'] = kwargs.get('args', []) + if flag_to_add not in kwargs['args']: + kwargs['args'].append(flag_to_add) + + return BlackOilSimulator(*args, **kwargs) + +def create_gas_water_simulator(*args, **kwargs): + """Create GasWaterSimulator with test-safe default arguments. + + Automatically disables async ECL output to prevent race conditions + with pushd context manager in tests. + """ + from opm.simulators import GasWaterSimulator + + flag_to_add = ENABLE_ASYNC_ECL_OUTPUT_FLAG + # Add our flag to args + kwargs['args'] = kwargs.get('args', []) + if flag_to_add not in kwargs['args']: + kwargs['args'].append(flag_to_add) + + return GasWaterSimulator(*args, **kwargs) + diff --git a/python/test/test_basic.py b/python/test/test_basic.py index 95ae49035ef..865a6514f9d 100755 --- a/python/test/test_basic.py +++ b/python/test/test_basic.py @@ -1,8 +1,7 @@ import os import unittest from pathlib import Path -from opm.simulators import BlackOilSimulator, GasWaterSimulator -from .pytest_common import pushd +from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator class TestBasic(unittest.TestCase): @classmethod @@ -21,7 +20,7 @@ def setUpClass(cls): # 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 = create_black_oil_simulator(args=['--linear-solver=ilu0'], filename="SPE1CASE1.DATA") sim.setup_mpi(init=True, finalize=False) sim.step_init() sim.step() @@ -48,7 +47,7 @@ def test_01_blackoil(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 = create_gas_water_simulator(args=['--linear-solver=ilu0'], filename="SPE1CASE2_GASWATER.DATA") sim.setup_mpi(init=False, finalize=True) sim.step_init() sim.step() diff --git a/python/test/test_fluidstate_variables.py b/python/test/test_fluidstate_variables.py index 30bc8dbd6a0..be7bae7d82d 100644 --- a/python/test/test_fluidstate_variables.py +++ b/python/test/test_fluidstate_variables.py @@ -1,8 +1,7 @@ import os import unittest from pathlib import Path -from opm.simulators import BlackOilSimulator, GasWaterSimulator -from .pytest_common import pushd +from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator class TestBasic(unittest.TestCase): @classmethod @@ -21,7 +20,7 @@ def setUpClass(cls): # 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 = create_black_oil_simulator(filename="SPE1CASE1.DATA") sim.setup_mpi(True, False) sim.step_init() sim.step() @@ -53,7 +52,7 @@ def test_01_blackoil(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 = create_gas_water_simulator(filename="SPE1CASE2_GASWATER.DATA") sim.setup_mpi(False, True) sim.step_init() sim.step() diff --git a/python/test/test_mpi.py b/python/test/test_mpi.py index 9d4d91f41b1..93f9092344c 100644 --- a/python/test/test_mpi.py +++ b/python/test/test_mpi.py @@ -1,8 +1,7 @@ import os import unittest from pathlib import Path -from opm.simulators import BlackOilSimulator -from .pytest_common import pushd +from .pytest_common import pushd, create_black_oil_simulator class TestBasic(unittest.TestCase): @classmethod @@ -18,14 +17,14 @@ def setUpClass(cls): # are run in a given order. def test_01_mpi_init(self): with pushd(self.data_dir): - sim = BlackOilSimulator("SPE1CASE1.DATA") + sim = create_black_oil_simulator(filename="SPE1CASE1.DATA") sim.setup_mpi(init=True, finalize=False) sim.step_init() # This will create the OPM::Main() object which will call MPI_Init() assert True def test_02_mpi_no_init(self): with pushd(self.data_dir): - sim = BlackOilSimulator("SPE1CASE1.DATA") + sim = create_black_oil_simulator(filename="SPE1CASE1.DATA") sim.setup_mpi(init=False, finalize=True) sim.step_init() # This will create the OPM::Main() object which will not call MPI_Init() # That this test runs shows that the simulator does not call diff --git a/python/test/test_primary_variables.py b/python/test/test_primary_variables.py index f481c094d91..60558ed8b7b 100644 --- a/python/test/test_primary_variables.py +++ b/python/test/test_primary_variables.py @@ -1,8 +1,7 @@ import os import unittest from pathlib import Path -from opm.simulators import BlackOilSimulator, GasWaterSimulator -from .pytest_common import pushd +from .pytest_common import pushd, create_black_oil_simulator, create_gas_water_simulator class TestBasic(unittest.TestCase): @classmethod @@ -24,7 +23,7 @@ def setUpClass(cls): # 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 = create_black_oil_simulator(filename="SPE1CASE1.DATA") sim.setup_mpi(True, False) sim.step_init() sim.step() @@ -54,7 +53,7 @@ def test_01_blackoil(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 = create_gas_water_simulator(filename="SPE1CASE2_GASWATER.DATA") sim.setup_mpi(False, True) sim.step_init() sim.step() diff --git a/python/test/test_schedule.py b/python/test/test_schedule.py index f811b1ff1d0..13085ef2b24 100755 --- a/python/test/test_schedule.py +++ b/python/test/test_schedule.py @@ -3,7 +3,7 @@ import datetime as dt from pathlib import Path import re -from opm.simulators import BlackOilSimulator +from .pytest_common import create_black_oil_simulator from opm.io.parser import Parser from opm.io.ecl_state import EclipseState from opm.io.schedule import Schedule @@ -29,8 +29,8 @@ def test_all(self): self.assertTrue('INJ' in self.schedule) self.assertEqual(dt.datetime(2015, 1, 1), self.schedule.start) self.assertEqual(dt.datetime(2016, 1, 1), self.schedule.end) - self.sim = BlackOilSimulator( - self.deck, state, self.schedule, summary_config ) + self.sim = create_black_oil_simulator( + self.deck, state, self.schedule, summary_config) tsteps = self.schedule.timesteps self.assertEqual(dt.datetime(2015, 1, 1), tsteps[0]) last_step = len(tsteps) - 1 diff --git a/python/test/test_throw.py b/python/test/test_throw.py index 73513d10cc2..779bb3a83e8 100644 --- a/python/test/test_throw.py +++ b/python/test/test_throw.py @@ -1,8 +1,7 @@ import os import unittest from pathlib import Path -from opm.simulators import BlackOilSimulator -from .pytest_common import pushd +from .pytest_common import pushd, create_black_oil_simulator class TestBasic(unittest.TestCase): @classmethod @@ -12,7 +11,7 @@ def setUpClass(cls): def test_all(self): with pushd(self.data_dir): - sim = BlackOilSimulator("SPE1CASE1.DATA") + sim = create_black_oil_simulator(filename="SPE1CASE1.DATA") # NOTE: The following call should throw an exception since the simulation # has not been initialized with self.assertRaises(RuntimeError):