marine/core/tests/redis_sqlite.rs

208 lines
6.8 KiB
Rust
Raw Permalink Normal View History

2020-05-02 15:34:48 +00:00
/*
2024-06-26 09:37:14 +00:00
* Marine WebAssembly runtime
2020-05-02 15:34:48 +00:00
*
2024-06-26 09:37:14 +00:00
* Copyright (C) 2024 Fluence DAO
2020-05-02 15:34:48 +00:00
*
2024-06-26 09:37:14 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation version 3 of the
* License.
2020-05-02 15:34:48 +00:00
*
2024-06-26 09:37:14 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-05-02 15:34:48 +00:00
*/
2022-06-09 14:00:19 +00:00
use marine_core::MarineCore;
use marine_core::MarineCoreConfig;
2022-06-09 14:00:19 +00:00
use marine_core::IValue;
use marine_wasm_backend_traits::WasmBackend;
use marine_wasmtime_backend::WasmtimeWasmBackend;
2020-05-02 15:34:48 +00:00
const REDIS_DOWNLOAD_URL: &str =
"https://github.com/fluencelabs/redis/releases/download/v0.14.0_w/redis.wasm";
2020-05-02 15:34:48 +00:00
const SQLITE_DOWNLOAD_URL: &str =
"https://github.com/fluencelabs/sqlite/releases/download/v0.14.0_w/sqlite3.wasm";
2020-05-02 15:34:48 +00:00
2020-08-23 22:00:38 +00:00
pub async fn download(url: &str) -> bytes::Bytes {
reqwest::get(url)
.await
.expect("failed to download redis")
.bytes()
.await
.expect("failed to convert response to bytes")
}
2020-05-02 15:34:48 +00:00
#[tokio::test]
async fn redis() {
2020-08-23 22:00:38 +00:00
let wasm_bytes = download(REDIS_DOWNLOAD_URL).await;
2020-05-02 15:34:48 +00:00
let backend = WasmtimeWasmBackend::new_async().unwrap();
let mut marine_core = MarineCore::new(MarineCoreConfig::new(backend, None)).unwrap();
2020-05-02 15:34:48 +00:00
let module_name = "redis";
2020-08-23 01:04:11 +00:00
let config = <_>::default();
2020-05-02 15:34:48 +00:00
2022-06-09 14:00:19 +00:00
marine_core
2021-05-10 09:51:22 +00:00
.load_module(module_name, wasm_bytes.as_ref(), config)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("can't load a module into Marine: {:?}", e));
2020-05-02 15:34:48 +00:00
2022-06-09 14:00:19 +00:00
let result1 = marine_core
.call_async(
2020-08-23 01:33:05 +00:00
module_name,
"invoke",
&[IValue::String(String::from("SET A 10"))],
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2022-06-09 14:00:19 +00:00
let result2 = marine_core
.call_async(
2020-08-23 01:33:05 +00:00
module_name,
"invoke",
&[IValue::String(String::from("SADD B 20"))],
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2022-06-09 14:00:19 +00:00
let result3 = marine_core
.call_async(
2020-08-23 01:33:05 +00:00
module_name,
"invoke",
&[IValue::String(String::from("GET A"))],
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2022-06-09 14:00:19 +00:00
let result4 = marine_core
.call_async(
2020-08-23 01:33:05 +00:00
module_name,
"invoke",
&[IValue::String(String::from("SMEMBERS B"))],
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2022-06-09 14:00:19 +00:00
let result5 = marine_core
.call_async(
2020-08-23 01:33:05 +00:00
module_name,
"invoke",
&[IValue::String(String::from(
"eval \"redis.call('incr', 'A') return redis.call('get', 'A') * 8 + 5\" 0",
))],
)
.await
2021-05-10 09:51:22 +00:00
.expect("error while Marine invocation");
2020-05-02 15:34:48 +00:00
2020-08-23 01:04:11 +00:00
assert_eq!(result1, vec![IValue::String(String::from("+OK\r\n"))]);
assert_eq!(result2, vec![IValue::String(String::from(":1\r\n"))]);
assert_eq!(result3, vec![IValue::String(String::from("$2\r\n10\r\n"))]);
2020-08-23 01:33:05 +00:00
assert_eq!(
result4,
vec![IValue::String(String::from("*1\r\n$2\r\n20\r\n"))]
);
2020-08-23 01:04:11 +00:00
assert_eq!(result5, vec![IValue::String(String::from(":93\r\n"))]);
2020-05-02 15:34:48 +00:00
}
#[tokio::test]
async fn sqlite() {
2020-08-23 22:00:38 +00:00
let wasm_bytes = download(SQLITE_DOWNLOAD_URL).await;
2020-05-02 15:34:48 +00:00
let backend = WasmtimeWasmBackend::new_async().unwrap();
let mut marine_core = MarineCore::new(MarineCoreConfig::new(backend, None)).unwrap();
2020-05-02 15:34:48 +00:00
let module_name = "sqlite";
2020-08-23 01:04:11 +00:00
let config = <_>::default();
2020-05-02 15:34:48 +00:00
2022-06-09 14:00:19 +00:00
marine_core
2021-05-10 09:51:22 +00:00
.load_module(module_name, wasm_bytes.as_ref(), config)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("can't load a module into Marine: {:?}", e));
2020-05-02 15:34:48 +00:00
2022-06-09 14:00:19 +00:00
let mut result1 = marine_core
.call_async(
2020-05-02 15:34:48 +00:00
module_name,
2021-03-16 10:51:59 +00:00
"sqlite3_open_v2",
&[
IValue::String(String::from(":memory:")),
IValue::S32(6),
IValue::String(String::new()),
],
2020-05-02 15:34:48 +00:00
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2021-03-16 10:51:59 +00:00
let mut record_values = match result1.remove(0) {
IValue::Record(value) => value.into_vec(),
_ => panic!("return result should have record type"),
};
let db_handle = match record_values.remove(1) {
IValue::U32(value) => value,
_ => panic!("db handle should have u32 type"),
};
2022-06-09 14:00:19 +00:00
let mut result1 = marine_core
.call_async(
2020-05-02 15:34:48 +00:00
module_name,
2021-03-16 10:51:59 +00:00
"sqlite3_exec",
&[
IValue::U32(db_handle),
IValue::String(String::from("CREATE VIRTUAL TABLE users USING FTS5(body)")),
IValue::S32(0),
IValue::S32(0),
],
2020-05-02 15:34:48 +00:00
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2021-03-16 10:51:59 +00:00
2022-06-09 14:00:19 +00:00
let mut result2 = marine_core
.call_async(
2020-05-02 15:34:48 +00:00
module_name,
2021-03-16 10:51:59 +00:00
"sqlite3_exec",
&[
IValue::U32(db_handle),
IValue::String(String::from(
"INSERT INTO users(body) VALUES('AB'), ('BC'), ('CD'), ('DE')",
)),
IValue::S32(0),
IValue::S32(0),
],
2020-05-02 15:34:48 +00:00
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2020-05-02 15:34:48 +00:00
2022-06-09 14:00:19 +00:00
let mut result3 = marine_core
.call_async(
2021-03-16 10:51:59 +00:00
module_name,
"sqlite3_exec",
&[
IValue::U32(db_handle),
IValue::String(String::from(
"SELECT * FROM users WHERE users MATCH 'A* OR B*'",
)),
IValue::S32(0),
IValue::S32(0),
],
)
.await
2021-05-10 09:51:22 +00:00
.unwrap_or_else(|e| panic!("error while Marine invocation: {:?}", e));
2021-03-16 10:51:59 +00:00
let result1 = match result1.remove(0) {
IValue::Record(value) => value.into_vec(),
_ => panic!("result should have record type"),
};
assert_eq!(result1, vec![IValue::S32(0), IValue::String(String::new())]);
let result2 = match result2.remove(0) {
IValue::Record(value) => value.into_vec(),
_ => panic!("result should have record type"),
};
assert_eq!(result2, vec![IValue::S32(0), IValue::String(String::new())]);
let result3 = match result3.remove(0) {
IValue::Record(value) => value.into_vec(),
_ => panic!("result should have record type"),
};
assert_eq!(result3, vec![IValue::S32(0), IValue::String(String::new())]);
2020-05-02 15:34:48 +00:00
}