2024-01-01 14:58:21 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2023-01-12 20:59:13 -05:00
2023-12-26 08:32:21 -05:00
use bytes ::Bytes ;
2023-04-22 05:17:31 -04:00
use deno_core ::anyhow ::anyhow ;
2023-01-12 20:59:13 -05:00
use deno_core ::error ::AnyError ;
use deno_core ::serde_json ;
use deno_core ::serde_json ::json ;
use deno_core ::url ;
2024-07-13 17:08:23 -04:00
use deno_fetch ::reqwest ;
2023-12-27 11:59:57 -05:00
use fastwebsockets ::FragmentCollector ;
use fastwebsockets ::Frame ;
use fastwebsockets ::WebSocket ;
use hyper ::body ::Incoming ;
use hyper ::upgrade ::Upgraded ;
use hyper ::Request ;
use hyper ::Response ;
2023-12-26 08:32:21 -05:00
use hyper_util ::rt ::TokioIo ;
2023-01-12 20:59:13 -05:00
use std ::io ::BufRead ;
2024-05-28 12:02:01 -04:00
use std ::process ::ChildStderr ;
2023-08-22 09:25:38 -04:00
use std ::time ::Duration ;
2023-01-12 20:59:13 -05:00
use test_util as util ;
use tokio ::net ::TcpStream ;
2023-08-22 09:25:38 -04:00
use tokio ::time ::timeout ;
2023-04-23 16:55:45 -04:00
use url ::Url ;
2024-05-08 22:45:06 -04:00
use util ::assert_contains ;
2023-05-22 21:28:36 -04:00
use util ::assert_starts_with ;
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
use util ::DenoChild ;
2023-11-17 10:05:42 -05:00
use util ::TestContextBuilder ;
2023-01-12 20:59:13 -05:00
2023-04-22 05:17:31 -04:00
struct SpawnExecutor ;
2023-12-27 11:59:57 -05:00
impl < Fut > hyper ::rt ::Executor < Fut > for SpawnExecutor
2023-04-22 05:17:31 -04:00
where
Fut : std ::future ::Future + Send + 'static ,
Fut ::Output : Send + 'static ,
{
fn execute ( & self , fut : Fut ) {
2023-08-23 19:03:05 -04:00
deno_core ::unsync ::spawn ( fut ) ;
2023-04-22 05:17:31 -04:00
}
}
2023-12-26 08:32:21 -05:00
async fn connect_to_ws (
uri : Url ,
) -> ( WebSocket < TokioIo < Upgraded > > , Response < Incoming > ) {
2023-04-23 16:55:45 -04:00
let domain = & uri . host ( ) . unwrap ( ) . to_string ( ) ;
let port = & uri . port ( ) . unwrap_or ( match uri . scheme ( ) {
" wss " | " https " = > 443 ,
_ = > 80 ,
} ) ;
let addr = format! ( " {domain} : {port} " ) ;
let stream = TcpStream ::connect ( addr ) . await . unwrap ( ) ;
let host = uri . host_str ( ) . unwrap ( ) ;
let req = Request ::builder ( )
. method ( " GET " )
. uri ( uri . path ( ) )
. header ( " Host " , host )
2023-12-27 11:59:57 -05:00
. header ( hyper ::header ::UPGRADE , " websocket " )
. header ( hyper ::header ::CONNECTION , " Upgrade " )
2023-04-23 16:55:45 -04:00
. header (
" Sec-WebSocket-Key " ,
2023-12-27 11:59:57 -05:00
fastwebsockets ::handshake ::generate_key ( ) ,
2023-04-23 16:55:45 -04:00
)
. header ( " Sec-WebSocket-Version " , " 13 " )
2023-12-26 08:32:21 -05:00
. body ( http_body_util ::Empty ::< Bytes > ::new ( ) )
2023-04-23 16:55:45 -04:00
. unwrap ( ) ;
2023-12-27 11:59:57 -05:00
fastwebsockets ::handshake ::client ( & SpawnExecutor , req , stream )
2023-04-23 16:55:45 -04:00
. await
. unwrap ( )
}
2024-05-28 12:02:01 -04:00
fn ignore_script_parsed ( msg : & str ) -> bool {
! msg . starts_with ( r # "{"method":"Debugger.scriptParsed","# )
}
struct StdErrLines {
reader : Box < dyn Iterator < Item = String > > ,
check_lines : Vec < String > ,
}
impl StdErrLines {
pub fn new ( stderr : ChildStderr ) -> Self {
Self {
reader : Box ::new ( std ::io ::BufReader ::new ( stderr ) . lines ( ) . map ( | r | {
let line = r . unwrap ( ) ;
eprintln! ( " STDERR: {} " , line ) ;
line
} ) ) ,
check_lines : Default ::default ( ) ,
}
}
pub fn next ( & mut self ) -> Option < String > {
loop {
let line = util ::strip_ansi_codes ( & self . reader . next ( ) ? ) . to_string ( ) ;
if line . starts_with ( " Check " ) | | line . starts_with ( " Download " ) {
self . check_lines . push ( line ) ;
} else {
return Some ( line ) ;
}
}
}
pub fn assert_lines ( & mut self , expected_lines : & [ & str ] ) {
let mut expected_index = 0 ;
loop {
let line = self . next ( ) . unwrap ( ) ;
assert_eq! ( line , expected_lines [ expected_index ] ) ;
expected_index + = 1 ;
if expected_index > = expected_lines . len ( ) {
break ;
}
}
}
pub fn extract_ws_url ( & mut self ) -> url ::Url {
let stderr_first_line = self . next ( ) . unwrap ( ) ;
assert_starts_with! ( & stderr_first_line , " Debugger listening on " ) ;
let v : Vec < _ > = stderr_first_line . match_indices ( " ws: " ) . collect ( ) ;
assert_eq! ( v . len ( ) , 1 ) ;
let ws_url_index = v [ 0 ] . 0 ;
let ws_url = & stderr_first_line [ ws_url_index .. ] ;
url ::Url ::parse ( ws_url ) . unwrap ( )
}
}
2023-01-12 20:59:13 -05:00
struct InspectorTester {
2023-12-26 08:32:21 -05:00
socket : FragmentCollector < TokioIo < Upgraded > > ,
2023-01-12 20:59:13 -05:00
notification_filter : Box < dyn FnMut ( & str ) -> bool + 'static > ,
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
child : DenoChild ,
2024-05-28 12:02:01 -04:00
stderr_lines : StdErrLines ,
2023-01-12 20:59:13 -05:00
stdout_lines : Box < dyn Iterator < Item = String > > ,
}
2023-08-21 05:53:52 -04:00
impl Drop for InspectorTester {
fn drop ( & mut self ) {
_ = self . child . kill ( ) ;
}
}
2023-01-12 20:59:13 -05:00
impl InspectorTester {
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
2023-03-27 18:01:52 -04:00
async fn create < F > ( mut child : DenoChild , notification_filter : F ) -> Self
2023-01-12 20:59:13 -05:00
where
F : FnMut ( & str ) -> bool + 'static ,
{
let stdout = child . stdout . take ( ) . unwrap ( ) ;
2024-05-08 22:45:06 -04:00
let stdout_lines = std ::io ::BufReader ::new ( stdout ) . lines ( ) . map ( | r | {
let line = r . unwrap ( ) ;
eprintln! ( " STDOUT: {} " , line ) ;
line
} ) ;
2023-01-12 20:59:13 -05:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
2023-01-12 20:59:13 -05:00
2024-05-28 12:02:01 -04:00
let uri = stderr_lines . extract_ws_url ( ) ;
2023-04-22 05:17:31 -04:00
2023-04-23 16:55:45 -04:00
let ( socket , response ) = connect_to_ws ( uri ) . await ;
2023-01-12 20:59:13 -05:00
2023-04-22 05:17:31 -04:00
assert_eq! ( response . status ( ) , 101 ) ; // Switching protocols.
2023-01-12 20:59:13 -05:00
Self {
2023-04-22 05:17:31 -04:00
socket : FragmentCollector ::new ( socket ) ,
2023-01-12 20:59:13 -05:00
notification_filter : Box ::new ( notification_filter ) ,
child ,
2024-05-28 12:02:01 -04:00
stderr_lines ,
2023-01-12 20:59:13 -05:00
stdout_lines : Box ::new ( stdout_lines ) ,
}
}
async fn send_many ( & mut self , messages : & [ serde_json ::Value ] ) {
// TODO(bartlomieju): add graceful error handling
for msg in messages {
let result = self
2023-04-22 05:17:31 -04:00
. socket
2023-08-10 00:29:06 -04:00
. write_frame ( Frame ::text ( msg . to_string ( ) . into_bytes ( ) . into ( ) ) )
2023-01-12 20:59:13 -05:00
. await
2023-04-22 05:17:31 -04:00
. map_err ( | e | anyhow! ( e ) ) ;
2023-01-12 20:59:13 -05:00
self . handle_error ( result ) ;
}
}
async fn send ( & mut self , message : serde_json ::Value ) {
self . send_many ( & [ message ] ) . await ;
}
fn handle_error < T > ( & mut self , result : Result < T , AnyError > ) -> T {
match result {
Ok ( result ) = > result ,
Err ( err ) = > {
let mut stdout = vec! [ ] ;
for line in self . stdout_lines . by_ref ( ) {
stdout . push ( line ) ;
}
let mut stderr = vec! [ ] ;
2024-05-28 12:02:01 -04:00
while let Some ( line ) = self . stderr_lines . next ( ) {
2023-01-12 20:59:13 -05:00
stderr . push ( line ) ;
}
let stdout = stdout . join ( " \n " ) ;
let stderr = stderr . join ( " \n " ) ;
self . child . kill ( ) . unwrap ( ) ;
panic! (
2023-01-27 10:43:16 -05:00
" Inspector test failed with error: {err:?}. \n stdout: \n {stdout} \n stderr: \n {stderr} "
2023-01-12 20:59:13 -05:00
) ;
}
}
}
async fn recv ( & mut self ) -> String {
loop {
2023-08-22 09:25:38 -04:00
// In the rare case this locks up, don't wait longer than one minute
let result = timeout ( Duration ::from_secs ( 60 ) , self . socket . read_frame ( ) )
. await
. expect ( " recv() timeout " )
. map_err ( | e | anyhow! ( e ) ) ;
2023-04-22 05:17:31 -04:00
let message =
2023-08-10 00:29:06 -04:00
String ::from_utf8 ( self . handle_error ( result ) . payload . to_vec ( ) ) . unwrap ( ) ;
2023-01-12 20:59:13 -05:00
if ( self . notification_filter ) ( & message ) {
return message ;
}
}
}
async fn recv_as_json ( & mut self ) -> serde_json ::Value {
let msg = self . recv ( ) . await ;
serde_json ::from_str ( & msg ) . unwrap ( )
}
async fn assert_received_messages (
& mut self ,
responses : & [ & str ] ,
notifications : & [ & str ] ,
) {
let expected_messages = responses . len ( ) + notifications . len ( ) ;
let mut responses_idx = 0 ;
let mut notifications_idx = 0 ;
for _ in 0 .. expected_messages {
let msg = self . recv ( ) . await ;
if msg . starts_with ( r # "{"id":"# ) {
assert! (
msg . starts_with ( responses [ responses_idx ] ) ,
" Doesn't start with {}, instead received {} " ,
responses [ responses_idx ] ,
msg
) ;
responses_idx + = 1 ;
} else {
assert! (
msg . starts_with ( notifications [ notifications_idx ] ) ,
" Doesn't start with {}, instead received {} " ,
notifications [ notifications_idx ] ,
msg
) ;
notifications_idx + = 1 ;
}
}
}
fn stderr_line ( & mut self ) -> String {
self . stderr_lines . next ( ) . unwrap ( )
}
fn stdout_line ( & mut self ) -> String {
self . stdout_lines . next ( ) . unwrap ( )
}
fn assert_stderr_for_inspect ( & mut self ) {
2024-05-28 12:02:01 -04:00
self
. stderr_lines
. assert_lines ( & [ " Visit chrome://inspect to connect to the debugger. " ] ) ;
2023-01-12 20:59:13 -05:00
}
fn assert_stderr_for_inspect_brk ( & mut self ) {
2024-05-28 12:02:01 -04:00
self . stderr_lines . assert_lines ( & [
" Visit chrome://inspect to connect to the debugger. " ,
" Deno is waiting for debugger to connect. " ,
] ) ;
2023-01-12 20:59:13 -05:00
}
}
fn inspect_flag_with_unique_port ( flag_prefix : & str ) -> String {
2023-01-14 23:18:58 -05:00
use std ::sync ::atomic ::AtomicU16 ;
use std ::sync ::atomic ::Ordering ;
2023-01-12 20:59:13 -05:00
static PORT : AtomicU16 = AtomicU16 ::new ( 9229 ) ;
let port = PORT . fetch_add ( 1 , Ordering ::Relaxed ) ;
2023-01-27 10:43:16 -05:00
format! ( " {flag_prefix} =127.0.0.1: {port} " )
2023-01-12 20:59:13 -05:00
}
#[ tokio::test ]
async fn inspector_connect ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let mut child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
let ws_url = stderr_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
2023-04-23 16:55:45 -04:00
let ( _socket , response ) = connect_to_ws ( ws_url ) . await ;
2023-01-12 20:59:13 -05:00
assert_eq! ( " 101 Switching Protocols " , response . status ( ) . to_string ( ) ) ;
child . kill ( ) . unwrap ( ) ;
child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_break_on_first_line ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector2.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( {
" id " :4 ,
" method " :" Runtime.evaluate " ,
" params " :{
2023-01-24 12:54:10 -05:00
" expression " :" Deno[Deno.internal].core.print( \" hello from the inspector \\ n \" ) " ,
2023-01-12 20:59:13 -05:00
" contextId " :1 ,
" includeCommandLineAPI " :true ,
" silent " :false ,
" returnByValue " :true
}
} ) )
. await ;
tester
. assert_received_messages (
2023-09-12 06:42:05 -04:00
& [ r # "{"id":4,"result":{"result":{"type":"object","subtype":"null","value":null}}}"# ] ,
2023-01-12 20:59:13 -05:00
& [ ] ,
)
. await ;
assert_eq! (
& tester . stdout_lines . next ( ) . unwrap ( ) ,
" hello from the inspector "
) ;
tester
. send ( json! ( { " id " :5 , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":5,"result":{}}"# ] , & [ ] )
. await ;
assert_eq! (
& tester . stdout_lines . next ( ) . unwrap ( ) ,
" hello from the script "
) ;
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_pause ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester
. send ( json! ( { " id " :6 , " method " :" Debugger.enable " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":6,"result":{"debuggerId":"# ] , & [ ] )
. await ;
tester
. send ( json! ( { " id " :31 , " method " :" Debugger.pause " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":31,"result":{}}"# ] , & [ ] )
. await ;
tester . child . kill ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_port_collision ( ) {
// Skip this test on WSL, which allows multiple processes to listen on the
2024-01-16 15:50:03 -05:00
// same port, rather than making `bind()` fail with `EADDRINUSE`. We also
// skip this test on Windows because it will occasionally flake, possibly
// due to a similar issue.
if ( cfg! ( target_os = " linux " )
& & std ::env ::var_os ( " WSL_DISTRO_NAME " ) . is_some ( ) )
| | cfg! ( windows )
2023-01-12 20:59:13 -05:00
{
return ;
}
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let inspect_flag = inspect_flag_with_unique_port ( " --inspect " ) ;
let mut child1 = util ::deno_cmd ( )
. arg ( " run " )
. arg ( & inspect_flag )
. arg ( script . clone ( ) )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr_1 = child1 . stderr . take ( ) . unwrap ( ) ;
let mut stderr_1_lines = StdErrLines ::new ( stderr_1 ) ;
let _ = stderr_1_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
let mut child2 = util ::deno_cmd ( )
. arg ( " run " )
. arg ( & inspect_flag )
. arg ( script )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let stderr_2 = child2 . stderr . as_mut ( ) . unwrap ( ) ;
let stderr_2_error_message = std ::io ::BufReader ::new ( stderr_2 )
. lines ( )
. map ( | r | r . unwrap ( ) )
. inspect ( | line | assert! ( ! line . contains ( " Debugger listening " ) ) )
2024-04-11 19:17:10 -04:00
. find ( | line | line . contains ( " Failed to start inspector server " ) ) ;
2023-01-12 20:59:13 -05:00
assert! ( stderr_2_error_message . is_some ( ) ) ;
child1 . kill ( ) . unwrap ( ) ;
child1 . wait ( ) . unwrap ( ) ;
child2 . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_does_not_hang ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector3.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. env ( " NO_COLOR " , " 1 " )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
2024-06-14 17:52:21 -04:00
json! ( { " id " :3 , " method " :" Debugger.setBlackboxPatterns " , " params " :{ " patterns " :[ " /node_modules/|/bower_components/ " ] } } ) ,
2023-01-12 20:59:13 -05:00
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
2024-06-14 17:52:21 -04:00
r # "{"id":2,"result":{"debuggerId":"# ,
r # "{"id":3,"result":"# ,
2023-01-12 20:59:13 -05:00
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"#
] ,
)
. await ;
tester
2024-06-14 17:52:21 -04:00
. send ( json! ( { " id " :4 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
2023-01-12 20:59:13 -05:00
. await ;
tester
. assert_received_messages (
2024-06-14 17:52:21 -04:00
& [ r # "{"id":4,"result":{}}"# ] ,
2023-01-12 20:59:13 -05:00
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
2024-06-14 17:52:21 -04:00
. send ( json! ( { " id " :5 , " method " :" Debugger.resume " } ) )
2023-01-12 20:59:13 -05:00
. await ;
tester
. assert_received_messages (
2024-06-14 17:52:21 -04:00
& [ r # "{"id":5,"result":{}}"# ] ,
2023-01-12 20:59:13 -05:00
& [ r # "{"method":"Debugger.resumed","params":{}}"# ] ,
)
. await ;
for i in 0 .. 128 u32 {
let request_id = i + 10 ;
// Expect the number {i} on stdout.
let s = i . to_string ( ) ;
assert_eq! ( tester . stdout_lines . next ( ) . unwrap ( ) , s ) ;
tester
. assert_received_messages (
& [ ] ,
& [
r # "{"method":"Runtime.consoleAPICalled","# ,
r # "{"method":"Debugger.paused","# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :request_id , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages (
2023-01-27 10:43:16 -05:00
& [ & format! ( r # "{{"id":{request_id},"result":{{}}}}"# ) ] ,
2023-01-12 20:59:13 -05:00
& [ r # "{"method":"Debugger.resumed","params":{}}"# ] ,
)
. await ;
}
// Check that we can gracefully close the websocket connection.
2023-04-22 05:17:31 -04:00
tester
. socket
2023-08-10 00:29:06 -04:00
. write_frame ( Frame ::close_raw ( vec! [ ] . into ( ) ) )
2023-04-22 05:17:31 -04:00
. await
. unwrap ( ) ;
2023-01-12 20:59:13 -05:00
assert_eq! ( & tester . stdout_lines . next ( ) . unwrap ( ) , " done " ) ;
assert! ( tester . child . wait ( ) . unwrap ( ) . success ( ) ) ;
}
#[ tokio::test ]
async fn inspector_without_brk_runs_code ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector4.js " ) ;
let mut child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
let _ = stderr_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
// Check that inspector actually runs code without waiting for inspector
// connection.
let stdout = child . stdout . as_mut ( ) . unwrap ( ) ;
let mut stdout_lines =
std ::io ::BufReader ::new ( stdout ) . lines ( ) . map ( | r | r . unwrap ( ) ) ;
let stdout_first_line = stdout_lines . next ( ) . unwrap ( ) ;
assert_eq! ( stdout_first_line , " hello " ) ;
child . kill ( ) . unwrap ( ) ;
child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_runtime_evaluate_does_not_crash ( ) {
let child = util ::deno_cmd ( )
. arg ( " repl " )
. arg ( " --allow-read " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. stdin ( std ::process ::Stdio ::piped ( ) )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
let stdin = tester . child . stdin . take ( ) . unwrap ( ) ;
tester . assert_stderr_for_inspect ( ) ;
assert_starts_with! ( & tester . stdout_line ( ) , " Deno " ) ;
assert_eq! (
& tester . stdout_line ( ) ,
" exit using ctrl+d, ctrl+c, or close() "
) ;
assert_eq! ( & tester . stderr_line ( ) , " Debugger session started. " ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( {
" id " :3 ,
" method " :" Runtime.compileScript " ,
" params " :{
" expression " :" Deno.cwd() " ,
" sourceURL " :" " ,
" persistScript " :false ,
" executionContextId " :1
}
} ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":3,"result":{}}"# ] , & [ ] )
. await ;
tester
. send ( json! ( {
" id " :4 ,
" method " :" Runtime.evaluate " ,
" params " :{
" expression " :" Deno.cwd() " ,
" objectGroup " :" console " ,
" includeCommandLineAPI " :true ,
" silent " :false ,
" contextId " :1 ,
" returnByValue " :true ,
" generatePreview " :true ,
" userGesture " :true ,
" awaitPromise " :false ,
" replMode " :true
}
} ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":4,"result":{"result":{"type":"string","value":""# ] ,
& [ ] ,
)
. await ;
tester
. send ( json! ( {
" id " :5 ,
" method " :" Runtime.evaluate " ,
" params " :{
" expression " :" console.error('done'); " ,
" objectGroup " :" console " ,
" includeCommandLineAPI " :true ,
" silent " :false ,
" contextId " :1 ,
" returnByValue " :true ,
" generatePreview " :true ,
" userGesture " :true ,
" awaitPromise " :false ,
" replMode " :true
}
} ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":5,"result":{"result":{"type":"undefined"}}}"# ] ,
& [ r # "{"method":"Runtime.consoleAPICalled"# ] ,
)
. await ;
assert_eq! ( & tester . stderr_line ( ) , " done " ) ;
drop ( stdin ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_json ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let mut child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
let ws_url = stderr_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
let mut url = ws_url . clone ( ) ;
let _ = url . set_scheme ( " http " ) ;
url . set_path ( " /json " ) ;
2023-08-15 18:30:33 -04:00
let client = reqwest ::Client ::new ( ) ;
// Ensure that the webSocketDebuggerUrl matches the host header
for ( host , expected ) in [
( None , ws_url . as_str ( ) ) ,
( Some ( " some.random.host " ) , " ws://some.random.host/ " ) ,
( Some ( " some.random.host:1234 " ) , " ws://some.random.host:1234/ " ) ,
( Some ( " [::1]:1234 " ) , " ws://[::1]:1234/ " ) ,
] {
let mut req = reqwest ::Request ::new ( reqwest ::Method ::GET , url . clone ( ) ) ;
if let Some ( host ) = host {
2023-12-27 11:59:57 -05:00
req . headers_mut ( ) . insert (
reqwest ::header ::HOST ,
reqwest ::header ::HeaderValue ::from_static ( host ) ,
) ;
2023-08-15 18:30:33 -04:00
}
let resp = client . execute ( req ) . await . unwrap ( ) ;
assert_eq! ( resp . status ( ) , reqwest ::StatusCode ::OK ) ;
let endpoint_list : Vec < deno_core ::serde_json ::Value > =
serde_json ::from_str ( & resp . text ( ) . await . unwrap ( ) ) . unwrap ( ) ;
let matching_endpoint = endpoint_list . iter ( ) . find ( | e | {
e [ " webSocketDebuggerUrl " ]
. as_str ( )
. unwrap ( )
. contains ( expected )
} ) ;
assert! ( matching_endpoint . is_some ( ) ) ;
}
2023-01-12 20:59:13 -05:00
child . kill ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_json_list ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let mut child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
let ws_url = stderr_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
let mut url = ws_url . clone ( ) ;
let _ = url . set_scheme ( " http " ) ;
url . set_path ( " /json/list " ) ;
let resp = reqwest ::get ( url ) . await . unwrap ( ) ;
assert_eq! ( resp . status ( ) , reqwest ::StatusCode ::OK ) ;
let endpoint_list : Vec < deno_core ::serde_json ::Value > =
serde_json ::from_str ( & resp . text ( ) . await . unwrap ( ) ) . unwrap ( ) ;
let matching_endpoint = endpoint_list
. iter ( )
. find ( | e | e [ " webSocketDebuggerUrl " ] = = ws_url . as_str ( ) ) ;
assert! ( matching_endpoint . is_some ( ) ) ;
child . kill ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_connect_non_ws ( ) {
// https://github.com/denoland/deno/issues/11449
// Verify we don't panic if non-WS connection is being established
let script = util ::testdata_path ( ) . join ( " inspector/inspector1.js " ) ;
let mut child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. stderr_piped ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
2024-05-28 12:02:01 -04:00
let stderr = child . stderr . take ( ) . unwrap ( ) ;
let mut stderr_lines = StdErrLines ::new ( stderr ) ;
let mut ws_url = stderr_lines . extract_ws_url ( ) ;
2023-01-12 20:59:13 -05:00
// Change scheme to URL and try send a request. We're not interested
// in the request result, just that the process doesn't panic.
ws_url . set_scheme ( " http " ) . unwrap ( ) ;
let resp = reqwest ::get ( ws_url ) . await . unwrap ( ) ;
assert_eq! ( " 400 Bad Request " , resp . status ( ) . to_string ( ) ) ;
child . kill ( ) . unwrap ( ) ;
child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_break_on_first_line_in_test ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspector_test.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " test " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. arg ( script )
. env ( " NO_COLOR " , " 1 " )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( {
" id " :4 ,
" method " :" Runtime.evaluate " ,
" params " :{
" expression " :" 1 + 1 " ,
" contextId " :1 ,
" includeCommandLineAPI " :true ,
" silent " :false ,
" returnByValue " :true
}
} ) )
. await ;
tester . assert_received_messages (
& [ r # "{"id":4,"result":{"result":{"type":"number","value":2,"description":"2"}}}"# ] ,
& [ ] ,
)
. await ;
tester
. send ( json! ( { " id " :5 , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":5,"result":{}}"# ] , & [ ] )
. await ;
assert_starts_with! ( & tester . stdout_line ( ) , " running 1 test from " ) ;
2023-12-13 10:07:26 -05:00
let line = tester . stdout_line ( ) ;
2024-05-08 22:45:06 -04:00
assert_contains! ( line , " basic test ... ok " ) ;
2023-01-12 20:59:13 -05:00
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_with_ts_files ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/test.ts " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( " --check " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
fn notification_filter ( msg : & str ) -> bool {
( msg . starts_with ( r # "{"method":"Debugger.scriptParsed","# )
& & msg . contains ( " testdata/inspector " ) )
| | ! msg . starts_with ( r # "{"method":"Debugger.scriptParsed","# )
}
let mut tester = InspectorTester ::create ( child , notification_filter ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
2024-05-08 22:45:06 -04:00
assert_eq! ( & tester . stderr_line ( ) , " Debugger session started. " ) ;
2023-01-12 20:59:13 -05:00
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
// receive messages with sources from this test
2024-05-18 11:42:03 -04:00
let mut scripts = vec! [
tester . recv ( ) . await ,
tester . recv ( ) . await ,
tester . recv ( ) . await ,
] ;
let script1 = scripts . remove (
scripts
. iter ( )
. position ( | s | s . contains ( " testdata/inspector/test.ts " ) )
. unwrap ( ) ,
) ;
2023-01-12 20:59:13 -05:00
let script1_id = {
let v : serde_json ::Value = serde_json ::from_str ( & script1 ) . unwrap ( ) ;
v [ " params " ] [ " scriptId " ] . as_str ( ) . unwrap ( ) . to_string ( )
} ;
2024-05-18 11:42:03 -04:00
let script2 = scripts . remove (
scripts
. iter ( )
. position ( | s | s . contains ( " testdata/inspector/foo.ts " ) )
. unwrap ( ) ,
) ;
2023-01-12 20:59:13 -05:00
let script2_id = {
let v : serde_json ::Value = serde_json ::from_str ( & script2 ) . unwrap ( ) ;
v [ " params " ] [ " scriptId " ] . as_str ( ) . unwrap ( ) . to_string ( )
} ;
2024-05-18 11:42:03 -04:00
let script3 = scripts . remove ( 0 ) ;
2024-05-08 22:45:06 -04:00
assert_contains! ( script3 , " testdata/inspector/bar.js " ) ;
2023-01-12 20:59:13 -05:00
let script3_id = {
let v : serde_json ::Value = serde_json ::from_str ( & script3 ) . unwrap ( ) ;
v [ " params " ] [ " scriptId " ] . as_str ( ) . unwrap ( ) . to_string ( )
} ;
tester
. assert_received_messages ( & [ r # "{"id":2,"result":{"debuggerId":"# ] , & [ ] )
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester . send_many (
& [
json! ( { " id " :4 , " method " :" Debugger.getScriptSource " , " params " :{ " scriptId " :script1_id . as_str ( ) } } ) ,
json! ( { " id " :5 , " method " :" Debugger.getScriptSource " , " params " :{ " scriptId " :script2_id . as_str ( ) } } ) ,
json! ( { " id " :6 , " method " :" Debugger.getScriptSource " , " params " :{ " scriptId " :script3_id . as_str ( ) } } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":4,"result":{"scriptSource":"import { foo } from \"./foo.ts\";\nimport { bar } from \"./bar.js\";\nconsole.log(foo());\nconsole.log(bar());\n//# sourceMappingURL=data:application/json;base64,"# ,
2023-08-21 05:53:52 -04:00
r # "{"id":5,"result":{"scriptSource":"class Foo {\n hello() {\n return \"hello\";\n }\n}\nexport function foo() {\n const f = new Foo();\n return f.hello();\n}\n//# sourceMappingURL=data:application/json;base64,"# ,
2023-01-12 20:59:13 -05:00
r # "{"id":6,"result":{"scriptSource":"export function bar() {\n return \"world\";\n}\n"# ,
] ,
& [ ] ,
)
. await ;
tester
. send ( json! ( { " id " :7 , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":7,"result":{}}"# ] , & [ ] )
. await ;
assert_eq! ( & tester . stdout_line ( ) , " hello " ) ;
assert_eq! ( & tester . stdout_line ( ) , " world " ) ;
tester . assert_received_messages (
& [ ] ,
& [
r # "{"method":"Debugger.resumed","params":{}}"# ,
r # "{"method":"Runtime.consoleAPICalled","# ,
r # "{"method":"Runtime.consoleAPICalled","# ,
2023-03-22 08:38:56 -04:00
r # "{"method":"Runtime.executionContextDestroyed","params":{"executionContextId":1"# ,
2023-01-12 20:59:13 -05:00
] ,
)
. await ;
assert_eq! (
2024-05-08 22:45:06 -04:00
& tester . stderr_line ( ) ,
" Program finished. Waiting for inspector to disconnect to exit the process... "
) ;
2024-05-28 12:02:01 -04:00
assert! ( ! tester . stderr_lines . check_lines . is_empty ( ) ) ;
2023-01-12 20:59:13 -05:00
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_memory ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/memory.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send_many ( & [
json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) ,
json! ( { " id " :4 , " method " :" HeapProfiler.enable " } ) ,
] )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# , r # "{"id":4,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( { " id " :5 , " method " :" Runtime.getHeapUsage " , " params " : { } } ) )
. await ;
let json_msg = tester . recv_as_json ( ) . await ;
assert_eq! ( json_msg [ " id " ] . as_i64 ( ) . unwrap ( ) , 5 ) ;
let result = & json_msg [ " result " ] ;
assert! (
result [ " usedSize " ] . as_i64 ( ) . unwrap ( )
< = result [ " totalSize " ] . as_i64 ( ) . unwrap ( )
) ;
tester
. send ( json! ( {
" id " :6 ,
" method " :" HeapProfiler.takeHeapSnapshot " ,
" params " : {
" reportProgress " : true ,
" treatGlobalObjectsAsRoots " : true ,
" captureNumberValue " : false
}
} ) )
. await ;
let mut progress_report_completed = false ;
loop {
let msg = tester . recv ( ) . await ;
// TODO(bartlomieju): can be abstracted
if ! progress_report_completed
& & msg . starts_with (
r # "{"method":"HeapProfiler.reportHeapSnapshotProgress","params""# ,
)
{
let json_msg : serde_json ::Value = serde_json ::from_str ( & msg ) . unwrap ( ) ;
if let Some ( finished ) = json_msg [ " params " ] . get ( " finished " ) {
progress_report_completed = finished . as_bool ( ) . unwrap ( ) ;
}
continue ;
}
if msg . starts_with ( r # "{"method":"HeapProfiler.reportHeapSnapshotProgress","params":{"done":"# , ) {
continue ;
}
if msg . starts_with ( r # "{"id":6,"result":{}}"# ) {
assert! ( progress_report_completed ) ;
break ;
}
}
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
#[ tokio::test ]
async fn inspector_profile ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/memory.js " ) ;
let child = util ::deno_cmd ( )
. arg ( " run " )
. arg ( inspect_flag_with_unique_port ( " --inspect-brk " ) )
. arg ( script )
2023-11-17 10:05:42 -05:00
. piped_output ( )
2023-01-12 20:59:13 -05:00
. spawn ( )
. unwrap ( ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send_many ( & [
json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) ,
json! ( { " id " :4 , " method " :" Profiler.enable " } ) ,
] )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# , r # "{"id":4,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester . send_many (
& [
json! ( { " id " :5 , " method " :" Profiler.setSamplingInterval " , " params " :{ " interval " : 100 } } ) ,
json! ( { " id " :6 , " method " :" Profiler.start " , " params " :{ } } ) ,
] ,
) . await ;
tester
. assert_received_messages (
& [ r # "{"id":5,"result":{}}"# , r # "{"id":6,"result":{}}"# ] ,
& [ ] ,
)
. await ;
tokio ::time ::sleep ( tokio ::time ::Duration ::from_millis ( 500 ) ) . await ;
tester
. send ( json! ( { " id " :7 , " method " :" Profiler.stop " , " params " : { } } ) )
. await ;
let json_msg = tester . recv_as_json ( ) . await ;
assert_eq! ( json_msg [ " id " ] . as_i64 ( ) . unwrap ( ) , 7 ) ;
let result = & json_msg [ " result " ] ;
let profile = & result [ " profile " ] ;
assert! (
profile [ " startTime " ] . as_i64 ( ) . unwrap ( )
< profile [ " endTime " ] . as_i64 ( ) . unwrap ( )
) ;
profile [ " samples " ] . as_array ( ) . unwrap ( ) ;
profile [ " nodes " ] . as_array ( ) . unwrap ( ) ;
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
2023-02-16 05:44:16 -05:00
// TODO(bartlomieju): this test became flaky on CI after wiring up "ext/node"
// compatibility layer. Can't reproduce this problem locally for either Mac M1
// or Linux. Ignoring for now to unblock further integration of "ext/node".
#[ ignore ]
2023-01-12 20:59:13 -05:00
#[ tokio::test ]
async fn inspector_break_on_first_line_npm_esm ( ) {
2023-11-17 10:05:42 -05:00
let context = TestContextBuilder ::for_npm ( ) . build ( ) ;
let child = context
. new_command ( )
. args_vec ( [
" run " ,
& inspect_flag_with_unique_port ( " --inspect-brk " ) ,
" npm:@denotest/bin/cli-esm " ,
" this " ,
" is " ,
" a " ,
" test " ,
] )
. spawn_with_piped_output ( ) ;
2023-01-12 20:59:13 -05:00
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( { " id " :4 , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":4,"result":{}}"# ] , & [ ] )
. await ;
assert_eq! ( & tester . stdout_line ( ) , " this " ) ;
assert_eq! ( & tester . stdout_line ( ) , " is " ) ;
assert_eq! ( & tester . stdout_line ( ) , " a " ) ;
assert_eq! ( & tester . stdout_line ( ) , " test " ) ;
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
2023-02-16 05:44:16 -05:00
// TODO(bartlomieju): this test became flaky on CI after wiring up "ext/node"
// compatibility layer. Can't reproduce this problem locally for either Mac M1
// or Linux. Ignoring for now to unblock further integration of "ext/node".
#[ ignore ]
2023-01-12 20:59:13 -05:00
#[ tokio::test ]
async fn inspector_break_on_first_line_npm_cjs ( ) {
2023-11-17 10:05:42 -05:00
let context = TestContextBuilder ::for_npm ( ) . build ( ) ;
let child = context
. new_command ( )
. args_vec ( [
" run " ,
& inspect_flag_with_unique_port ( " --inspect-brk " ) ,
" npm:@denotest/bin/cli-cjs " ,
" this " ,
" is " ,
" a " ,
" test " ,
] )
. spawn_with_piped_output ( ) ;
2023-01-12 20:59:13 -05:00
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( { " id " :4 , " method " :" Debugger.resume " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":4,"result":{}}"# ] , & [ ] )
. await ;
assert_eq! ( & tester . stdout_line ( ) , " this " ) ;
assert_eq! ( & tester . stdout_line ( ) , " is " ) ;
assert_eq! ( & tester . stdout_line ( ) , " a " ) ;
assert_eq! ( & tester . stdout_line ( ) , " test " ) ;
tester . child . kill ( ) . unwrap ( ) ;
tester . child . wait ( ) . unwrap ( ) ;
}
2023-02-16 05:44:16 -05:00
// TODO(bartlomieju): this test became flaky on CI after wiring up "ext/node"
// compatibility layer. Can't reproduce this problem locally for either Mac M1
// or Linux. Ignoring for now to unblock further integration of "ext/node".
#[ ignore ]
2023-01-12 20:59:13 -05:00
#[ tokio::test ]
async fn inspector_error_with_npm_import ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/error_with_npm_import.js " ) ;
2023-11-17 10:05:42 -05:00
let context = TestContextBuilder ::for_npm ( ) . build ( ) ;
let child = context
. new_command ( )
. args_vec ( [
" run " ,
" -A " ,
& inspect_flag_with_unique_port ( " --inspect-brk " ) ,
& script . to_string_lossy ( ) ,
] )
. spawn_with_piped_output ( ) ;
2023-01-12 20:59:13 -05:00
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages (
& [ r # "{"id":3,"result":{}}"# ] ,
& [ r # "{"method":"Debugger.paused","# ] ,
)
. await ;
tester
. send ( json! ( { " id " :4 , " method " :" Debugger.resume " } ) )
. await ;
tester
2023-12-18 05:43:02 -05:00
. assert_received_messages (
& [ r # "{"id":4,"result":{}}"# ] ,
& [ r # "{"method":"Runtime.exceptionThrown","# ] ,
)
2023-01-12 20:59:13 -05:00
. await ;
assert_eq! ( & tester . stderr_line ( ) , " Debugger session started. " ) ;
assert_eq! ( & tester . stderr_line ( ) , " error: Uncaught Error: boom! " ) ;
assert_eq! ( tester . child . wait ( ) . unwrap ( ) . code ( ) , Some ( 1 ) ) ;
}
#[ tokio::test ]
async fn inspector_wait ( ) {
let script = util ::testdata_path ( ) . join ( " inspector/inspect_wait.js " ) ;
2023-11-17 10:05:42 -05:00
let test_context = TestContextBuilder ::new ( ) . use_temp_cwd ( ) . build ( ) ;
let temp_dir = test_context . temp_dir ( ) ;
let child = test_context
. new_command ( )
. args_vec ( [
" run " ,
" -A " ,
& inspect_flag_with_unique_port ( " --inspect-wait " ) ,
& script . to_string_lossy ( ) ,
] )
. spawn_with_piped_output ( ) ;
2023-01-12 20:59:13 -05:00
tokio ::time ::sleep ( tokio ::time ::Duration ::from_millis ( 300 ) ) . await ;
assert! ( ! temp_dir . path ( ) . join ( " hello.txt " ) . exists ( ) ) ;
let mut tester = InspectorTester ::create ( child , ignore_script_parsed ) . await ;
tester . assert_stderr_for_inspect_brk ( ) ;
tester
. send_many ( & [
json! ( { " id " :1 , " method " :" Runtime.enable " } ) ,
json! ( { " id " :2 , " method " :" Debugger.enable " } ) ,
] )
. await ;
tester . assert_received_messages (
& [
r # "{"id":1,"result":{}}"# ,
r # "{"id":2,"result":{"debuggerId":"# ,
] ,
& [
r # "{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"# ,
] ,
)
. await ;
// TODO(bartlomieju): ideally this shouldn't be needed, but currently there's
// no way to express that in inspector code. Most clients always send this
// message anyway.
tester
. send ( json! ( { " id " :3 , " method " :" Runtime.runIfWaitingForDebugger " } ) )
. await ;
tester
. assert_received_messages ( & [ r # "{"id":3,"result":{}}"# ] , & [ ] )
. await ;
assert_eq! ( & tester . stderr_line ( ) , " Debugger session started. " ) ;
tokio ::time ::sleep ( tokio ::time ::Duration ::from_millis ( 300 ) ) . await ;
assert_eq! ( & tester . stderr_line ( ) , " did run " ) ;
assert! ( temp_dir . path ( ) . join ( " hello.txt " ) . exists ( ) ) ;
tester . child . kill ( ) . unwrap ( ) ;
}