Skip to content

Commit 22bdd75

Browse files
authored
TaskVine: add a test case to test library loading context from a library input file. (#4236)
* Add test case for context loading from file in library. * Fix comment
1 parent 99bd5a1 commit 22bdd75

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

taskvine/test/TR_vine_python_serverless.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PORT_FILE=vine.port
1414
PYTHON_SCRIPT=vine_python_serverless.py
1515
TEST_INPUT_FILE=${PYTHON_SCRIPT}.input
1616
TEST_OUTPUT_FILE=${PYTHON_SCRIPT}.output
17+
TEST_CONTEXT_INPUT_FILE=${PYTHON_SCRIPT}.context.libtask.input
1718

1819
check_needed()
1920
{
@@ -36,8 +37,9 @@ prepare()
3637
{
3738
rm -f $STATUS_FILE
3839
rm -f $PORT_FILE
39-
rm -f $TEST_INPUT_FILE
40-
rm -f $TEST_OUTPUT_FILE
40+
rm -f $TEST_INPUT_FILE
41+
rm -f $TEST_OUTPUT_FILE
42+
rm -f $TEST_CONTEXT_INPUT_FILE
4143
return 0
4244
}
4345

@@ -82,10 +84,11 @@ clean()
8284
{
8385
rm -f $STATUS_FILE
8486
rm -f $PORT_FILE
85-
rm -f $TEST_INPUT_FILE
86-
rm -f $TEST_OUTPUT_FILE
87+
rm -f $TEST_INPUT_FILE
88+
rm -f $TEST_OUTPUT_FILE
89+
rm -f $TEST_CONTEXT_INPUT_FILE
8790
rm -rf vine-run-info
88-
rm worker.log
91+
rm worker.log
8992
exit 0
9093
}
9194

taskvine/test/vine_python_serverless.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def func_copy_input_to_output(input_filename, output_filename):
4949
f.write(contents)
5050
return 0
5151

52+
def func_load_partial_data_from_file(filename):
53+
with open(filename, 'r') as f:
54+
contents = f.read()
55+
contents = contents.strip()
56+
partial_value = sum([int(x) for x in contents.split(' ')])
57+
return {'partial_value': partial_value}
58+
59+
def compute_final_value(value):
60+
partial_value = load_variable_from_library('partial_value')
61+
return partial_value + value
62+
5263
def main():
5364
parser = argparse.ArgumentParser("Test for taskvine python bindings.")
5465
parser.add_argument("port_file", help="File to write the port the queue is using.")
@@ -88,6 +99,14 @@ def main():
8899
# this is to test the input/output staging of function calls
89100
libtask_with_io_fn = q.create_library_from_functions('test-library-with-io-fn', func_copy_input_to_output, add_env=False, exec_mode='fork')
90101

102+
# define a function that loads partial data from a file in the library.
103+
# this is to test the context loading of the library.
104+
test_filename_for_libtask_with_context_from_file = os.path.basename(__file__) + '.context.libtask.input'
105+
with open(test_filename_for_libtask_with_context_from_file, 'w') as f:
106+
print('1 2', file=f)
107+
libtask_with_context_from_file = q.create_library_from_functions('test-library-with-context-from-file', compute_final_value, add_env=False, exec_mode='fork', library_context_info=[func_load_partial_data_from_file, [test_filename_for_libtask_with_context_from_file], {}])
108+
taskvine_file_for_libtask_with_context_from_file = q.declare_file(test_filename_for_libtask_with_context_from_file)
109+
libtask_with_context_from_file.add_input(taskvine_file_for_libtask_with_context_from_file, test_filename_for_libtask_with_context_from_file)
91110
# Just take default resources for the library, this will cause it to fill the whole worker.
92111
# And the number of functions slots will match the number of cores available.
93112

@@ -97,12 +116,14 @@ def main():
97116
q.install_library(libtask_with_context_fork)
98117
q.install_library(libtask_with_special_fns)
99118
q.install_library(libtask_with_io_fn)
119+
q.install_library(libtask_with_context_from_file)
100120
lib_task_names = ['test-library-no-context-direct',
101121
'test-library-no-context-fork',
102122
'test-library-with-context-direct',
103123
'test-library-with-context-fork',
104124
'test-library-with-special-fns',
105-
'test-library-with-io-fn']
125+
'test-library-with-io-fn',
126+
'test-library-with-context-from-file']
106127
print("Submitting function call tasks...")
107128

108129
tasks = 100
@@ -130,6 +151,12 @@ def main():
130151
s_task.add_output(output_file, output_filename)
131152
q.submit(s_task)
132153

154+
# do this test once only
155+
break
156+
elif lib_name == 'test-library-with-context-from-file':
157+
s_task = vine.FunctionCall(lib_name, 'compute_final_value', 3)
158+
q.submit(s_task)
159+
133160
# do this test once only
134161
break
135162
else:
@@ -161,7 +188,8 @@ def main():
161188
with_context_direct_expected = (tasks * (divide(2, 2**2) + double(3) + cube(4) + base_val * 3))
162189
with_context_fork_expected = (tasks * (divide(2, 2**2) + double(3) + cube(4) + base_val * 3))
163190
special_fns_expected = (tasks * (lambda_fn(1) + dyn_fn(1)))
164-
expected = no_context_direct_expected + no_context_fork_expected + with_context_direct_expected + with_context_fork_expected + special_fns_expected
191+
with_context_from_file_expected = (1 + 2 + 3) # 1 and 2 from the context loaded from the library input file, and 3 from the function call
192+
expected = no_context_direct_expected + no_context_fork_expected + with_context_direct_expected + with_context_fork_expected + special_fns_expected + with_context_from_file_expected
165193

166194
print(f"Total: {total_sum}")
167195
print(f"Expected: {expected}")

0 commit comments

Comments
 (0)