summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-28 15:12:21 -0700
committerGitHub <noreply@github.com>2024-02-28 22:12:21 +0000
commitb6e44f91ad55f9737d65a4832d10cfa608f27c41 (patch)
treee184d9c7b0a4ebbb7cbb20ab72cc99c07eb46da0 /tests
parentc9b2139b1e7e1240db792173118b7d54d16c5f73 (diff)
fix(cli): ensure that pre- and post-test output is flushed at the appropriate times (#22611)
Some `deno_std` tests were failing to print output that was resolved after the last test finished. In addition, output printed before tests began would sometimes appear above the "running X tests ..." line, and sometimes below it depending on timing. We now guarantee that all output is flushed before and after tests run, making the output consistent. Pre-test and post-test output are captured in `------ pre-test output ------` and `------ post-test output ------` blocks to differentiate them from the regular output blocks. Here's an example of a test (that is much noisier than normal, but an example of what the output will look like): ``` Check ./load_unload.ts ------- pre-test output ------- load ----- output end ----- running 1 test from ./load_unload.ts test ... ------- output ------- test ----- output end ----- test ... ok ([WILDCARD]) ------- post-test output ------- unload ----- output end ----- ```
Diffstat (limited to 'tests')
-rw-r--r--tests/integration/run_tests.rs20
-rw-r--r--tests/testdata/run/websocket_server_idletimeout.ts6
-rw-r--r--tests/testdata/test/load_unload.out14
-rw-r--r--tests/testdata/test/load_unload.ts3
-rw-r--r--tests/util/server/src/macros.rs2
5 files changed, 32 insertions, 13 deletions
diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs
index ff983ed4c..77e1db2ab 100644
--- a/tests/integration/run_tests.rs
+++ b/tests/integration/run_tests.rs
@@ -4560,15 +4560,14 @@ async fn websocket_server_multi_field_connection_header() {
assert!(child.wait().unwrap().success());
}
-// TODO(bartlomieju): this should use `deno run`, not `deno test`; but the
-// test hangs then. https://github.com/denoland/deno/issues/14283
#[tokio::test]
async fn websocket_server_idletimeout() {
+ test_util::timeout!(60);
let script =
util::testdata_path().join("run/websocket_server_idletimeout.ts");
let root_ca = util::testdata_path().join("tls/RootCA.pem");
let mut child = util::deno_cmd()
- .arg("test")
+ .arg("run")
.arg("--unstable")
.arg("--allow-net")
.arg("--cert")
@@ -4579,11 +4578,13 @@ async fn websocket_server_idletimeout() {
.unwrap();
let stdout = child.stdout.as_mut().unwrap();
- let mut buffer = [0; 5];
- let read = stdout.read(&mut buffer).unwrap();
- assert_eq!(read, 5);
- let msg = std::str::from_utf8(&buffer).unwrap();
- assert_eq!(msg, "READY");
+ let mut buf: Vec<u8> = vec![];
+ while !String::from_utf8(buf.clone()).unwrap().contains("READY") {
+ let mut buffer = [0; 64];
+ let read = stdout.read(&mut buffer).unwrap();
+ buf.extend_from_slice(&buffer[0..read]);
+ eprintln!("buf = {buf:?}");
+ }
let stream = tokio::net::TcpStream::connect("localhost:4509")
.await
@@ -4604,8 +4605,7 @@ async fn websocket_server_idletimeout() {
fastwebsockets::handshake::client(&SpawnExecutor, req, stream)
.await
.unwrap();
-
- assert!(child.wait().unwrap().success());
+ assert_eq!(child.wait().unwrap().code(), Some(123));
}
itest!(auto_discover_lockfile {
diff --git a/tests/testdata/run/websocket_server_idletimeout.ts b/tests/testdata/run/websocket_server_idletimeout.ts
index 85b031b8e..82a35605a 100644
--- a/tests/testdata/run/websocket_server_idletimeout.ts
+++ b/tests/testdata/run/websocket_server_idletimeout.ts
@@ -11,10 +11,12 @@ const { response, socket } = Deno.upgradeWebSocket(request, {
idleTimeout: 1,
});
socket.onerror = (e) => {
+ console.log(e);
assertEquals((e as ErrorEvent).message, "No response from ping frame.");
errorDeferred.resolve();
};
socket.onclose = (e) => {
+ console.log(e);
assertEquals(e.reason, "No response from ping frame.");
closeDeferred.resolve();
};
@@ -22,4 +24,6 @@ await respondWith(response);
await errorDeferred.promise;
await closeDeferred.promise;
-listener.close();
+
+// TODO(mmastrac): this doesn't exit on its own. Why?
+Deno.exit(123);
diff --git a/tests/testdata/test/load_unload.out b/tests/testdata/test/load_unload.out
index aef7531af..75187fa7b 100644
--- a/tests/testdata/test/load_unload.out
+++ b/tests/testdata/test/load_unload.out
@@ -1,6 +1,16 @@
-Check [WILDCARD]/test/load_unload.ts
-running 1 test from ./test/load_unload.ts
+Check [WILDCARD]/load_unload.ts
+------- pre-test output -------
+load
+----- pre-test output end -----
+running 1 test from [WILDCARD]/load_unload.ts
+test ...
+------- output -------
+test
+----- output end -----
test ... ok ([WILDCARD])
+------- post-test output -------
+unload
+----- post-test output end -----
ok | 1 passed | 0 failed ([WILDCARD])
diff --git a/tests/testdata/test/load_unload.ts b/tests/testdata/test/load_unload.ts
index 2bd04a676..5027b949a 100644
--- a/tests/testdata/test/load_unload.ts
+++ b/tests/testdata/test/load_unload.ts
@@ -4,6 +4,7 @@ addEventListener("load", () => {
throw new Error("Interval is already set");
}
+ console.log("load");
interval = setInterval(() => {}, 0);
});
@@ -12,10 +13,12 @@ addEventListener("unload", () => {
throw new Error("Interval was not set");
}
+ console.log("unload");
clearInterval(interval);
});
Deno.test("test", () => {
+ console.log("test");
if (!interval) {
throw new Error("Interval was not set");
}
diff --git a/tests/util/server/src/macros.rs b/tests/util/server/src/macros.rs
index 530d8cea0..542214d20 100644
--- a/tests/util/server/src/macros.rs
+++ b/tests/util/server/src/macros.rs
@@ -30,7 +30,9 @@ macro_rules! timeout {
let timeout = *timeout.get(0).unwrap_or(&300);
::std::thread::spawn(move || {
if rx.recv_timeout(::std::time::Duration::from_secs(timeout)) == Err(::std::sync::mpsc::RecvTimeoutError::Timeout) {
+ use std::io::Write;
eprintln!("Test {function} timed out after {timeout} seconds, aborting");
+ _ = std::io::stderr().flush();
::std::process::exit(1);
}
});