Add multiservice marine_test example (#25), update crate versions

This commit is contained in:
Valery Antopol 2021-10-11 21:33:32 +03:00 committed by GitHub
parent d52f06dfc3
commit db377aa46e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 321 additions and 26 deletions

View File

@ -11,12 +11,12 @@ name = "process_files"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
log = "0.4.14"
rand = "0.8.4"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"
[profile.release]
opt-level = "s"

View File

@ -10,6 +10,6 @@ name = "echo_service"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version="0.6.10", feature=["log"]}
marine-rs-sdk = { version="0.6.14", feature=["log"]}
log = "0.4.14"

View File

@ -12,6 +12,6 @@ name = "greeting"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version="0.6.10", feature=["log"]}
marine-rs-sdk = { version="0.6.14", feature=["log"]}

View File

@ -9,5 +9,5 @@ name = "secure_greeter"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version="0.6.10", feature=["log"]}
marine-rs-sdk = { version="0.6.14", feature=["log"]}
log = "0.4.14"

View File

@ -11,11 +11,11 @@ name = "utilities"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
log = "0.4.14"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"
[dev]
[profile.release]

View File

@ -10,5 +10,5 @@ path = "src/main.rs"
name = "curl_adapter"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
log = "0.4.8"

View File

@ -11,7 +11,7 @@ name = "mean_service"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
log = "0.4.14"
[dev]

View File

@ -11,7 +11,7 @@ name = "price_getter_service"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
log = "0.4.14"
picorand = "0.1.1"
fstrings = "0.2.3"

View File

@ -11,11 +11,11 @@ name = "ts_oracle"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
log = "0.4.14"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"
[dev]
[profile.release]

View File

