2020-10-30 20:29:05 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
use air_test_utils::checked_call_vm;
|
2021-05-10 14:25:34 +03:00
|
|
|
use air_test_utils::create_avm;
|
2021-05-16 22:52:22 +03:00
|
|
|
use air_test_utils::executed_state;
|
2021-05-10 14:25:34 +03:00
|
|
|
use air_test_utils::set_variables_call_service;
|
2021-08-24 16:14:15 +03:00
|
|
|
use air_test_utils::trace_from_result;
|
2021-05-10 14:25:34 +03:00
|
|
|
use air_test_utils::unit_call_service;
|
|
|
|
use air_test_utils::CallServiceClosure;
|
|
|
|
use air_test_utils::IValue;
|
|
|
|
use air_test_utils::NEVec;
|
2020-10-30 20:29:05 +03:00
|
|
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn seq_par_call() {
|
2021-08-24 16:14:15 +03:00
|
|
|
let vm_peer_id = "some_peer_id";
|
|
|
|
let mut vm = create_avm(unit_call_service(), vm_peer_id);
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2020-12-28 00:12:11 +03:00
|
|
|
let script = format!(
|
2020-10-30 20:29:05 +03:00
|
|
|
r#"
|
2020-11-03 17:43:58 +03:00
|
|
|
(seq
|
|
|
|
(par
|
2020-12-28 00:12:11 +03:00
|
|
|
(call "{0}" ("local_service_id" "local_fn_name") [] result_1)
|
2020-11-03 17:43:58 +03:00
|
|
|
(call "remote_peer_id" ("service_id" "fn_name") [] g)
|
|
|
|
)
|
2020-12-28 00:12:11 +03:00
|
|
|
(call "{0}" ("local_service_id" "local_fn_name") [] result_2)
|
2020-11-03 17:43:58 +03:00
|
|
|
)"#,
|
2020-12-28 00:12:11 +03:00
|
|
|
vm_peer_id
|
2020-10-30 20:29:05 +03:00
|
|
|
);
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
let result = checked_call_vm!(vm, "asd", script, "", "");
|
|
|
|
let actual_trace = trace_from_result(&result);
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-05-16 22:52:22 +03:00
|
|
|
let test_string = "test";
|
2021-01-15 00:38:58 +03:00
|
|
|
let expected_trace = vec![
|
2021-05-16 22:52:22 +03:00
|
|
|
executed_state::par(1, 1),
|
|
|
|
executed_state::scalar_string(test_string),
|
|
|
|
executed_state::request_sent_by(vm_peer_id),
|
|
|
|
executed_state::scalar_string(test_string),
|
2020-10-30 20:29:05 +03:00
|
|
|
];
|
|
|
|
|
2021-01-15 00:38:58 +03:00
|
|
|
assert_eq!(actual_trace, expected_trace);
|
2021-08-24 16:14:15 +03:00
|
|
|
assert_eq!(result.next_peer_pks, vec![String::from("remote_peer_id")]);
|
2020-10-30 20:29:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn par_par_call() {
|
2021-08-24 16:14:15 +03:00
|
|
|
let vm_peer_id = "some_peer_id";
|
|
|
|
let mut vm = create_avm(unit_call_service(), vm_peer_id);
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2020-12-28 00:12:11 +03:00
|
|
|
let script = format!(
|
2020-10-30 20:29:05 +03:00
|
|
|
r#"
|
2020-11-03 17:43:58 +03:00
|
|
|
(par
|
|
|
|
(par
|
2020-12-28 00:12:11 +03:00
|
|
|
(call "{0}" ("local_service_id" "local_fn_name") [] result_1)
|
2020-11-03 17:43:58 +03:00
|
|
|
(call "remote_peer_id" ("service_id" "fn_name") [] g)
|
|
|
|
)
|
2020-12-28 00:12:11 +03:00
|
|
|
(call "{0}" ("local_service_id" "local_fn_name") [] result_2)
|
2020-11-03 17:43:58 +03:00
|
|
|
)"#,
|
2020-12-28 00:12:11 +03:00
|
|
|
vm_peer_id,
|
2020-10-30 20:29:05 +03:00
|
|
|
);
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
let result = checked_call_vm!(vm, "asd", script, "", "");
|
|
|
|
let actual_trace = trace_from_result(&result);
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-05-16 22:52:22 +03:00
|
|
|
let test_string = "test";
|
2021-01-15 00:38:58 +03:00
|
|
|
let expected_trace = vec![
|
2021-05-16 22:52:22 +03:00
|
|
|
executed_state::par(3, 1),
|
|
|
|
executed_state::par(1, 1),
|
|
|
|
executed_state::scalar_string(test_string),
|
|
|
|
executed_state::request_sent_by(vm_peer_id),
|
|
|
|
executed_state::scalar_string(test_string),
|
2020-10-30 20:29:05 +03:00
|
|
|
];
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
assert_eq!(actual_trace, expected_trace);
|
|
|
|
assert_eq!(result.next_peer_pks, vec![String::from("remote_peer_id")]);
|
2020-10-30 20:29:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_service() {
|
|
|
|
let module = "greeting";
|
|
|
|
let module_config = json!(
|
|
|
|
{
|
|
|
|
"name": module,
|
|
|
|
"mem_pages_count": 100,
|
|
|
|
"logger_enabled": true,
|
|
|
|
"wasi": {
|
|
|
|
"envs": json!({}),
|
|
|
|
"preopened_files": vec!["/tmp"],
|
|
|
|
"mapped_dirs": json!({}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
let module_bytes = json!([1, 2]);
|
|
|
|
let blueprint = json!({ "name": "blueprint", "dependencies": [module]});
|
|
|
|
|
|
|
|
let variables_mapping = maplit::hashmap!(
|
|
|
|
String::from("module_bytes") => module_bytes.to_string(),
|
|
|
|
String::from("module_config") => module_config.to_string(),
|
|
|
|
String::from("blueprint") => blueprint.to_string(),
|
|
|
|
);
|
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
let mut set_variables_vm = create_avm(set_variables_call_service(variables_mapping), "set_variables");
|
2020-10-30 20:29:05 +03:00
|
|
|
|
|
|
|
let add_module_response = String::from("add_module response");
|
|
|
|
let add_blueprint_response = String::from("add_blueprint response");
|
|
|
|
let create_response = String::from("create response");
|
|
|
|
|
2021-06-30 18:58:54 +03:00
|
|
|
let call_service: CallServiceClosure = Box::new(move |args| -> Option<IValue> {
|
|
|
|
let builtin_service = match &args.function_args[0] {
|
2020-10-30 20:29:05 +03:00
|
|
|
IValue::String(str) => str,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let response = match builtin_service.as_str() {
|
|
|
|
"add_module" => add_module_response.clone(),
|
|
|
|
"add_blueprint" => add_blueprint_response.clone(),
|
|
|
|
"create" => create_response.clone(),
|
|
|
|
_ => String::from("unknown response"),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(IValue::Record(
|
2021-01-12 13:04:25 +03:00
|
|
|
NEVec::new(vec![IValue::S32(0), IValue::String(format!("\"{}\"", response))]).unwrap(),
|
2020-10-30 20:29:05 +03:00
|
|
|
))
|
|
|
|
});
|
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
let mut vm = create_avm(call_service, "A");
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2020-12-22 21:05:04 +03:00
|
|
|
let script = include_str!("./scripts/create_service.clj");
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
let result = checked_call_vm!(set_variables_vm, "init_peer_id", script.clone(), "", "");
|
|
|
|
let result = checked_call_vm!(vm, "init_peer_id", script, "", result.data);
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-05-16 22:52:22 +03:00
|
|
|
let add_module_response = "add_module response";
|
|
|
|
let add_blueprint_response = "add_blueprint response";
|
|
|
|
let create_response = "create response";
|
2021-08-24 16:14:15 +03:00
|
|
|
|
|
|
|
let actual_trace = trace_from_result(&result);
|
2021-01-15 00:38:58 +03:00
|
|
|
let expected_trace = vec![
|
2021-08-24 16:14:15 +03:00
|
|
|
executed_state::scalar(module_bytes),
|
|
|
|
executed_state::scalar(module_config),
|
|
|
|
executed_state::scalar(blueprint),
|
2021-05-16 22:52:22 +03:00
|
|
|
executed_state::scalar_string(add_module_response),
|
|
|
|
executed_state::scalar_string(add_blueprint_response),
|
|
|
|
executed_state::scalar_string(create_response),
|
|
|
|
executed_state::request_sent_by("A"),
|
2020-10-30 20:29:05 +03:00
|
|
|
];
|
|
|
|
|
2021-01-15 00:38:58 +03:00
|
|
|
assert_eq!(actual_trace, expected_trace);
|
2021-08-24 16:14:15 +03:00
|
|
|
assert_eq!(result.next_peer_pks, vec![String::from("remote_peer_id")]);
|
2020-10-30 20:29:05 +03:00
|
|
|
}
|