Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for parallel requests #195

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions examples/example-deno-runtime/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const imports: Imports = {
},

importGenerics: (
arg: StructWithGenerics<number>
arg: StructWithGenerics<number>,
): StructWithGenerics<number> => {
assertEquals(arg, {
list: [0, 64],
Expand Down Expand Up @@ -204,7 +204,7 @@ const imports: Imports = {
},

importSerdeAdjacentlyTagged: (
arg: SerdeAdjacentlyTagged
arg: SerdeAdjacentlyTagged,
): SerdeAdjacentlyTagged => {
assertEquals(arg, { type: "Bar", payload: "Hello, plugin!" });
return { type: "Baz", payload: { a: -8, b: 64 } };
Expand All @@ -226,7 +226,7 @@ const imports: Imports = {
},

importSerdeInternallyTagged: (
arg: SerdeInternallyTagged
arg: SerdeInternallyTagged,
): SerdeInternallyTagged => {
assertEquals(arg, { type: "Foo" });
return { type: "Baz", a: -8, b: 64 };
Expand Down Expand Up @@ -363,7 +363,7 @@ async function loadExamplePlugin() {
if (!examplePlugin) {
examplePlugin = await loadPlugin(
"../example-plugin/target/wasm32-unknown-unknown/debug/example_plugin.wasm",
imports
imports,
);

const { init } = examplePlugin;
Expand Down Expand Up @@ -424,7 +424,7 @@ Deno.test("timestamp", async () => {

assertEquals(
plugin.exportTimestamp?.("2022-04-12T19:10:00Z"),
"2022-04-13T12:37:00Z"
"2022-04-13T12:37:00Z",
);
});

Expand All @@ -441,7 +441,7 @@ Deno.test("flattened structs", async () => {
fooBar: "fooBar",
QUX_BAZ: -64.0,
rawStruct: 32,
}
},
);

assertEquals(plugin.exportFpEnum?.("foo_bar"), {
Expand All @@ -461,7 +461,7 @@ Deno.test("flattened structs", async () => {
fooBar: "fooBar",
QUX_BAZ: -64.0,
rawStruct: 32,
}
},
);

assertEquals(plugin.exportSerdeEnum?.("foo_bar"), {
Expand Down Expand Up @@ -495,7 +495,7 @@ Deno.test("generics", async () => {
twee: [{ value: 2.0 }],
},
optional_timestamp: "1970-01-01T00:00:00Z",
}
},
);
});

Expand Down Expand Up @@ -524,7 +524,7 @@ Deno.test("tagged enums", async () => {
{
type: "Baz",
payload: { a: -8, b: 64 },
}
},
);

assertEquals(plugin.exportFpInternallyTagged?.({ type: "Foo" }), {
Expand All @@ -543,7 +543,7 @@ Deno.test("tagged enums", async () => {
{
type: "Baz",
payload: { a: -8, b: 64 },
}
},
);

assertEquals(plugin.exportSerdeInternallyTagged?.({ type: "Foo" }), {
Expand Down Expand Up @@ -598,13 +598,13 @@ Deno.test("async struct", async () => {
QUX_BAZ: 64.0,
rawStruct: -32,
},
64n
64n,
),
{
fooBar: "fooBar",
QUX_BAZ: -64.0,
rawStruct: 32,
}
},
);
});

Expand All @@ -618,6 +618,14 @@ Deno.test("fetch async data", async () => {
});
});

Deno.test("invoke parallel requests", async () => {
const { makeParallelRequests } = await loadExamplePlugin();
assert(makeParallelRequests);

const data = await makeParallelRequests();
assertEquals(data, JSON.stringify({ status: "confirmed" }));
});

Deno.test("bytes", async () => {
const { exportGetBytes, exportGetSerdeBytes } = await loadExamplePlugin();
assert(exportGetBytes);
Expand Down
13 changes: 7 additions & 6 deletions examples/example-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ crate-type = ["cdylib"]

[dependencies]
bytes = "1"
example-bindings = {path = "../example-protocol/bindings/rust-plugin"}
http = {version = "0.2"}
once_cell = {version = "1"}
redux-example = {path = "../redux-example"}
serde_bytes = {version = "0.11"}
time = {version = "0.3", features = ["serde-human-readable"]}
example-bindings = { path = "../example-protocol/bindings/rust-plugin" }
futures = "0.3"
http = { version = "0.2" }
once_cell = { version = "1" }
redux-example = { path = "../redux-example" }
serde_bytes = { version = "0.11" }
time = { version = "0.3", features = ["serde-human-readable"] }
tracing = "0.1.37"
21 changes: 17 additions & 4 deletions examples/example-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,7 @@ async fn fetch_data(r#type: String) -> Result<String, String> {
url: Uri::from_static("https://fiberplane.dev"),
method: Method::POST,
headers,
body: Some(ByteBuf::from(format!(
r#"{{"country":"🇳🇱","type":"{}"}}"#,
r#type
))),
body: Some(ByteBuf::from(format!(r#"{{"country":"🇳🇱","type":"{}"}}"#, r#type))),
})
.await;

Expand All @@ -428,6 +425,22 @@ async fn fetch_data(r#type: String) -> Result<String, String> {
}
}

#[fp_export_impl(example_bindings)]
async fn make_parallel_requests() -> String {
let request1 = fetch_data("sign-up".to_owned());
let request2 = fetch_data("sign-up".to_owned());

match futures::future::join(request1, request2).await {
(Ok(response1), Ok(response2)) => {
assert_eq!(response1, response2);
response1
}
(maybe_error1, maybe_error2) => {
format!("Error of these is an error: {maybe_error1:?}, {maybe_error2:?}")
}
}
}

#[fp_export_impl(example_bindings)]
fn export_get_bytes() -> Result<Bytes, String> {
import_get_bytes().map(|bytes| {
Expand Down
3 changes: 3 additions & 0 deletions examples/example-protocol/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ fp_export! {
/// Example how plugin could expose async data-fetching capabilities.
async fn fetch_data(r#type: String) -> Result<String, String>;

/// Example that shows how to make parallel requests from the guest module.
async fn make_parallel_requests() -> String;

/// Called on the plugin to give it a chance to initialize.
fn init();

Expand Down
3 changes: 2 additions & 1 deletion examples/example-rust-wasmer2-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fp-bindgen-support = { path = "../../fp-bindgen-support", features = [
] }
http = "0.2"
once_cell = "1"
rand = "0.8"
rmp-serde = "1.0.0"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
Expand All @@ -24,7 +25,7 @@ time = { version = "0.3", features = [
"serde-well-known",
"macros",
] }
tokio = { version = "1.9.0", features = ["rt", "macros"] }
tokio = { version = "1.9.0", features = ["rt-multi-thread", "macros", "time"] }
tracing = "0.1.37"
wasmer = { version = "2.3", features = ["compiler", "cranelift", "singlepass"] }
wasmer-wasi = "2.3"
Expand Down
4 changes: 4 additions & 0 deletions examples/example-rust-wasmer2-runtime/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod types;

use bytes::Bytes;
use serde_bytes::ByteBuf;
use tokio::time::{sleep, Duration};
use types::*;
use super::GLOBAL_STATE;

Expand Down Expand Up @@ -198,6 +199,9 @@ fn log(msg: String) {
}

async fn make_http_request(opts: Request) -> Result<Response, RequestError> {
// Add a little randomized sleeping to see if we trigger potential race issues...
//sleep(Duration::from_millis(rand::random::<u8>().into())).await;

Ok(Response {
body: ByteBuf::from(r#"{"status":"confirmed"}"#.to_string()),
headers: opts.headers,
Expand Down
10 changes: 10 additions & 0 deletions examples/example-rust-wasmer2-runtime/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ async fn fetch_async_data() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn make_parallel_requests() -> Result<()> {
let rt = new_runtime()?;

let response = rt.make_parallel_requests().await?;

assert_eq!(response, r#"{"status":"confirmed"}"#.to_string());
Ok(())
}

#[test]
fn bytes() -> Result<()> {
let rt = new_runtime()?;
Expand Down
Loading