From 4d3862f2f21011ed5dab0dde2b5d3839959a79bc Mon Sep 17 00:00:00 2001 From: afinch7 Date: Tue, 2 Jul 2019 14:51:13 -0400 Subject: [PATCH] add async test op to test plugin --- Cargo.lock | 1 + tests/034_plugin.out | 2 ++ tests/034_plugin.ts | 23 ++++++++++++++++++++++- tests/plugin/BUILD.gn | 2 ++ tests/plugin/Cargo.toml | 4 +++- tests/plugin/lib.rs | 20 ++++++++++++++++++++ 6 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b350c15d09d8b..ebe868fbd07305 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1121,6 +1121,7 @@ name = "test_plugin" version = "0.1.0" dependencies = [ "deno 0.10.0", + "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/tests/034_plugin.out b/tests/034_plugin.out index d1e82e375c71e9..dfc98d51a214f7 100644 --- a/tests/034_plugin.out +++ b/tests/034_plugin.out @@ -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 diff --git a/tests/034_plugin.ts b/tests/034_plugin.ts index 021c27a729b08b..3e16bf12a091ce 100644 --- a/tests/034_plugin.ts +++ b/tests/034_plugin.ts @@ -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 @@ -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 { + 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 { + console.log(doTestOp({ data: "test", zeroCopyData: { some: "data" } })); + console.log( + await doAsyncTestOp({ data: "test", zeroCopyData: { some: "data" } }) + ); +} + +main(); diff --git a/tests/plugin/BUILD.gn b/tests/plugin/BUILD.gn index 04b0aaf250bc3a..66d6b57cea5002 100644 --- a/tests/plugin/BUILD.gn +++ b/tests/plugin/BUILD.gn @@ -16,6 +16,8 @@ rust_cdylib("test_plugin") { "obj/core/libdeno/", ] + extern_rlib = [ "futures" ] + extern = [ { label = "//core:deno" diff --git a/tests/plugin/Cargo.toml b/tests/plugin/Cargo.toml index af32befc79a240..af117d4bce1767 100644 --- a/tests/plugin/Cargo.toml +++ b/tests/plugin/Cargo.toml @@ -10,4 +10,6 @@ path = "lib.rs" crate-type = ["cdylib"] [dependencies] -deno = { path = "../../core" } \ No newline at end of file +deno = { path = "../../core" } + +futures = "0.1.27" \ No newline at end of file diff --git a/tests/plugin/lib.rs b/tests/plugin/lib.rs index b403287516910c..8855b2d83e8628 100644 --- a/tests/plugin/lib.rs +++ b/tests/plugin/lib.rs @@ -2,6 +2,7 @@ use deno::CoreOp; use deno::Op; use deno::{Buf, PinnedBuf}; +use futures::future::lazy; #[macro_use] extern crate deno; @@ -21,3 +22,22 @@ pub fn op_test_op(data: &[u8], zero_copy: Option) -> CoreOp { } declare_plugin_op!(test_op, op_test_op); + +pub fn op_async_test_op(data: &[u8], zero_copy: Option) -> 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);