Skip to content

Commit

Permalink
ArithmeticAddCalculation: Add the metadata.options.sleep input (#…
Browse files Browse the repository at this point in the history
…5663)

When set, this will add a sleep in front of the main command. This is
useful for testing where the calculation needs to run at least for a
certain time in order for another component to get the chance to run. An
example is to test `CalcJob` monitors. Without the sleep, the job would
finish before they get the chance to analyze the output files.
  • Loading branch information
sphuber authored Sep 26, 2022
1 parent e4da380 commit d666332
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions aiida/calculations/arithmetic/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def define(cls, spec: CalcJobProcessSpec):
spec.input('x', valid_type=(orm.Int, orm.Float), help='The left operand.')
spec.input('y', valid_type=(orm.Int, orm.Float), help='The right operand.')
spec.output('sum', valid_type=(orm.Int, orm.Float), help='The sum of the left and right operand.')
spec.input('metadata.options.sleep', required=False, valid_type=int)
# set default options (optional)
spec.inputs['metadata']['options']['parser_name'].default = 'core.arithmetic.add'
spec.inputs['metadata']['options']['input_filename'].default = 'aiida.in'
Expand All @@ -50,6 +51,8 @@ def prepare_for_submission(self, folder: Folder) -> CalcInfo:
:returns: the `CalcInfo` instance
"""
with folder.open(self.options.input_filename, 'w', encoding='utf8') as handle:
if 'sleep' in self.options:
handle.write(f'sleep {self.options.sleep}\n')
handle.write(f'echo $(({self.inputs.x.value} + {self.inputs.y.value}))\n')

codeinfo = CodeInfo()
Expand Down
33 changes: 27 additions & 6 deletions tests/calculations/arithmetic/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@


@pytest.mark.requires_rmq
@pytest.mark.usefixtures('aiida_profile_clean')
@pytest.mark.usefixtures('aiida_profile')
def test_add_default(fixture_sandbox, aiida_localhost, generate_calc_job):
"""Test a default `ArithmeticAddCalculation`."""
entry_point_name = 'core.arithmetic.add'
inputs = {
'x': orm.Int(1),
'y': orm.Int(2),
'code': orm.InstalledCode(computer=aiida_localhost, filepath_executable='/bin/bash')
}

calc_info = generate_calc_job(fixture_sandbox, entry_point_name, inputs)
calc_info = generate_calc_job(fixture_sandbox, 'core.arithmetic.add', inputs)
options = ArithmeticAddCalculation.spec().inputs['metadata']['options']

# Check the attributes of the returned `CalcInfo`
Expand All @@ -49,10 +48,9 @@ def test_add_default(fixture_sandbox, aiida_localhost, generate_calc_job):


@pytest.mark.requires_rmq
@pytest.mark.usefixtures('aiida_profile_clean')
@pytest.mark.usefixtures('aiida_profile')
def test_add_custom_filenames(fixture_sandbox, aiida_localhost, generate_calc_job):
"""Test an `ArithmeticAddCalculation` with non-default input and output filenames."""
entry_point_name = 'core.arithmetic.add'
input_filename = 'custom.in'
output_filename = 'custom.out'
inputs = {
Expand All @@ -67,9 +65,32 @@ def test_add_custom_filenames(fixture_sandbox, aiida_localhost, generate_calc_jo
}
}

calc_info = generate_calc_job(fixture_sandbox, entry_point_name, inputs)
calc_info = generate_calc_job(fixture_sandbox, 'core.arithmetic.add', inputs)
code_info = calc_info.codes_info[0]

assert code_info.stdin_name == input_filename
assert code_info.stdout_name == output_filename
assert calc_info.retrieve_list == [output_filename]


@pytest.mark.requires_rmq
@pytest.mark.usefixtures('aiida_profile')
def test_sleep(fixture_sandbox, aiida_localhost, generate_calc_job):
"""Test the ``metadata.options.sleep`` input."""
sleep = 5
inputs = {
'x': orm.Int(1),
'y': orm.Int(2),
'code': orm.InstalledCode(computer=aiida_localhost, filepath_executable='/bin/bash'),
'metadata': {
'options': {
'sleep': sleep,
}
}
}

generate_calc_job(fixture_sandbox, 'core.arithmetic.add', inputs)

filename = ArithmeticAddCalculation.spec().inputs['metadata']['options']['input_filename'].default
with fixture_sandbox.open(filename) as handle:
assert handle.readlines()[0] == f'sleep {sleep}\n'

0 comments on commit d666332

Please sign in to comment.