Skip to content

Commit

Permalink
Add test for Py Get exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
eisenhauer committed Mar 19, 2023
1 parent 7acf7b9 commit 740c5bf
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions testing/adios2/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python_add_test(NAME Bindings.Python.HighLevelAPI.Serial SCRIPT TestHighLevelAPI

python_add_test(NAME Bindings.Python.BPWriteReadTypes.Serial SCRIPT TestBPWriteReadTypes_nompi.py)
python_add_test(NAME Bindings.Python.BPSelectSteps.Serial SCRIPT TestBPSelectSteps_nompi.py)
python_add_test(NAME Bindings.Python.TypeExceptionOnGet.Serial SCRIPT TestGetException_nompi.py)

if(ADIOS2_HAVE_MPI)
add_python_mpi_test(BPWriteReadTypes)
Expand Down
133 changes: 133 additions & 0 deletions testing/adios2/bindings/python/TestGetException_nompi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

import numpy as np
import logging
import adios2

if __name__ == '__main__':
__spec__ = None


def main():

print("====================================")

format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
writer()
reader_fail()
reader_success()


def writer():

logging.info(" Writer: initiating writing")
data = np.arange(3, dtype=np.float32)
logging.info(f" data dtypes: {data.dtype!s}")
shape = data.shape
count = shape
start = (0,)*len(shape)
logging.info(f" data on writer side {data!s}")

adios_io = adios2.ADIOS()
IO = adios_io.DeclareIO("writer")

writer = IO.Open("testdatafile", adios2.Mode.Write)
writebuffer = IO.DefineVariable("np_data", data, shape, start,
count, adios2.ConstantDims)
if writebuffer:
writer.BeginStep()
writer.Put(writebuffer, data, adios2.Mode.Sync)
writer.EndStep()
else:
raise ValueError("DefineVariable failed")

writer.Close()

logging.info(" Writer: writing finished")


def reader_fail():

adios_io = adios2.ADIOS()
io = adios_io.DeclareIO("reader")

logging.info(" Reader: initiating erroneous reading ")
reader = io.Open("testdatafile", adios2.Mode.Read)
while True:
stepStatus = reader.BeginStep()
if stepStatus == adios2.StepStatus.OK:
# inquire for variable
recvar = io.InquireVariable("np_data")
if recvar:
got_exception = 0
# determine the shape of the data that will be sent
bufshape = recvar.Shape()
typ = recvar.Type()
logging.info(" Incoming variable type is " + typ)
# allocate buffer for new numpy
data = np.ones(bufshape)
logging.info(f" receive buffer dtype: {data.dtype!s}")
try:
reader.Get(recvar, data, adios2.Mode.Sync)
except Exception as e:
got_exception = 1
logging.info(str(e))
if not got_exception:
raise Exception("Didn't get an exception as expected")
else:
logging.info(" Got expected exception")
else:
raise ValueError("InquireVariable failed")
elif stepStatus == adios2.StepStatus.EndOfStream:
break
else:
raise StopIteration(f"next step failed to initiate {stepStatus!s}")
reader.EndStep()
reader.Close()
logging.info(" Reader: finished reading",)


def reader_success():

adios_io = adios2.ADIOS()
io = adios_io.DeclareIO("reader")

logging.info(" Reader: initiating successful reading ")
reader = io.Open("testdatafile", adios2.Mode.Read)
while True:
stepStatus = reader.BeginStep()
if stepStatus == adios2.StepStatus.OK:
# inquire for variable
recvar = io.InquireVariable("np_data")
if recvar:
got_exception = 0
# determine the shape of the data that will be sent
bufshape = recvar.Shape()
typ = recvar.Type()
logging.info(" Incoming variable type is " + typ)
# allocate buffer for new numpy
data = np.ones(bufshape, dtype=np.float32)
logging.info(f" receive buffer dtype: {data.dtype!s}")
try:
reader.Get(recvar, data, adios2.Mode.Sync)
except Exception as e:
got_exception = 1
logging.info(str(e))
if got_exception:
raise Exception(" Got unexpected exception")
else:
logging.info(" No exception as expected")
else:
raise ValueError("InquireVariable failed")
elif stepStatus == adios2.StepStatus.EndOfStream:
break
else:
raise StopIteration(f"next step failed to initiate {stepStatus!s}")
reader.EndStep()
reader.Close()
logging.info(" Reader: finished reading",)


if __name__ == "__main__":
main()

0 comments on commit 740c5bf

Please sign in to comment.