Skip to content

Commit

Permalink
add async test op to test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Jul 2, 2019
1 parent 6a3bdb7 commit 1961a12
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/034_plugin.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Hello from native bindings. data: "test" | zero_copy: {"some":"data"}
test
Hello from native bindings. data: "test" | zero_copy: {"some":"data"}
test
23 changes: 22 additions & 1 deletion tests/034_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const plugin = openPlugin(
env().DENO_BUILD_PATH + "/rust_crates/" + pluginFilename("test_plugin")
);
const testOp = plugin.loadOp("test_op");
const asyncTestOp = plugin.loadOp("async_test_op");

interface TestOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -43,4 +44,24 @@ const doTestOp = (args: TestOptions): any => {
}
};

console.log(doTestOp({ data: "test", zeroCopyData: { some: "data" } }));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function doAsyncTestOp(args: TestOptions): Promise<any> {
const response = asyncTestOp.dispatch(
encodeTestOp(args.data),
encodeTestOp(args.zeroCopyData)
);
if (response instanceof Promise) {
return decodeTestOp(await response);
} else {
throw new Error("Unexpected response type");
}
}

async function main(): Promise<void> {
console.log(doTestOp({ data: "test", zeroCopyData: { some: "data" } }));
console.log(
await doAsyncTestOp({ data: "test", zeroCopyData: { some: "data" } })
);
}

main();
2 changes: 2 additions & 0 deletions tests/plugin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ rust_cdylib("test_plugin") {
"obj/core/libdeno/",
]

extern_rlib = [ "futures" ]

extern = [
{
label = "//core:deno"
Expand Down
4 changes: 3 additions & 1 deletion tests/plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
deno = { path = "../../core" }
deno = { path = "../../core" }

futures = "0.1.27"
20 changes: 20 additions & 0 deletions tests/plugin/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use deno::CoreOp;
use deno::Op;
use deno::{Buf, PinnedBuf};
use futures::future::lazy;

#[macro_use]
extern crate deno;
Expand All @@ -21,3 +22,22 @@ pub fn op_test_op(data: &[u8], zero_copy: Option<PinnedBuf>) -> CoreOp {
}

declare_plugin_op!(test_op, op_test_op);

pub fn op_async_test_op(data: &[u8], zero_copy: Option<PinnedBuf>) -> CoreOp {
if let Some(buf) = zero_copy {
let data_str = std::str::from_utf8(&data[..]).unwrap();
let buf_str = std::str::from_utf8(&buf[..]).unwrap();
println!(
"Hello from native bindings. data: {} | zero_copy: {}",
data_str, buf_str
);
}
let op = Box::new(lazy(move || {
let result = b"test";
let result_box: Buf = Box::new(*result);
Ok(result_box)
}));
Op::Async(op)
}

declare_plugin_op!(async_test_op, op_async_test_op);

0 comments on commit 1961a12

Please sign in to comment.