2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-07-11 21:12:26 -04:00
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
use test_util::deno_cmd;
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
const BUILD_VARIANT: &str = "debug";
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
const BUILD_VARIANT: &str = "release";
|
|
|
|
|
2022-01-10 10:33:25 -05:00
|
|
|
fn build() {
|
2021-07-11 21:12:26 -04:00
|
|
|
let mut build_plugin_base = Command::new("cargo");
|
|
|
|
let mut build_plugin =
|
2021-08-06 17:28:10 -04:00
|
|
|
build_plugin_base.arg("build").arg("-p").arg("test_ffi");
|
2021-07-11 21:12:26 -04:00
|
|
|
if BUILD_VARIANT == "release" {
|
|
|
|
build_plugin = build_plugin.arg("--release");
|
|
|
|
}
|
|
|
|
let build_plugin_output = build_plugin.output().unwrap();
|
|
|
|
assert!(build_plugin_output.status.success());
|
2022-01-10 10:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
build();
|
|
|
|
|
2021-07-11 21:12:26 -04:00
|
|
|
let output = deno_cmd()
|
|
|
|
.arg("run")
|
2021-08-06 17:28:10 -04:00
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
2021-07-11 21:12:26 -04:00
|
|
|
.arg("--unstable")
|
2022-01-05 02:25:31 -05:00
|
|
|
.arg("--quiet")
|
2021-07-11 21:12:26 -04:00
|
|
|
.arg("tests/test.js")
|
2021-08-06 17:28:10 -04:00
|
|
|
.env("NO_COLOR", "1")
|
2021-07-11 21:12:26 -04:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
let expected = "\
|
2021-08-06 17:28:10 -04:00
|
|
|
something\n\
|
2021-10-05 18:27:05 -04:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8]\n\
|
2022-06-20 23:46:59 -04:00
|
|
|
[4, 5, 6]\n\
|
2021-10-10 09:18:02 -04:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8] [9, 10]\n\
|
2021-12-15 09:41:49 -05:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8]\n\
|
|
|
|
[ 1, 2, 3, 4, 5, 6 ]\n\
|
|
|
|
[ 4, 5, 6 ]\n\
|
|
|
|
[ 4, 5, 6 ]\n\
|
|
|
|
Hello from pointer!\n\
|
|
|
|
pointer!\n\
|
|
|
|
false\n\
|
|
|
|
true\n\
|
|
|
|
false\n\
|
2021-08-06 17:28:10 -04:00
|
|
|
579\n\
|
2022-01-12 06:38:26 -05:00
|
|
|
true\n\
|
|
|
|
579\n\
|
2021-09-20 14:38:28 -04:00
|
|
|
579\n\
|
2022-06-08 07:13:10 -04:00
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
2021-09-20 14:38:28 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
579\n\
|
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2022-01-11 01:21:16 -05:00
|
|
|
After sleep_blocking\n\
|
|
|
|
true\n\
|
2021-10-05 08:50:00 -04:00
|
|
|
Before\n\
|
|
|
|
true\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
After\n\
|
|
|
|
true\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
logCallback\n\
|
|
|
|
1 -1 2 -2 3 -3 4n -4n 0.5 -0.5 1 2 3 4 5 6 7 8\n\
|
|
|
|
u8: 8\n\
|
|
|
|
buf: [1, 2, 3, 4, 5, 6, 7, 8]\n\
|
|
|
|
logCallback\n\
|
|
|
|
30\n\
|
|
|
|
STORED_FUNCTION cleared\n\
|
|
|
|
STORED_FUNCTION_2 cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
Thread safe call counter: 0\n\
|
|
|
|
logCallback\n\
|
|
|
|
Thread safe call counter: 1\n\
|
|
|
|
u8: 8\n\
|
2022-02-18 07:21:19 -05:00
|
|
|
Static u32: 42\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
Static i64: -1242464576485n\n\
|
2022-02-18 07:21:19 -05:00
|
|
|
Static ptr: true\n\
|
|
|
|
Static ptr value: 42\n\
|
2021-08-06 17:28:10 -04:00
|
|
|
Correct number of resources\n";
|
2021-07-11 21:12:26 -04:00
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-01-10 10:33:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn symbol_types() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
|
|
|
.arg("cache")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/ffi_types.ts")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-06-28 05:23:36 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe_callback() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
|
|
|
.arg("run")
|
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/thread_safe_test.js")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
let expected = "\
|
|
|
|
Callback on main thread\n\
|
|
|
|
Callback on worker thread\n\
|
|
|
|
Calling callback, isolate should stay asleep until callback is called\n\
|
|
|
|
Callback being called\n\
|
|
|
|
Isolate should now exit\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|