#!/bin/bash
set -e

# run tests requiring network access
# (test_exodus.py pulls data files from github)

# allow for the possibility that MPI tests are run
# on systems with a single CPU
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe

# OpenMPI 5 often sends uphelpful warnings to stderr, which are not actually errors:
#   opal/mca/btl/tcp/btl_tcp_proc.c:266:mca_btl_tcp_proc_create_interface_graph] Unable to find reachable pairing between local and remote interfaces
# This env variable was identified as a workaround in Bug#1087784, https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1087784
export OMPI_MCA_btl_tcp_if_include=lo

n_proc=`nproc`
echo "${n_proc} CPUs are available"
for pyver in `py3versions -sv`; do
    # By default, pytest tmp_path retains the temporary directory for the last 3
    # pytest invocations.
    # Using the --basetemp option will remove the directory before every run,
    # effectively meaning the temporary directories of only the most recent run
    # will be kept. We definitely do not want to keep old runs, because they may
    # result in io4dolfinx trying to read in output files generated with a
    # different PETSc scalar type (real vs complex).

    echo "=== network tests for python $pyver (serial) ==="
    python${pyver} -m pytest -v --basetemp=${AUTOPKGTEST_TMP}/io4dolfinx${TMPDIR_TAG}_py${pyver} --color=no tests/test_exodus.py

    echo "=== network tests for python $pyver (MPI) ==="
    mpirun -n 2 python${pyver} -m pytest -v --basetemp=${AUTOPKGTEST_TMP}/io4dolfinx${TMPDIR_TAG}_py${pyver}_mpi --color=no tests/test_exodus.py

    echo "=== network tests for python $pyver (serial with complex numbers) ==="
    PETSC_DIR=/usr/lib/petscdir/petsc-complex python${pyver} -m pytest -v --basetemp=${AUTOPKGTEST_TMP}/io4dolfinx${TMPDIR_TAG}_py${pyver}_complex --color=no tests/test_exodus.py

    echo "=== network tests for python $pyver (MPI with complex numbers) ==="
    PETSC_DIR=/usr/lib/petscdir/petsc-complex mpirun -n 2 python${pyver} -m pytest -v --basetemp=${AUTOPKGTEST_TMP}/io4dolfinx${TMPDIR_TAG}_py${pyver}_complex_mpi --color=no tests/test_exodus.py
done
