summaryrefslogtreecommitdiff
path: root/test_plugin
diff options
context:
space:
mode:
authorAndy Finch <andyfinch7@gmail.com>2019-12-05 15:30:20 -0500
committerRy Dahl <ry@tinyclouds.org>2019-12-05 15:30:20 -0500
commit7c3b9b4f4f2f4ec8fdeb0e77bb853fd22ffaa476 (patch)
treeaeafe5cc2560c5366704d7a580a5b0e0dced504d /test_plugin
parent214b3eb29aa9cce8a55a247b4bd816cbd19bfe6b (diff)
feat: first pass at native plugins (#3372)
Diffstat (limited to 'test_plugin')
-rw-r--r--test_plugin/Cargo.toml14
-rw-r--r--test_plugin/src/lib.rs53
-rw-r--r--test_plugin/tests/integration_tests.rs44
-rw-r--r--test_plugin/tests/test.js47
4 files changed, 158 insertions, 0 deletions
diff --git a/test_plugin/Cargo.toml b/test_plugin/Cargo.toml
new file mode 100644
index 000000000..d6c4eb1de
--- /dev/null
+++ b/test_plugin/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "test_plugin"
+version = "0.0.1"
+authors = ["the deno authors"]
+edition = "2018"
+publish = false
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+futures = "0.3"
+deno = { path = "../core" }
+deno_cli = { path = "../cli" } \ No newline at end of file
diff --git a/test_plugin/src/lib.rs b/test_plugin/src/lib.rs
new file mode 100644
index 000000000..30df114b9
--- /dev/null
+++ b/test_plugin/src/lib.rs
@@ -0,0 +1,53 @@
+#[macro_use]
+extern crate deno;
+extern crate futures;
+
+use deno::CoreOp;
+use deno::Op;
+use deno::PluginInitContext;
+use deno::{Buf, PinnedBuf};
+use futures::future::FutureExt;
+
+fn init(context: &mut dyn PluginInitContext) {
+ context.register_op("testSync", Box::new(op_test_sync));
+ context.register_op("testAsync", Box::new(op_test_async));
+}
+init_fn!(init);
+
+pub fn op_test_sync(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 plugin. data: {} | zero_copy: {}",
+ data_str, buf_str
+ );
+ }
+ let result = b"test";
+ let result_box: Buf = Box::new(*result);
+ Op::Sync(result_box)
+}
+
+pub fn op_test_async(data: &[u8], zero_copy: Option<PinnedBuf>) -> CoreOp {
+ let data_str = std::str::from_utf8(&data[..]).unwrap().to_string();
+ let fut = async move {
+ if let Some(buf) = zero_copy {
+ let buf_str = std::str::from_utf8(&buf[..]).unwrap();
+ println!(
+ "Hello from plugin. data: {} | zero_copy: {}",
+ data_str, buf_str
+ );
+ }
+ let (tx, rx) = futures::channel::oneshot::channel::<Result<(), ()>>();
+ std::thread::spawn(move || {
+ std::thread::sleep(std::time::Duration::from_secs(1));
+ tx.send(Ok(())).unwrap();
+ });
+ assert!(rx.await.is_ok());
+ let result = b"test";
+ let result_box: Buf = Box::new(*result);
+ Ok(result_box)
+ };
+
+ Op::Async(fut.boxed())
+}
diff --git a/test_plugin/tests/integration_tests.rs b/test_plugin/tests/integration_tests.rs
new file mode 100644
index 000000000..a6790d013
--- /dev/null
+++ b/test_plugin/tests/integration_tests.rs
@@ -0,0 +1,44 @@
+use deno_cli::test_util::*;
+use std::process::Command;
+
+fn deno_cmd() -> Command {
+ assert!(deno_exe_path().exists());
+ Command::new(deno_exe_path())
+}
+
+#[cfg(debug_assertions)]
+const BUILD_VARIANT: &str = "debug";
+
+#[cfg(not(debug_assertions))]
+const BUILD_VARIANT: &str = "release";
+
+#[test]
+fn basic() {
+ let mut build_plugin_base = Command::new("cargo");
+ let mut build_plugin =
+ build_plugin_base.arg("build").arg("-p").arg("test_plugin");
+ if BUILD_VARIANT == "release" {
+ build_plugin = build_plugin.arg("--release");
+ }
+ let _build_plugin_output = build_plugin.output().unwrap();
+ let output = deno_cmd()
+ .arg("--allow-plugin")
+ .arg("tests/test.js")
+ .arg(BUILD_VARIANT)
+ .output()
+ .unwrap();
+ let stdout = std::str::from_utf8(&output.stdout).unwrap();
+ let stderr = std::str::from_utf8(&output.stderr).unwrap();
+ if !output.status.success() {
+ println!("stdout {}", stdout);
+ println!("stderr {}", stderr);
+ }
+ assert!(output.status.success());
+ let expected = if cfg!(target_os = "windows") {
+ "Hello from plugin. data: test | zero_copy: test\nPlugin Sync Response: test\r\nHello from plugin. data: test | zero_copy: test\nPlugin Async Response: test\r\n"
+ } else {
+ "Hello from plugin. data: test | zero_copy: test\nPlugin Sync Response: test\nHello from plugin. data: test | zero_copy: test\nPlugin Async Response: test\n"
+ };
+ assert_eq!(stdout, expected);
+ assert_eq!(stderr, "");
+}
diff --git a/test_plugin/tests/test.js b/test_plugin/tests/test.js
new file mode 100644
index 000000000..5a127d328
--- /dev/null
+++ b/test_plugin/tests/test.js
@@ -0,0 +1,47 @@
+const filenameBase = "test_plugin";
+
+let filenameSuffix = ".so";
+let filenamePrefix = "lib";
+
+if (Deno.build.os === "win") {
+ filenameSuffix = ".dll";
+ filenamePrefix = "";
+}
+if (Deno.build.os === "mac") {
+ filenameSuffix = ".dylib";
+}
+
+const filename = `../target/${Deno.args[1]}/${filenamePrefix}${filenameBase}${filenameSuffix}`;
+
+const plugin = Deno.openPlugin(filename);
+
+const { testSync, testAsync } = plugin.ops;
+
+const textDecoder = new TextDecoder();
+
+function runTestSync() {
+ const response = testSync.dispatch(
+ new Uint8Array([116, 101, 115, 116]),
+ new Uint8Array([116, 101, 115, 116])
+ );
+
+ console.log(`Plugin Sync Response: ${textDecoder.decode(response)}`);
+}
+
+testAsync.setAsyncHandler(response => {
+ console.log(`Plugin Async Response: ${textDecoder.decode(response)}`);
+});
+
+function runTestAsync() {
+ const response = testAsync.dispatch(
+ new Uint8Array([116, 101, 115, 116]),
+ new Uint8Array([116, 101, 115, 116])
+ );
+
+ if (response != null || response != undefined) {
+ throw new Error("Expected null response!");
+ }
+}
+
+runTestSync();
+runTestAsync();