@ -498,6 +498,154 @@ result: String("Ok")
You can build, inspect and test the project as outlined in the [Greeting Example](#Greeting-Example).
## Multiservice Marine Test example
In the examples above we used the `marine_test` macro to test a service. This example illustrates another ability of the `marine_test` macro: testing several services at once. To show that we will create couple of services:
a producer service which will create some data, and a consumer service which will process the data. Then write a test that ensures that the consumer properly processed data from the producer.
Let's look at the services and test now:
Producer:
```rust
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub struct Input {
pub first_name: String,
pub last_name: String,
}
#[marine]
pub fn produce(data: Input) -> Data {
Data {
name: format!("{} {}", data.first_name, data.last_name),
}
}
```
The only new thing here is `#[marine]` on a struct. This adds the structure into module interface and requires all the fields to be supported by default or be another `#[marine]` structure. There is more information about it in [docs](https://doc.fluence.dev/docs/knowledge_aquamarine/marine/marine-rs-sdk#function-export).
Consumer:
```rust
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub fn consume(data: Data) -> String {
data.name
}
```
There is nothing special.
The test will show what we can do with them:
```rust
fn main() {}
#[cfg(test)]
mod tests {
use marine_rs_sdk_test::marine_test;
#[marine_test(
producer(
config_path = "../producer/Config.toml", // <- 1
modules_dir = "../producer/artifacts"
),
consumer(
config_path = "../consumer/Config.toml",
modules_dir = "../consumer/artifacts"
)
)]
fn test() {
let mut producer = marine_test_env::producer::ServiceInterface::new(); // <- 2
let mut consumer = marine_test_env::consumer::ServiceInterface::new();
let input = marine_test_env::producer::Input { // <- 3
first_name: String::from("John"),
last_name: String::from("Doe"),
};
let data = producer.produce(input); // <- 4
let result = consumer.consume(data); // <- 5
assert_eq!(result, "John Doe")
}
}
```
As the `marine_test` needs only compiled .wasm files and not the code, the test can be in any project. In this case the test is in a separate crate.
We describe the services as named pairs of config file and directory with .wasm files(1), then in test function we create services(2). Please note, that each service creates its own `marine` runtime. Then, we create a structure(3) to pass it to a function from interface of producer service(4) and finally pass its result to the consumer service. The `ServiceInterface` and interface structures are accessed through `marine_test_env` — the module defined by the `marine_test` macro. The functions in turn are accessed through the `ServiceInterace` instance.
Now we can build services:
```shell
cd multiservice_marine_test
./build.sh
```
And run the test:
```shell
$ cargo test
Finished test [unoptimized + debuginfo] target(s) in 0.25s
Running unittests (target/debug/deps/multiservice_marine_test-533b3dcdcf22d98d)
running 1 test
test tests::test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 9.59s
```
We can do the same in the `mrepl`. But doing it to test services during the development will be a huge pain.
First, pass data to the producer:
```shell
$ cd producer
$ mrepl Config.toml
Welcome to the Marine REPL (version 0.9.1)
Minimal supported versions
sdk: 0.6.0
interface-types: 0.20.0
app service was created with service id = 8032487e-3348-406d-8fc7-02b927acb932
elapsed time 85.863792ms
1> call producer produce [{"first_name": "John", "last_name": "Doe"}]
result: Object({"name": String("John Doe")})
elapsed time: 16.718ms
```
Second, pass the result to the consumer:
```shell
$ cd ../consumer
$ mrepl Config.toml
Welcome to the Marine REPL (version 0.9.1)
Minimal supported versions
sdk: 0.6.0
interface-types: 0.20.0
app service was created with service id = f4a1e021-07e3-42e0-8dcb-66cfc69e3ee7
elapsed time 78.382834ms
1> call consumer consume [{"name": "John Doe"}]
result: String("John Doe")
elapsed time: 4.946209ms
```
S
## Deploying Services

View File

@ -10,7 +10,7 @@ name = "call_parameters"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"

View File

@ -12,7 +12,7 @@ name = "greeting"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"

View File

@ -0,0 +1,7 @@
.DS_Store
.repl_history
/target
**/**.bak
**/**.bk
/artifacts
keypair.json

View File

@ -0,0 +1,11 @@
[package]
name = "multiservice-marine-test"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2018"
publish = false
[dependencies]
[dev-dependencies]
marine-rs-sdk-test = "0.3.0"

View File

@ -0,0 +1,4 @@
#!/bin/sh
(cd producer; ./build.sh)
(cd consumer; ./build.sh)

View File

@ -0,0 +1,13 @@
[package]
name = "consumer"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2018"
publish = false
[[bin]]
name = "consumer"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.14"

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "consumer"
mem_pages_count = 50
logger_enabled = false

View File

@ -0,0 +1,10 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
cargo update --aggressive
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp target/wasm32-wasi/release/consumer.wasm artifacts/

View File

@ -0,0 +1,16 @@
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub fn consume(data: Data) -> String {
data.name
}

View File

@ -0,0 +1,13 @@
[package]
name = "producer"
version = "0.1.0"
authors = ["Fluence Labs"]
edition = "2018"
publish = false
[[bin]]
name = "producer"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.14"

View File

@ -0,0 +1,6 @@
modules_dir = "artifacts/"
[[module]]
name = "producer"
mem_pages_count = 50
logger_enabled = false

View File

@ -0,0 +1,10 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
cargo update --aggressive
marine build --release
rm artifacts/* || true
mkdir -p artifacts
cp target/wasm32-wasi/release/producer.wasm artifacts/

View File

@ -0,0 +1,24 @@
use marine_rs_sdk::marine;
use marine_rs_sdk::module_manifest;
module_manifest!();
pub fn main() {}
#[marine]
pub struct Data {
pub name: String,
}
#[marine]
pub struct Input {
pub first_name: String,
pub last_name: String,
}
#[marine]
pub fn produce(data: Input) -> Data {
Data {
name: format!("{} {}", data.first_name, data.last_name),
}
}

View File

@ -0,0 +1,27 @@
fn main() {}
#[cfg(test)]
mod tests {
use marine_rs_sdk_test::marine_test;
#[marine_test(
producer(
config_path = "../producer/Config.toml", // <- 1
modules_dir = "../producer/artifacts"
),
consumer(
config_path = "../consumer/Config.toml",
modules_dir = "../consumer/artifacts"
)
)]
fn test() {
let mut producer = marine_test_env::producer::ServiceInterface::new();
let mut consumer = marine_test_env::consumer::ServiceInterface::new();
let input = marine_test_env::producer::Input {
first_name: String::from("John"),
last_name: String::from("Doe"),
};
let data = producer.produce(input);
let result = consumer.consume(data);
assert_eq!(result, "John Doe")
}
}

View File

@ -10,5 +10,5 @@ name = "records_effector"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
test-record = { path = "../test-record" }

View File

@ -10,5 +10,5 @@ name = "records_pure"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
test-record = { path = "../test-record" }

View File

@ -10,4 +10,4 @@ name = "test_record"
path = "src/test_record.rs"
[dependencies]
marine-rs-sdk = "=0.6.11"
marine-rs-sdk = "0.6.14"

View File

@ -10,8 +10,8 @@ name = "sqlite_test"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
marine-sqlite-connector = "0.5.1"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"

View File

@ -10,5 +10,5 @@ path = "src/main.rs"
name = "curl_adapter"
[dependencies]
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
log = "0.4.8"

View File

@ -10,6 +10,6 @@ name = "facade"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
anyhow = "1.0.31"
log = "0.4.8"

View File

@ -10,6 +10,6 @@ name = "local_storage"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
marine-rs-sdk = { version = "0.6.14", features = ["logger"] }
wasm-tracing-allocator = "0.1.1"
log = "0.4.8"

View File

@ -11,7 +11,7 @@ name = "hello_world"
path = "src/main.rs"
[dependencies]
marine-rs-sdk = "0.6.11"
marine-rs-sdk = "0.6.14"
[dev-dependencies]
marine-rs-sdk-test = "0.2.0"
marine-rs-sdk-test = "0.3.0"