2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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")
|
2022-07-11 21:03:05 -04:00
|
|
|
.arg(r#"--v8-flags=--allow-natives-syntax"#)
|
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() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2021-07-11 21:12:26 -04:00
|
|
|
}
|
|
|
|
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\
|
2022-10-20 00:07:37 -04:00
|
|
|
true\n\
|
2022-06-29 11:00:29 -04:00
|
|
|
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-07-11 22:50:20 -04:00
|
|
|
5\n\
|
|
|
|
5\n\
|
2022-07-11 21:03:05 -04:00
|
|
|
579\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
2021-09-20 14:38:28 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2022-09-04 23:26:52 -04:00
|
|
|
true\n\
|
|
|
|
false\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
|
|
|
579.912\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
579\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2021-10-05 08:50:00 -04:00
|
|
|
Before\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
After\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
logCallback\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
1 -1 2 -2 3 -3 4 -4 0.5 -0.5 1 2 3 4 5 6 7 8\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
u8: 8\n\
|
|
|
|
buf: [1, 2, 3, 4, 5, 6, 7, 8]\n\
|
|
|
|
logCallback\n\
|
|
|
|
30\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
255 65535 4294967295 4294967296 123.456 789.876 -1 -2 -3 -4 -1000 1000 12345.67891 12345.679 12345.67891 12345.679 12345.67891 12345.679 12345.67891\n\
|
|
|
|
255 65535 4294967295 4294967296 123.456 789.876 -1 -2 -3 -4 -1000 1000 12345.67891 12345.679 12345.67891 12345.679 12345.67891 12345.679 12345.67891\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
78\n\
|
|
|
|
78\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
|
|
|
STORED_FUNCTION_2 cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
logCallback\n\
|
|
|
|
u8: 8\n\
|
2023-01-07 22:58:10 -05:00
|
|
|
Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Rect { x: 20.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Mixed { u8: 3, f32: 12.515, rect: Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }, usize: 12456789, array: [8, 32] }\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
2264956937\n\
|
|
|
|
2264956937\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()
|
2022-07-24 06:41:11 -04:00
|
|
|
.arg("check")
|
2022-01-10 10:33:25 -05:00
|
|
|
.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() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-01-10 10:33:25 -05:00
|
|
|
}
|
|
|
|
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() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
let expected = "\
|
|
|
|
Callback on main thread\n\
|
|
|
|
Callback on worker thread\n\
|
2023-06-28 16:41:59 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
Calling callback, isolate should stay asleep until callback is called\n\
|
|
|
|
Callback being called\n\
|
2023-06-28 16:41:59 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
Isolate should now exit\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-07-09 05:49:20 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn event_loop_integration() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
|
|
|
.arg("run")
|
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
|
|
|
.arg("--unstable")
|
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/event_loop_integration.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() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-07-09 05:49:20 -04:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
// TODO(aapoalas): The order of logging in thread safe callbacks is
|
|
|
|
// unexpected: The callback logs synchronously and creates an asynchronous
|
|
|
|
// logging task, which then gets called synchronously before the callback
|
|
|
|
// actually yields to the calling thread. This is in contrast to what the
|
|
|
|
// logging would look like if the call was coming from within Deno itself,
|
|
|
|
// and may lead users to unknowingly run heavy asynchronous tasks from thread
|
|
|
|
// safe callbacks synchronously.
|
|
|
|
// The fix would be to make sure microtasks are only run after the event loop
|
|
|
|
// middleware that polls them has completed its work. This just does not seem
|
|
|
|
// to work properly with Linux release builds.
|
|
|
|
let expected = "\
|
|
|
|
SYNCHRONOUS\n\
|
|
|
|
Sync\n\
|
|
|
|
STORED_FUNCTION called\n\
|
|
|
|
Async\n\
|
|
|
|
Timeout\n\
|
|
|
|
THREAD SAFE\n\
|
|
|
|
Sync\n\
|
|
|
|
Async\n\
|
|
|
|
STORED_FUNCTION called\n\
|
2023-02-22 14:09:59 -05:00
|
|
|
Timeout\n\
|
|
|
|
RETRY THREAD SAFE\n\
|
|
|
|
Sync\n\
|
|
|
|
Async\n\
|
|
|
|
STORED_FUNCTION called\n\
|
2022-07-09 05:49:20 -04:00
|
|
|
Timeout\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|