2023-01-02 16:00:42 -05:00
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2022-02-16 13:53:17 -05:00
2023-02-07 14:22:46 -05:00
const core = globalThis . Deno . core ;
const ops = core . ops ;
2023-03-08 06:44:54 -05:00
import { setExitHandler } from "ext:runtime/30_os.js" ;
2023-04-30 05:11:37 -04:00
import { Console } from "ext:deno_console/01_console.js" ;
2023-03-08 06:44:54 -05:00
import { serializePermissions } from "ext:runtime/10_permissions.js" ;
2023-08-04 09:29:04 -04:00
import { setTimeout } from "ext:deno_web/02_timers.js" ;
2023-03-08 06:44:54 -05:00
import { assert } from "ext:deno_web/00_infra.js" ;
2023-02-07 14:22:46 -05:00
const primordials = globalThis . _ _bootstrap . primordials ;
const {
ArrayPrototypeFilter ,
ArrayPrototypeJoin ,
ArrayPrototypePush ,
ArrayPrototypeShift ,
DateNow ,
Error ,
FunctionPrototype ,
Map ,
MapPrototypeGet ,
MapPrototypeHas ,
MapPrototypeSet ,
MathCeil ,
ObjectKeys ,
ObjectPrototypeIsPrototypeOf ,
Promise ,
SafeArrayIterator ,
Set ,
2023-08-09 19:40:34 -04:00
StringPrototypeReplaceAll ,
2023-02-07 14:22:46 -05:00
SymbolToStringTag ,
TypeError ,
} = primordials ;
const opSanitizerDelayResolveQueue = [ ] ;
2023-04-13 13:43:23 -04:00
let hasSetOpSanitizerDelayMacrotask = false ;
2023-02-07 14:22:46 -05:00
// Even if every resource is closed by the end of a test, there can be a delay
// until the pending ops have all finished. This function returns a promise
// that resolves when it's (probably) fine to run the op sanitizer.
//
// This is implemented by adding a macrotask callback that runs after the
2023-09-16 01:48:31 -04:00
// all ready async ops resolve, and the timer macrotask. Using just a macrotask
// callback without delaying is sufficient, because when the macrotask callback
// runs after async op dispatch, we know that all async ops that can currently
// return `Poll::Ready` have done so, and have been dispatched to JS.
//
// Worker ops are an exception to this, because there is no way for the user to
// await shutdown of the worker from the thread calling `worker.terminate()`.
// Because of this, we give extra leeway for worker ops to complete, by waiting
// for a whole millisecond if there are pending worker ops.
function opSanitizerDelay ( hasPendingWorkerOps ) {
2023-04-13 13:43:23 -04:00
if ( ! hasSetOpSanitizerDelayMacrotask ) {
core . setMacrotaskCallback ( handleOpSanitizerDelayMacrotask ) ;
hasSetOpSanitizerDelayMacrotask = true ;
}
2023-09-16 01:48:31 -04:00
const p = new Promise ( ( resolve ) => {
// Schedule an async op to complete immediately to ensure the macrotask is
// run. We rely on the fact that enqueueing the resolver callback during the
// timeout callback will mean that the resolver gets called in the same
// event loop tick as the timeout callback.
2023-02-07 14:22:46 -05:00
setTimeout ( ( ) => {
ArrayPrototypePush ( opSanitizerDelayResolveQueue , resolve ) ;
2023-09-16 01:48:31 -04:00
} , hasPendingWorkerOps ? 1 : 0 ) ;
2023-02-07 14:22:46 -05:00
} ) ;
2023-09-16 01:48:31 -04:00
return p ;
2023-02-07 14:22:46 -05:00
}
function handleOpSanitizerDelayMacrotask ( ) {
2023-09-16 01:48:31 -04:00
const resolve = ArrayPrototypeShift ( opSanitizerDelayResolveQueue ) ;
if ( resolve ) {
resolve ( ) ;
return opSanitizerDelayResolveQueue . length === 0 ;
}
return undefined ; // we performed no work, so can skip microtasks checkpoint
2023-02-07 14:22:46 -05:00
}
// An async operation to $0 was started in this test, but never completed. This is often caused by not $1.
// An async operation to $0 was started in this test, but never completed. Async operations should not complete in a test if they were not started in that test.
// deno-fmt-ignore
const OP _DETAILS = {
"op_blob_read_part" : [ "read from a Blob or File" , "awaiting the result of a Blob or File read" ] ,
"op_broadcast_recv" : [ "receive a message from a BroadcastChannel" , "closing the BroadcastChannel" ] ,
"op_broadcast_send" : [ "send a message to a BroadcastChannel" , "closing the BroadcastChannel" ] ,
"op_chmod_async" : [ "change the permissions of a file" , "awaiting the result of a `Deno.chmod` call" ] ,
"op_chown_async" : [ "change the owner of a file" , "awaiting the result of a `Deno.chown` call" ] ,
"op_copy_file_async" : [ "copy a file" , "awaiting the result of a `Deno.copyFile` call" ] ,
"op_crypto_decrypt" : [ "decrypt data" , "awaiting the result of a `crypto.subtle.decrypt` call" ] ,
"op_crypto_derive_bits" : [ "derive bits from a key" , "awaiting the result of a `crypto.subtle.deriveBits` call" ] ,
"op_crypto_encrypt" : [ "encrypt data" , "awaiting the result of a `crypto.subtle.encrypt` call" ] ,
"op_crypto_generate_key" : [ "generate a key" , "awaiting the result of a `crypto.subtle.generateKey` call" ] ,
"op_crypto_sign_key" : [ "sign data" , "awaiting the result of a `crypto.subtle.sign` call" ] ,
"op_crypto_subtle_digest" : [ "digest data" , "awaiting the result of a `crypto.subtle.digest` call" ] ,
"op_crypto_verify_key" : [ "verify data" , "awaiting the result of a `crypto.subtle.verify` call" ] ,
"op_net_recv_udp" : [ "receive a datagram message via UDP" , "awaiting the result of `Deno.DatagramConn#receive` call, or not breaking out of a for await loop looping over a `Deno.DatagramConn`" ] ,
"op_net_recv_unixpacket" : [ "receive a datagram message via Unixpacket" , "awaiting the result of `Deno.DatagramConn#receive` call, or not breaking out of a for await loop looping over a `Deno.DatagramConn`" ] ,
"op_net_send_udp" : [ "send a datagram message via UDP" , "awaiting the result of `Deno.DatagramConn#send` call" ] ,
"op_net_send_unixpacket" : [ "send a datagram message via Unixpacket" , "awaiting the result of `Deno.DatagramConn#send` call" ] ,
"op_dns_resolve" : [ "resolve a DNS name" , "awaiting the result of a `Deno.resolveDns` call" ] ,
"op_fdatasync_async" : [ "flush pending data operations for a file to disk" , "awaiting the result of a `Deno.fdatasync` call" ] ,
"op_fetch_send" : [ "send a HTTP request" , "awaiting the result of a `fetch` call" ] ,
2023-04-28 10:48:00 -04:00
"op_ffi_call_nonblocking" : [ "do a non blocking ffi call" , "awaiting the returned promise" ] ,
"op_ffi_call_ptr_nonblocking" : [ "do a non blocking ffi call" , "awaiting the returned promise" ] ,
2023-02-07 14:22:46 -05:00
"op_flock_async" : [ "lock a file" , "awaiting the result of a `Deno.flock` call" ] ,
"op_fs_events_poll" : [ "get the next file system event" , "breaking out of a for await loop looping over `Deno.FsEvents`" ] ,
"op_fstat_async" : [ "get file metadata" , "awaiting the result of a `Deno.File#fstat` call" ] ,
"op_fsync_async" : [ "flush pending data operations for a file to disk" , "awaiting the result of a `Deno.fsync` call" ] ,
"op_ftruncate_async" : [ "truncate a file" , "awaiting the result of a `Deno.ftruncate` call" ] ,
"op_funlock_async" : [ "unlock a file" , "awaiting the result of a `Deno.funlock` call" ] ,
"op_futime_async" : [ "change file timestamps" , "awaiting the result of a `Deno.futime` call" ] ,
"op_http_accept" : [ "accept a HTTP request" , "closing a `Deno.HttpConn`" ] ,
"op_http_shutdown" : [ "shutdown a HTTP connection" , "awaiting `Deno.HttpEvent#respondWith`" ] ,
"op_http_upgrade_websocket" : [ "upgrade a HTTP connection to a WebSocket" , "awaiting `Deno.HttpEvent#respondWith`" ] ,
"op_http_write_headers" : [ "write HTTP response headers" , "awaiting `Deno.HttpEvent#respondWith`" ] ,
"op_http_write" : [ "write HTTP response body" , "awaiting `Deno.HttpEvent#respondWith`" ] ,
"op_link_async" : [ "create a hard link" , "awaiting the result of a `Deno.link` call" ] ,
"op_make_temp_dir_async" : [ "create a temporary directory" , "awaiting the result of a `Deno.makeTempDir` call" ] ,
"op_make_temp_file_async" : [ "create a temporary file" , "awaiting the result of a `Deno.makeTempFile` call" ] ,
"op_message_port_recv_message" : [ "receive a message from a MessagePort" , "awaiting the result of not closing a `MessagePort`" ] ,
"op_mkdir_async" : [ "create a directory" , "awaiting the result of a `Deno.mkdir` call" ] ,
"op_net_accept_tcp" : [ "accept a TCP stream" , "closing a `Deno.Listener`" ] ,
"op_net_accept_unix" : [ "accept a Unix stream" , "closing a `Deno.Listener`" ] ,
"op_net_connect_tcp" : [ "connect to a TCP server" , "awaiting a `Deno.connect` call" ] ,
"op_net_connect_unix" : [ "connect to a Unix server" , "awaiting a `Deno.connect` call" ] ,
"op_open_async" : [ "open a file" , "awaiting the result of a `Deno.open` call" ] ,
"op_read_dir_async" : [ "read a directory" , "collecting all items in the async iterable returned from a `Deno.readDir` call" ] ,
"op_read_link_async" : [ "read a symlink" , "awaiting the result of a `Deno.readLink` call" ] ,
"op_realpath_async" : [ "resolve a path" , "awaiting the result of a `Deno.realpath` call" ] ,
"op_remove_async" : [ "remove a file or directory" , "awaiting the result of a `Deno.remove` call" ] ,
"op_rename_async" : [ "rename a file or directory" , "awaiting the result of a `Deno.rename` call" ] ,
"op_run_status" : [ "get the status of a subprocess" , "awaiting the result of a `Deno.Process#status` call" ] ,
"op_seek_async" : [ "seek in a file" , "awaiting the result of a `Deno.File#seek` call" ] ,
"op_signal_poll" : [ "get the next signal" , "un-registering a OS signal handler" ] ,
"op_sleep" : [ "sleep for a duration" , "cancelling a `setTimeout` or `setInterval` call" ] ,
"op_stat_async" : [ "get file metadata" , "awaiting the result of a `Deno.stat` call" ] ,
"op_symlink_async" : [ "create a symlink" , "awaiting the result of a `Deno.symlink` call" ] ,
"op_net_accept_tls" : [ "accept a TLS stream" , "closing a `Deno.TlsListener`" ] ,
"op_net_connect_tls" : [ "connect to a TLS server" , "awaiting a `Deno.connectTls` call" ] ,
"op_tls_handshake" : [ "perform a TLS handshake" , "awaiting a `Deno.TlsConn#handshake` call" ] ,
"op_tls_start" : [ "start a TLS connection" , "awaiting a `Deno.startTls` call" ] ,
"op_truncate_async" : [ "truncate a file" , "awaiting the result of a `Deno.truncate` call" ] ,
"op_utime_async" : [ "change file timestamps" , "awaiting the result of a `Deno.utime` call" ] ,
2023-09-16 01:48:31 -04:00
"op_host_recv_message" : [ "receive a message from a web worker" , "terminating a `Worker`" ] ,
"op_host_recv_ctrl" : [ "receive a message from a web worker" , "terminating a `Worker`" ] ,
2023-02-07 14:22:46 -05:00
"op_ws_close" : [ "close a WebSocket" , "awaiting until the `close` event is emitted on a `WebSocket`, or the `WebSocketStream#closed` promise resolves" ] ,
"op_ws_create" : [ "create a WebSocket" , "awaiting until the `open` event is emitted on a `WebSocket`, or the result of a `WebSocketStream#connection` promise" ] ,
"op_ws_next_event" : [ "receive the next message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
2023-04-25 07:53:06 -04:00
"op_ws_send_text" : [ "send a message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
"op_ws_send_binary" : [ "send a message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
2023-10-04 17:43:58 -04:00
"op_ws_send_binary_ab" : [ "send a message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
2023-04-25 07:53:06 -04:00
"op_ws_send_ping" : [ "send a message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
"op_ws_send_pong" : [ "send a message on a WebSocket" , "closing a `WebSocket` or `WebSocketStream`" ] ,
2023-02-07 14:22:46 -05:00
} ;
2023-09-19 06:10:20 -04:00
let opIdHostRecvMessage = - 1 ;
let opIdHostRecvCtrl = - 1 ;
let opNames = null ;
function populateOpNames ( ) {
opNames = core . ops . op _op _names ( ) ;
opIdHostRecvMessage = opNames . indexOf ( "op_host_recv_message" ) ;
opIdHostRecvCtrl = opNames . indexOf ( "op_host_recv_ctrl" ) ;
2023-09-16 01:48:31 -04:00
}
2023-02-07 14:22:46 -05:00
// Wrap test function in additional assertion that makes sure
// the test case does not leak async "ops" - ie. number of async
// completed ops after the test is the same as number of dispatched
// ops. Note that "unref" ops are ignored since in nature that are
// optional.
function assertOps ( fn ) {
/** @param desc {TestDescription | TestStepDescription} */
return async function asyncOpSanitizer ( desc ) {
2023-09-19 06:10:20 -04:00
if ( opNames === null ) populateOpNames ( ) ;
const res = core . ops . op _test _op _sanitizer _collect (
desc . id ,
false ,
opIdHostRecvMessage ,
opIdHostRecvCtrl ,
) ;
if ( res !== 0 ) {
await opSanitizerDelay ( res === 2 ) ;
core . ops . op _test _op _sanitizer _collect (
desc . id ,
true ,
opIdHostRecvMessage ,
opIdHostRecvCtrl ,
) ;
2023-09-16 01:48:31 -04:00
}
2023-09-19 06:10:20 -04:00
const preTraces = new Map ( core . opCallTraces ) ;
2023-09-16 01:48:31 -04:00
let postTraces ;
2023-09-19 06:10:20 -04:00
let report = null ;
2023-09-16 01:48:31 -04:00
2023-02-07 14:22:46 -05:00
try {
2023-03-25 15:32:11 -04:00
const innerResult = await fn ( desc ) ;
if ( innerResult ) return innerResult ;
2023-02-07 14:22:46 -05:00
} finally {
2023-09-19 06:10:20 -04:00
let res = core . ops . op _test _op _sanitizer _finish (
desc . id ,
false ,
opIdHostRecvMessage ,
opIdHostRecvCtrl ,
) ;
if ( res === 1 || res === 2 ) {
await opSanitizerDelay ( res === 2 ) ;
res = core . ops . op _test _op _sanitizer _finish (
desc . id ,
true ,
opIdHostRecvMessage ,
opIdHostRecvCtrl ,
) ;
}
postTraces = new Map ( core . opCallTraces ) ;
if ( res === 3 ) {
report = core . ops . op _test _op _sanitizer _report ( desc . id ) ;
2023-09-16 01:48:31 -04:00
}
2023-02-07 14:22:46 -05:00
}
2021-10-11 11:00:33 -04:00
2023-09-19 06:10:20 -04:00
if ( report === null ) return null ;
2022-02-16 13:53:17 -05:00
2023-02-07 14:22:46 -05:00
const details = [ ] ;
2023-09-19 06:10:20 -04:00
for ( const opReport of report ) {
const opName = opNames [ opReport . id ] ;
const diff = opReport . diff ;
2023-02-07 14:22:46 -05:00
2023-09-19 06:10:20 -04:00
if ( diff > 0 ) {
const [ name , hint ] = OP _DETAILS [ opName ] || [ opName , null ] ;
const count = diff ;
2023-02-07 14:22:46 -05:00
let message = ` ${ count } async operation ${
count === 1 ? "" : "s"
} to $ { name } $ {
count === 1 ? "was" : "were"
} started in this test , but never completed . ` ;
if ( hint ) {
message += ` This is often caused by not ${ hint } . ` ;
2023-01-16 11:17:18 -05:00
}
2023-02-07 14:22:46 -05:00
const traces = [ ] ;
2023-09-19 06:10:20 -04:00
for ( const [ id , { opName : traceOpName , stack } ] of postTraces ) {
if ( traceOpName !== opName ) continue ;
2023-02-07 14:22:46 -05:00
if ( MapPrototypeHas ( preTraces , id ) ) continue ;
ArrayPrototypePush ( traces , stack ) ;
}
if ( traces . length === 1 ) {
message += " The operation was started here:\n" ;
message += traces [ 0 ] ;
} else if ( traces . length > 1 ) {
message += " The operations were started here:\n" ;
message += ArrayPrototypeJoin ( traces , "\n\n" ) ;
}
ArrayPrototypePush ( details , message ) ;
2023-09-19 06:10:20 -04:00
} else if ( diff < 0 ) {
const [ name , hint ] = OP _DETAILS [ opName ] || [ opName , null ] ;
const count = - diff ;
2023-09-14 10:38:15 -04:00
let message = ` ${ count } async operation ${
count === 1 ? "" : "s"
} to $ { name } $ {
count === 1 ? "was" : "were"
} started before this test , but $ {
count === 1 ? "was" : "were"
} completed during the test . Async operations should not complete in a test if they were not started in that test . ` ;
if ( hint ) {
message += ` This is often caused by not ${ hint } . ` ;
}
const traces = [ ] ;
2023-09-19 06:10:20 -04:00
for ( const [ id , { opName : traceOpName , stack } ] of preTraces ) {
if ( opName !== traceOpName ) continue ;
2023-09-14 10:38:15 -04:00
if ( MapPrototypeHas ( postTraces , id ) ) continue ;
ArrayPrototypePush ( traces , stack ) ;
}
if ( traces . length === 1 ) {
message += " The operation was started here:\n" ;
message += traces [ 0 ] ;
} else if ( traces . length > 1 ) {
message += " The operations were started here:\n" ;
message += ArrayPrototypeJoin ( traces , "\n\n" ) ;
}
ArrayPrototypePush ( details , message ) ;
2023-09-19 06:10:20 -04:00
} else {
throw new Error ( "unreachable" ) ;
2021-10-11 11:00:33 -04:00
}
2023-02-07 14:22:46 -05:00
}
2023-09-16 01:48:31 -04:00
2023-03-25 15:32:11 -04:00
return { failed : { leakedOps : [ details , core . isOpCallTracingEnabled ( ) ] } } ;
2023-02-07 14:22:46 -05:00
} ;
}
function prettyResourceNames ( name ) {
switch ( name ) {
case "fsFile" :
return [ "A file" , "opened" , "closed" ] ;
case "fetchRequest" :
return [ "A fetch request" , "started" , "finished" ] ;
case "fetchRequestBody" :
return [ "A fetch request body" , "created" , "closed" ] ;
2023-06-15 09:34:21 -04:00
case "fetchResponse" :
2023-02-07 14:22:46 -05:00
return [ "A fetch response body" , "created" , "consumed" ] ;
case "httpClient" :
return [ "An HTTP client" , "created" , "closed" ] ;
case "dynamicLibrary" :
return [ "A dynamic library" , "loaded" , "unloaded" ] ;
case "httpConn" :
return [ "An inbound HTTP connection" , "accepted" , "closed" ] ;
case "httpStream" :
return [ "An inbound HTTP request" , "accepted" , "closed" ] ;
case "tcpStream" :
return [ "A TCP connection" , "opened/accepted" , "closed" ] ;
case "unixStream" :
return [ "A Unix connection" , "opened/accepted" , "closed" ] ;
case "tlsStream" :
return [ "A TLS connection" , "opened/accepted" , "closed" ] ;
case "tlsListener" :
return [ "A TLS listener" , "opened" , "closed" ] ;
case "unixListener" :
return [ "A Unix listener" , "opened" , "closed" ] ;
case "unixDatagram" :
return [ "A Unix datagram" , "opened" , "closed" ] ;
case "tcpListener" :
return [ "A TCP listener" , "opened" , "closed" ] ;
case "udpSocket" :
return [ "A UDP socket" , "opened" , "closed" ] ;
case "timer" :
return [ "A timer" , "started" , "fired/cleared" ] ;
case "textDecoder" :
return [ "A text decoder" , "created" , "finished" ] ;
case "messagePort" :
return [ "A message port" , "created" , "closed" ] ;
case "webSocketStream" :
return [ "A WebSocket" , "opened" , "closed" ] ;
case "fsEvents" :
return [ "A file system watcher" , "created" , "closed" ] ;
case "childStdin" :
return [ "A child process stdin" , "opened" , "closed" ] ;
case "childStdout" :
return [ "A child process stdout" , "opened" , "closed" ] ;
case "childStderr" :
return [ "A child process stderr" , "opened" , "closed" ] ;
case "child" :
return [ "A child process" , "started" , "closed" ] ;
case "signal" :
return [ "A signal listener" , "created" , "fired/cleared" ] ;
case "stdin" :
return [ "The stdin pipe" , "opened" , "closed" ] ;
case "stdout" :
return [ "The stdout pipe" , "opened" , "closed" ] ;
case "stderr" :
return [ "The stderr pipe" , "opened" , "closed" ] ;
case "compression" :
return [ "A CompressionStream" , "created" , "closed" ] ;
default :
return [ ` A " ${ name } " resource ` , "created" , "cleaned up" ] ;
}
}
function resourceCloseHint ( name ) {
switch ( name ) {
case "fsFile" :
return "Close the file handle by calling `file.close()`." ;
case "fetchRequest" :
return "Await the promise returned from `fetch()` or abort the fetch with an abort signal." ;
case "fetchRequestBody" :
return "Terminate the request body `ReadableStream` by closing or erroring it." ;
2023-06-15 09:34:21 -04:00
case "fetchResponse" :
2023-02-07 14:22:46 -05:00
return "Consume or close the response body `ReadableStream`, e.g `await resp.text()` or `await resp.body.cancel()`." ;
case "httpClient" :
return "Close the HTTP client by calling `httpClient.close()`." ;
case "dynamicLibrary" :
return "Unload the dynamic library by calling `dynamicLibrary.close()`." ;
case "httpConn" :
return "Close the inbound HTTP connection by calling `httpConn.close()`." ;
case "httpStream" :
return "Close the inbound HTTP request by responding with `e.respondWith()` or closing the HTTP connection." ;
case "tcpStream" :
return "Close the TCP connection by calling `tcpConn.close()`." ;
case "unixStream" :
return "Close the Unix socket connection by calling `unixConn.close()`." ;
case "tlsStream" :
return "Close the TLS connection by calling `tlsConn.close()`." ;
case "tlsListener" :
return "Close the TLS listener by calling `tlsListener.close()`." ;
case "unixListener" :
return "Close the Unix socket listener by calling `unixListener.close()`." ;
case "unixDatagram" :
return "Close the Unix datagram socket by calling `unixDatagram.close()`." ;
case "tcpListener" :
return "Close the TCP listener by calling `tcpListener.close()`." ;
case "udpSocket" :
return "Close the UDP socket by calling `udpSocket.close()`." ;
case "timer" :
return "Clear the timer by calling `clearInterval` or `clearTimeout`." ;
case "textDecoder" :
return "Close the text decoder by calling `textDecoder.decode('')` or `await textDecoderStream.readable.cancel()`." ;
case "messagePort" :
return "Close the message port by calling `messagePort.close()`." ;
case "webSocketStream" :
return "Close the WebSocket by calling `webSocket.close()`." ;
case "fsEvents" :
return "Close the file system watcher by calling `watcher.close()`." ;
case "childStdin" :
return "Close the child process stdin by calling `proc.stdin.close()`." ;
case "childStdout" :
return "Close the child process stdout by calling `proc.stdout.close()` or `await child.stdout.cancel()`." ;
case "childStderr" :
return "Close the child process stderr by calling `proc.stderr.close()` or `await child.stderr.cancel()`." ;
case "child" :
return "Close the child process by calling `proc.kill()` or `proc.close()`." ;
case "signal" :
return "Clear the signal listener by calling `Deno.removeSignalListener`." ;
case "stdin" :
return "Close the stdin pipe by calling `Deno.stdin.close()`." ;
case "stdout" :
return "Close the stdout pipe by calling `Deno.stdout.close()`." ;
case "stderr" :
return "Close the stderr pipe by calling `Deno.stderr.close()`." ;
case "compression" :
return "Close the compression stream by calling `await stream.writable.close()`." ;
default :
return "Close the resource before the end of the test." ;
2022-01-25 11:03:38 -05:00
}
2023-02-07 14:22:46 -05:00
}
2022-01-25 11:03:38 -05:00
2023-02-07 14:22:46 -05:00
// Wrap test function in additional assertion that makes sure
// the test case does not "leak" resources - ie. resource table after
// the test has exactly the same contents as before the test.
function assertResources ( fn ) {
/** @param desc {TestDescription | TestStepDescription} */
return async function resourceSanitizer ( desc ) {
const pre = core . resources ( ) ;
2023-03-25 15:32:11 -04:00
const innerResult = await fn ( desc ) ;
if ( innerResult ) return innerResult ;
2023-02-07 14:22:46 -05:00
const post = core . resources ( ) ;
const allResources = new Set ( [
... new SafeArrayIterator ( ObjectKeys ( pre ) ) ,
... new SafeArrayIterator ( ObjectKeys ( post ) ) ,
] ) ;
const details = [ ] ;
for ( const resource of allResources ) {
const preResource = pre [ resource ] ;
const postResource = post [ resource ] ;
if ( preResource === postResource ) continue ;
if ( preResource === undefined ) {
const [ name , action1 , action2 ] = prettyResourceNames ( postResource ) ;
const hint = resourceCloseHint ( postResource ) ;
const detail =
` ${ name } (rid ${ resource } ) was ${ action1 } during the test, but not ${ action2 } during the test. ${ hint } ` ;
ArrayPrototypePush ( details , detail ) ;
} else {
const [ name , action1 , action2 ] = prettyResourceNames ( preResource ) ;
const detail =
` ${ name } (rid ${ resource } ) was ${ action1 } before the test started, but was ${ action2 } during the test. Do not close resources in a test that were not created during that test. ` ;
ArrayPrototypePush ( details , detail ) ;
2022-01-25 11:03:38 -05:00
}
2023-02-07 14:22:46 -05:00
}
2023-03-25 15:32:11 -04:00
if ( details . length == 0 ) {
return null ;
}
return { failed : { leakedResources : details } } ;
2023-02-07 14:22:46 -05:00
} ;
}
// Wrap test function in additional assertion that makes sure
// that the test case does not accidentally exit prematurely.
function assertExit ( fn , isTest ) {
return async function exitSanitizer ( ... params ) {
setExitHandler ( ( exitCode ) => {
assert (
false ,
` ${
isTest ? "Test case" : "Bench"
} attempted to exit with exit code : $ { exitCode } ` ,
) ;
} ) ;
2020-07-19 13:49:44 -04:00
2023-02-07 14:22:46 -05:00
try {
2023-03-25 15:32:11 -04:00
const innerResult = await fn ( ... new SafeArrayIterator ( params ) ) ;
if ( innerResult ) return innerResult ;
2023-02-07 14:22:46 -05:00
} finally {
setExitHandler ( null ) ;
}
} ;
}
2021-02-24 07:55:50 -05:00
2023-04-13 13:43:23 -04:00
function wrapOuter ( fn , desc ) {
return async function outerWrapped ( ) {
try {
if ( desc . ignore ) {
return "ignored" ;
}
return await fn ( desc ) ? ? "ok" ;
} catch ( error ) {
return { failed : { jsError : core . destructureError ( error ) } } ;
} finally {
const state = MapPrototypeGet ( testStates , desc . id ) ;
for ( const childDesc of state . children ) {
stepReportResult ( childDesc , { failed : "incomplete" } , 0 ) ;
}
state . completed = true ;
}
} ;
}
function wrapInner ( fn ) {
2023-02-07 14:22:46 -05:00
/** @param desc {TestDescription | TestStepDescription} */
2023-04-13 13:43:23 -04:00
return async function innerWrapped ( desc ) {
2023-03-25 15:32:11 -04:00
function getRunningStepDescs ( ) {
const results = [ ] ;
let childDesc = desc ;
while ( childDesc . parent != null ) {
const state = MapPrototypeGet ( testStates , childDesc . parent . id ) ;
for ( const siblingDesc of state . children ) {
if ( siblingDesc . id == childDesc . id ) {
continue ;
}
const siblingState = MapPrototypeGet ( testStates , siblingDesc . id ) ;
if ( ! siblingState . completed ) {
ArrayPrototypePush ( results , siblingDesc ) ;
2023-02-07 14:22:46 -05:00
}
}
2023-03-25 15:32:11 -04:00
childDesc = childDesc . parent ;
2021-10-11 09:45:02 -04:00
}
2023-03-25 15:32:11 -04:00
return results ;
2023-02-07 14:22:46 -05:00
}
2023-03-25 15:32:11 -04:00
const runningStepDescs = getRunningStepDescs ( ) ;
const runningStepDescsWithSanitizers = ArrayPrototypeFilter (
runningStepDescs ,
( d ) => usesSanitizer ( d ) ,
) ;
2023-02-07 14:22:46 -05:00
2023-03-25 15:32:11 -04:00
if ( runningStepDescsWithSanitizers . length > 0 ) {
return {
failed : {
overlapsWithSanitizers : runningStepDescsWithSanitizers . map (
getFullName ,
) ,
} ,
} ;
2022-07-15 13:09:22 -04:00
}
2021-10-11 09:45:02 -04:00
2023-03-25 15:32:11 -04:00
if ( usesSanitizer ( desc ) && runningStepDescs . length > 0 ) {
return {
failed : { hasSanitizersAndOverlaps : runningStepDescs . map ( getFullName ) } ,
} ;
2023-02-07 14:22:46 -05:00
}
2023-03-25 15:32:11 -04:00
await fn ( MapPrototypeGet ( testStates , desc . id ) . context ) ;
2023-04-13 13:43:23 -04:00
let failedSteps = 0 ;
2023-03-25 15:32:11 -04:00
for ( const childDesc of MapPrototypeGet ( testStates , desc . id ) . children ) {
2023-04-13 13:43:23 -04:00
const state = MapPrototypeGet ( testStates , childDesc . id ) ;
if ( ! state . completed ) {
2023-03-25 15:32:11 -04:00
return { failed : "incompleteSteps" } ;
}
2023-04-13 13:43:23 -04:00
if ( state . failed ) {
failedSteps ++ ;
}
2023-03-25 15:32:11 -04:00
}
2023-04-13 13:43:23 -04:00
return failedSteps == 0 ? null : { failed : { failedSteps } } ;
2023-03-25 15:32:11 -04:00
} ;
2023-02-07 14:22:46 -05:00
}
2021-07-13 18:11:02 -04:00
2023-02-07 14:22:46 -05:00
function pledgePermissions ( permissions ) {
return ops . op _pledge _test _permissions (
serializePermissions ( permissions ) ,
) ;
}
2021-07-13 18:11:02 -04:00
2023-02-07 14:22:46 -05:00
function restorePermissions ( token ) {
ops . op _restore _test _permissions ( token ) ;
}
2021-07-13 18:11:02 -04:00
2023-02-07 14:22:46 -05:00
function withPermissions ( fn , permissions ) {
return async function applyPermissions ( ... params ) {
const token = pledgePermissions ( permissions ) ;
2021-07-13 18:11:02 -04:00
2023-02-07 14:22:46 -05:00
try {
2023-04-01 16:20:16 -04:00
return await fn ( ... new SafeArrayIterator ( params ) ) ;
2023-02-07 14:22:46 -05:00
} finally {
restorePermissions ( token ) ;
2022-06-24 06:00:53 -04:00
}
2023-02-07 14:22:46 -05:00
} ;
}
2023-08-09 19:40:34 -04:00
const ESCAPE _ASCII _CHARS = [
[ "\b" , "\\b" ] ,
[ "\f" , "\\f" ] ,
[ "\t" , "\\t" ] ,
[ "\n" , "\\n" ] ,
[ "\r" , "\\r" ] ,
[ "\v" , "\\v" ] ,
] ;
2023-09-10 08:07:00 -04:00
/ * *
* @ param { string } name
* @ returns { string }
* /
2023-08-09 19:40:34 -04:00
function escapeName ( name ) {
2023-09-10 08:07:00 -04:00
// Check if we need to escape a character
for ( let i = 0 ; i < name . length ; i ++ ) {
const ch = name . charCodeAt ( i ) ;
if ( ch <= 13 && ch >= 8 ) {
// Slow path: We do need to escape it
for ( const [ escape , replaceWith ] of ESCAPE _ASCII _CHARS ) {
name = StringPrototypeReplaceAll ( name , escape , replaceWith ) ;
}
return name ;
}
2023-08-09 19:40:34 -04:00
}
2023-09-10 08:07:00 -04:00
// We didn't need to escape anything, return original string
2023-08-09 19:40:34 -04:00
return name ;
}
2023-09-10 08:07:00 -04:00
2023-02-07 14:22:46 -05:00
/ * *
* @ typedef { {
* id : number ,
* name : string ,
* fn : TestFunction
* origin : string ,
* location : TestLocation ,
* ignore : boolean ,
* only : boolean .
* sanitizeOps : boolean ,
* sanitizeResources : boolean ,
* sanitizeExit : boolean ,
* permissions : PermissionOptions ,
* } } TestDescription
*
* @ typedef { {
* id : number ,
* name : string ,
* fn : TestFunction
* origin : string ,
* location : TestLocation ,
* ignore : boolean ,
* level : number ,
* parent : TestDescription | TestStepDescription ,
* rootId : number ,
* rootName : String ,
* sanitizeOps : boolean ,
* sanitizeResources : boolean ,
* sanitizeExit : boolean ,
* } } TestStepDescription
*
* @ typedef { {
* context : TestContext ,
* children : TestStepDescription [ ] ,
2023-03-25 15:32:11 -04:00
* completed : boolean ,
2023-02-07 14:22:46 -05:00
* } } TestState
*
* @ typedef { {
* context : TestContext ,
* children : TestStepDescription [ ] ,
2023-03-25 15:32:11 -04:00
* completed : boolean ,
* failed : boolean ,
2023-02-07 14:22:46 -05:00
* } } TestStepState
*
* @ typedef { {
* id : number ,
* name : string ,
* fn : BenchFunction
* origin : string ,
* ignore : boolean ,
* only : boolean .
* sanitizeExit : boolean ,
* permissions : PermissionOptions ,
* } } BenchDescription
* /
/** @type {Map<number, TestState | TestStepState>} */
const testStates = new Map ( ) ;
2023-07-31 06:02:59 -04:00
/** @type {number | null} */
let currentBenchId = null ;
// These local variables are used to track time measurements at
// `BenchContext::{start,end}` calls. They are global instead of using a state
// map to minimise the overhead of assigning them.
/** @type {number | null} */
let currentBenchUserExplicitStart = null ;
/** @type {number | null} */
let currentBenchUserExplicitEnd = null ;
2023-02-07 14:22:46 -05:00
2023-09-25 22:21:18 -04:00
const registerTestIdRetBuf = new Uint32Array ( 1 ) ;
const registerTestIdRetBufU8 = new Uint8Array ( registerTestIdRetBuf . buffer ) ;
2023-09-06 08:17:33 -04:00
function testInner (
2023-02-07 14:22:46 -05:00
nameOrFnOrOptions ,
optionsOrFn ,
maybeFn ,
2023-09-06 08:17:33 -04:00
overrides = { } ,
2023-02-07 14:22:46 -05:00
) {
2023-04-13 13:43:23 -04:00
if ( typeof ops . op _register _test != "function" ) {
2023-02-07 14:22:46 -05:00
return ;
}
2022-06-24 06:00:53 -04:00
2023-02-07 14:22:46 -05:00
let testDesc ;
const defaults = {
ignore : false ,
only : false ,
sanitizeOps : true ,
sanitizeResources : true ,
sanitizeExit : true ,
permissions : null ,
} ;
2020-07-19 13:49:44 -04:00
2023-02-07 14:22:46 -05:00
if ( typeof nameOrFnOrOptions === "string" ) {
if ( ! nameOrFnOrOptions ) {
throw new TypeError ( "The test name can't be empty" ) ;
}
if ( typeof optionsOrFn === "function" ) {
testDesc = { fn : optionsOrFn , name : nameOrFnOrOptions , ... defaults } ;
} else {
if ( ! maybeFn || typeof maybeFn !== "function" ) {
throw new TypeError ( "Missing test function" ) ;
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
2021-11-23 08:57:51 -05:00
}
2023-02-07 14:22:46 -05:00
if ( optionsOrFn . fn != undefined ) {
throw new TypeError (
"Unexpected 'fn' field in options, test function is already provided as the third argument." ,
) ;
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
2021-11-23 08:57:51 -05:00
}
2023-02-07 14:22:46 -05:00
if ( optionsOrFn . name != undefined ) {
throw new TypeError (
"Unexpected 'name' field in options, test name is already provided as the first argument." ,
) ;
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
2021-11-23 08:57:51 -05:00
}
2022-07-15 13:09:22 -04:00
testDesc = {
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
2021-11-23 08:57:51 -05:00
... defaults ,
2023-02-07 14:22:46 -05:00
... optionsOrFn ,
fn : maybeFn ,
name : nameOrFnOrOptions ,
feat(test): Add more overloads for "Deno.test" (#12749)
This commit adds 4 more overloads to "Deno.test()" API.
```
// Deno.test(function testName() { });
export function test(fn: (t: TestContext) => void | Promise<void>): void;
// Deno.test("test name", { only: true }, function() { });
export function test(
name: string,
options: Omit<TestDefinition, "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ name: "test name" }, function() { });
export function test(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void;
// Deno.test({ only: true }, function testName() { });
export function test(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void;
```
2021-11-23 08:57:51 -05:00
} ;
2023-02-07 14:22:46 -05:00
}
} else if ( typeof nameOrFnOrOptions === "function" ) {
if ( ! nameOrFnOrOptions . name ) {
throw new TypeError ( "The test function must have a name" ) ;
}
if ( optionsOrFn != undefined ) {
throw new TypeError ( "Unexpected second argument to Deno.test()" ) ;
}
if ( maybeFn != undefined ) {
throw new TypeError ( "Unexpected third argument to Deno.test()" ) ;
}
testDesc = {
... defaults ,
fn : nameOrFnOrOptions ,
name : nameOrFnOrOptions . name ,
} ;
} else {
let fn ;
let name ;
if ( typeof optionsOrFn === "function" ) {
fn = optionsOrFn ;
if ( nameOrFnOrOptions . fn != undefined ) {
throw new TypeError (
"Unexpected 'fn' field in options, test function is already provided as the second argument." ,
) ;
2020-07-19 13:49:44 -04:00
}
2023-02-07 14:22:46 -05:00
name = nameOrFnOrOptions . name ? ? fn . name ;
} else {
if (
! nameOrFnOrOptions . fn || typeof nameOrFnOrOptions . fn !== "function"
) {
throw new TypeError (
"Expected 'fn' field in the first argument to be a test function." ,
) ;
2020-07-19 13:49:44 -04:00
}
2023-02-07 14:22:46 -05:00
fn = nameOrFnOrOptions . fn ;
name = nameOrFnOrOptions . name ? ? fn . name ;
2020-07-19 13:49:44 -04:00
}
2023-02-07 14:22:46 -05:00
if ( ! name ) {
throw new TypeError ( "The test name can't be empty" ) ;
2021-07-13 18:11:02 -04:00
}
2023-02-07 14:22:46 -05:00
testDesc = { ... defaults , ... nameOrFnOrOptions , fn , name } ;
}
2022-07-15 13:09:22 -04:00
2023-09-06 08:17:33 -04:00
testDesc = { ... testDesc , ... overrides } ;
2023-02-07 14:22:46 -05:00
// Delete this prop in case the user passed it. It's used to detect steps.
delete testDesc . parent ;
2023-08-02 11:11:04 -04:00
2023-09-25 21:01:56 -04:00
testDesc . location = core . currentUserCallSite ( ) ;
2023-04-13 13:43:23 -04:00
testDesc . fn = wrapTest ( testDesc ) ;
2023-08-09 19:40:34 -04:00
testDesc . name = escapeName ( testDesc . name ) ;
2020-07-19 13:49:44 -04:00
2023-09-25 22:21:18 -04:00
const origin = ops . op _register _test (
testDesc . fn ,
testDesc . name ,
testDesc . ignore ,
testDesc . only ,
testDesc . location . fileName ,
testDesc . location . lineNumber ,
testDesc . location . columnNumber ,
registerTestIdRetBufU8 ,
) ;
testDesc . id = registerTestIdRetBuf [ 0 ] ;
2023-04-13 13:43:23 -04:00
testDesc . origin = origin ;
2023-02-07 14:22:46 -05:00
MapPrototypeSet ( testStates , testDesc . id , {
context : createTestContext ( testDesc ) ,
children : [ ] ,
2023-03-25 15:32:11 -04:00
completed : false ,
2023-02-07 14:22:46 -05:00
} ) ;
}
2023-09-06 08:17:33 -04:00
// Main test function provided by Deno.
function test (
nameOrFnOrOptions ,
optionsOrFn ,
maybeFn ,
) {
return testInner ( nameOrFnOrOptions , optionsOrFn , maybeFn ) ;
}
test . ignore = function ( nameOrFnOrOptions , optionsOrFn , maybeFn ) {
return testInner ( nameOrFnOrOptions , optionsOrFn , maybeFn , { ignore : true } ) ;
} ;
test . only = function (
nameOrFnOrOptions ,
optionsOrFn ,
maybeFn ,
) {
return testInner ( nameOrFnOrOptions , optionsOrFn , maybeFn , { only : true } ) ;
} ;
2023-07-17 17:17:28 -04:00
let registeredWarmupBench = false ;
2023-02-07 14:22:46 -05:00
// Main bench function provided by Deno.
function bench (
nameOrFnOrOptions ,
optionsOrFn ,
maybeFn ,
) {
2023-04-13 13:43:23 -04:00
if ( typeof ops . op _register _bench != "function" ) {
2023-02-07 14:22:46 -05:00
return ;
}
2022-06-24 06:00:53 -04:00
2023-07-17 17:17:28 -04:00
if ( ! registeredWarmupBench ) {
registeredWarmupBench = true ;
const warmupBenchDesc = {
name : "<warmup>" ,
fn : function warmup ( ) { } ,
async : false ,
ignore : false ,
baseline : false ,
only : false ,
sanitizeExit : true ,
permissions : null ,
warmup : true ,
} ;
warmupBenchDesc . fn = wrapBenchmark ( warmupBenchDesc ) ;
const { id , origin } = ops . op _register _bench ( warmupBenchDesc ) ;
warmupBenchDesc . id = id ;
warmupBenchDesc . origin = origin ;
}
2023-02-07 14:22:46 -05:00
let benchDesc ;
const defaults = {
ignore : false ,
baseline : false ,
only : false ,
sanitizeExit : true ,
permissions : null ,
} ;
2022-03-11 17:07:02 -05:00
2023-02-07 14:22:46 -05:00
if ( typeof nameOrFnOrOptions === "string" ) {
if ( ! nameOrFnOrOptions ) {
throw new TypeError ( "The bench name can't be empty" ) ;
}
if ( typeof optionsOrFn === "function" ) {
benchDesc = { fn : optionsOrFn , name : nameOrFnOrOptions , ... defaults } ;
} else {
if ( ! maybeFn || typeof maybeFn !== "function" ) {
throw new TypeError ( "Missing bench function" ) ;
2022-03-11 17:07:02 -05:00
}
2023-02-07 14:22:46 -05:00
if ( optionsOrFn . fn != undefined ) {
throw new TypeError (
"Unexpected 'fn' field in options, bench function is already provided as the third argument." ,
) ;
2022-03-11 17:07:02 -05:00
}
2023-02-07 14:22:46 -05:00
if ( optionsOrFn . name != undefined ) {
throw new TypeError (
"Unexpected 'name' field in options, bench name is already provided as the first argument." ,
) ;
2022-03-11 17:07:02 -05:00
}
2022-05-30 13:58:44 -04:00
benchDesc = {
2022-03-11 17:07:02 -05:00
... defaults ,
2023-02-07 14:22:46 -05:00
... optionsOrFn ,
fn : maybeFn ,
name : nameOrFnOrOptions ,
2022-03-11 17:07:02 -05:00
} ;
2023-02-07 14:22:46 -05:00
}
} else if ( typeof nameOrFnOrOptions === "function" ) {
if ( ! nameOrFnOrOptions . name ) {
throw new TypeError ( "The bench function must have a name" ) ;
}
if ( optionsOrFn != undefined ) {
throw new TypeError ( "Unexpected second argument to Deno.bench()" ) ;
}
if ( maybeFn != undefined ) {
throw new TypeError ( "Unexpected third argument to Deno.bench()" ) ;
}
benchDesc = {
... defaults ,
fn : nameOrFnOrOptions ,
name : nameOrFnOrOptions . name ,
} ;
} else {
let fn ;
let name ;
if ( typeof optionsOrFn === "function" ) {
fn = optionsOrFn ;
if ( nameOrFnOrOptions . fn != undefined ) {
throw new TypeError (
"Unexpected 'fn' field in options, bench function is already provided as the second argument." ,
) ;
2022-03-11 17:07:02 -05:00
}
2023-02-07 14:22:46 -05:00
name = nameOrFnOrOptions . name ? ? fn . name ;
} else {
if (
! nameOrFnOrOptions . fn || typeof nameOrFnOrOptions . fn !== "function"
) {
throw new TypeError (
"Expected 'fn' field in the first argument to be a bench function." ,
) ;
2022-03-11 17:07:02 -05:00
}
2023-02-07 14:22:46 -05:00
fn = nameOrFnOrOptions . fn ;
name = nameOrFnOrOptions . name ? ? fn . name ;
2022-03-11 17:07:02 -05:00
}
2023-02-07 14:22:46 -05:00
if ( ! name ) {
throw new TypeError ( "The bench name can't be empty" ) ;
}
benchDesc = { ... defaults , ... nameOrFnOrOptions , fn , name } ;
}
2022-03-11 17:07:02 -05:00
2023-02-07 14:22:46 -05:00
const AsyncFunction = ( async ( ) => { } ) . constructor ;
benchDesc . async = AsyncFunction === benchDesc . fn . constructor ;
2023-04-13 13:43:23 -04:00
benchDesc . fn = wrapBenchmark ( benchDesc ) ;
2023-07-17 17:17:28 -04:00
benchDesc . warmup = false ;
2023-08-09 19:40:34 -04:00
benchDesc . name = escapeName ( benchDesc . name ) ;
2022-04-20 15:06:39 -04:00
2023-04-13 13:43:23 -04:00
const { id , origin } = ops . op _register _bench ( benchDesc ) ;
2023-02-07 14:22:46 -05:00
benchDesc . id = id ;
2023-04-13 13:43:23 -04:00
benchDesc . origin = origin ;
2023-02-07 14:22:46 -05:00
}
function compareMeasurements ( a , b ) {
if ( a > b ) return 1 ;
if ( a < b ) return - 1 ;
return 0 ;
}
fix(bench): explicit timers don't force high precision measurements (#20272)
Disables `BenchContext::start()` and `BenchContext::end()` for low
precision benchmarks (less than 0.01s per iteration). Prints a warning
when they are used in such benchmarks, suggesting to remove them.
```ts
Deno.bench("noop", { group: "noops" }, () => {});
Deno.bench("noop with start/end", { group: "noops" }, (b) => {
b.start();
b.end();
});
```
Before:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.2 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 2.63 ns/iter 380,674,131.4 (2.45 ns … 27.78 ns) 2.55 ns 4.03 ns 5.33 ns
noop with start and end 302.47 ns/iter 3,306,146.0 (200 ns … 151.2 µs) 300 ns 400 ns 400 ns
summary
noop
115.14x faster than noop with start and end
```
After:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 3.01 ns/iter 332,565,561.7 (2.73 ns … 29.54 ns) 2.93 ns 5.29 ns 7.45 ns
noop with start and end 7.73 ns/iter 129,291,091.5 (6.61 ns … 46.76 ns) 7.87 ns 13.12 ns 15.32 ns
Warning start() and end() calls in "noop with start and end" are ignored because it averages less than 0.01s per iteration. Remove them for better results.
summary
noop
2.57x faster than noop with start and end
```
2023-08-26 05:29:45 -04:00
function benchStats ( n , highPrecision , usedExplicitTimers , avg , min , max , all ) {
2023-02-07 14:22:46 -05:00
return {
n ,
min ,
max ,
p75 : all [ MathCeil ( n * ( 75 / 100 ) ) - 1 ] ,
p99 : all [ MathCeil ( n * ( 99 / 100 ) ) - 1 ] ,
p995 : all [ MathCeil ( n * ( 99.5 / 100 ) ) - 1 ] ,
p999 : all [ MathCeil ( n * ( 99.9 / 100 ) ) - 1 ] ,
avg : ! highPrecision ? ( avg / n ) : MathCeil ( avg / n ) ,
fix(bench): explicit timers don't force high precision measurements (#20272)
Disables `BenchContext::start()` and `BenchContext::end()` for low
precision benchmarks (less than 0.01s per iteration). Prints a warning
when they are used in such benchmarks, suggesting to remove them.
```ts
Deno.bench("noop", { group: "noops" }, () => {});
Deno.bench("noop with start/end", { group: "noops" }, (b) => {
b.start();
b.end();
});
```
Before:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.2 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 2.63 ns/iter 380,674,131.4 (2.45 ns … 27.78 ns) 2.55 ns 4.03 ns 5.33 ns
noop with start and end 302.47 ns/iter 3,306,146.0 (200 ns … 151.2 µs) 300 ns 400 ns 400 ns
summary
noop
115.14x faster than noop with start and end
```
After:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 3.01 ns/iter 332,565,561.7 (2.73 ns … 29.54 ns) 2.93 ns 5.29 ns 7.45 ns
noop with start and end 7.73 ns/iter 129,291,091.5 (6.61 ns … 46.76 ns) 7.87 ns 13.12 ns 15.32 ns
Warning start() and end() calls in "noop with start and end" are ignored because it averages less than 0.01s per iteration. Remove them for better results.
summary
noop
2.57x faster than noop with start and end
```
2023-08-26 05:29:45 -04:00
highPrecision ,
usedExplicitTimers ,
2023-02-07 14:22:46 -05:00
} ;
}
2023-07-31 06:02:59 -04:00
async function benchMeasure ( timeBudget , fn , async , context ) {
2023-02-07 14:22:46 -05:00
let n = 0 ;
let avg = 0 ;
let wavg = 0 ;
2023-07-31 06:02:59 -04:00
let usedExplicitTimers = false ;
2023-02-07 14:22:46 -05:00
const all = [ ] ;
let min = Infinity ;
let max = - Infinity ;
const lowPrecisionThresholdInNs = 1e4 ;
// warmup step
let c = 0 ;
let iterations = 20 ;
let budget = 10 * 1e6 ;
2023-04-13 13:43:23 -04:00
if ( ! async ) {
2023-02-07 14:22:46 -05:00
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
fn ( context ) ;
const t2 = benchNow ( ) ;
const totalTime = t2 - t1 ;
let measuredTime = totalTime ;
if ( currentBenchUserExplicitStart !== null ) {
measuredTime -= currentBenchUserExplicitStart - t1 ;
currentBenchUserExplicitStart = null ;
usedExplicitTimers = true ;
}
if ( currentBenchUserExplicitEnd !== null ) {
measuredTime -= t2 - currentBenchUserExplicitEnd ;
currentBenchUserExplicitEnd = null ;
usedExplicitTimers = true ;
}
2023-02-07 14:22:46 -05:00
c ++ ;
2023-07-31 06:02:59 -04:00
wavg += measuredTime ;
budget -= totalTime ;
2023-02-07 14:22:46 -05:00
}
} else {
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
await fn ( context ) ;
const t2 = benchNow ( ) ;
const totalTime = t2 - t1 ;
let measuredTime = totalTime ;
if ( currentBenchUserExplicitStart !== null ) {
measuredTime -= currentBenchUserExplicitStart - t1 ;
currentBenchUserExplicitStart = null ;
usedExplicitTimers = true ;
}
if ( currentBenchUserExplicitEnd !== null ) {
measuredTime -= t2 - currentBenchUserExplicitEnd ;
currentBenchUserExplicitEnd = null ;
usedExplicitTimers = true ;
}
2022-04-20 15:06:39 -04:00
2023-02-07 14:22:46 -05:00
c ++ ;
2023-07-31 06:02:59 -04:00
wavg += measuredTime ;
budget -= totalTime ;
2023-02-07 14:22:46 -05:00
}
2022-04-20 15:06:39 -04:00
}
2023-02-07 14:22:46 -05:00
wavg /= c ;
2022-04-20 15:06:39 -04:00
2023-02-07 14:22:46 -05:00
// measure step
fix(bench): explicit timers don't force high precision measurements (#20272)
Disables `BenchContext::start()` and `BenchContext::end()` for low
precision benchmarks (less than 0.01s per iteration). Prints a warning
when they are used in such benchmarks, suggesting to remove them.
```ts
Deno.bench("noop", { group: "noops" }, () => {});
Deno.bench("noop with start/end", { group: "noops" }, (b) => {
b.start();
b.end();
});
```
Before:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.2 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 2.63 ns/iter 380,674,131.4 (2.45 ns … 27.78 ns) 2.55 ns 4.03 ns 5.33 ns
noop with start and end 302.47 ns/iter 3,306,146.0 (200 ns … 151.2 µs) 300 ns 400 ns 400 ns
summary
noop
115.14x faster than noop with start and end
```
After:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 3.01 ns/iter 332,565,561.7 (2.73 ns … 29.54 ns) 2.93 ns 5.29 ns 7.45 ns
noop with start and end 7.73 ns/iter 129,291,091.5 (6.61 ns … 46.76 ns) 7.87 ns 13.12 ns 15.32 ns
Warning start() and end() calls in "noop with start and end" are ignored because it averages less than 0.01s per iteration. Remove them for better results.
summary
noop
2.57x faster than noop with start and end
```
2023-08-26 05:29:45 -04:00
if ( wavg > lowPrecisionThresholdInNs ) {
2023-02-07 14:22:46 -05:00
let iterations = 10 ;
let budget = timeBudget * 1e6 ;
2022-04-20 15:06:39 -04:00
2023-04-13 13:43:23 -04:00
if ( ! async ) {
2022-04-20 15:06:39 -04:00
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
fn ( context ) ;
const t2 = benchNow ( ) ;
const totalTime = t2 - t1 ;
let measuredTime = totalTime ;
if ( currentBenchUserExplicitStart !== null ) {
measuredTime -= currentBenchUserExplicitStart - t1 ;
currentBenchUserExplicitStart = null ;
}
if ( currentBenchUserExplicitEnd !== null ) {
measuredTime -= t2 - currentBenchUserExplicitEnd ;
currentBenchUserExplicitEnd = null ;
}
2022-04-20 15:06:39 -04:00
2023-02-07 14:22:46 -05:00
n ++ ;
2023-07-31 06:02:59 -04:00
avg += measuredTime ;
budget -= totalTime ;
ArrayPrototypePush ( all , measuredTime ) ;
if ( measuredTime < min ) min = measuredTime ;
if ( measuredTime > max ) max = measuredTime ;
2022-04-20 15:06:39 -04:00
}
} else {
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
await fn ( context ) ;
const t2 = benchNow ( ) ;
const totalTime = t2 - t1 ;
let measuredTime = totalTime ;
if ( currentBenchUserExplicitStart !== null ) {
measuredTime -= currentBenchUserExplicitStart - t1 ;
currentBenchUserExplicitStart = null ;
}
if ( currentBenchUserExplicitEnd !== null ) {
measuredTime -= t2 - currentBenchUserExplicitEnd ;
currentBenchUserExplicitEnd = null ;
}
2022-04-20 15:06:39 -04:00
2023-02-07 14:22:46 -05:00
n ++ ;
2023-07-31 06:02:59 -04:00
avg += measuredTime ;
budget -= totalTime ;
ArrayPrototypePush ( all , measuredTime ) ;
if ( measuredTime < min ) min = measuredTime ;
if ( measuredTime > max ) max = measuredTime ;
2022-04-20 15:06:39 -04:00
}
}
2023-02-07 14:22:46 -05:00
} else {
fix(bench): explicit timers don't force high precision measurements (#20272)
Disables `BenchContext::start()` and `BenchContext::end()` for low
precision benchmarks (less than 0.01s per iteration). Prints a warning
when they are used in such benchmarks, suggesting to remove them.
```ts
Deno.bench("noop", { group: "noops" }, () => {});
Deno.bench("noop with start/end", { group: "noops" }, (b) => {
b.start();
b.end();
});
```
Before:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.2 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 2.63 ns/iter 380,674,131.4 (2.45 ns … 27.78 ns) 2.55 ns 4.03 ns 5.33 ns
noop with start and end 302.47 ns/iter 3,306,146.0 (200 ns … 151.2 µs) 300 ns 400 ns 400 ns
summary
noop
115.14x faster than noop with start and end
```
After:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 3.01 ns/iter 332,565,561.7 (2.73 ns … 29.54 ns) 2.93 ns 5.29 ns 7.45 ns
noop with start and end 7.73 ns/iter 129,291,091.5 (6.61 ns … 46.76 ns) 7.87 ns 13.12 ns 15.32 ns
Warning start() and end() calls in "noop with start and end" are ignored because it averages less than 0.01s per iteration. Remove them for better results.
summary
noop
2.57x faster than noop with start and end
```
2023-08-26 05:29:45 -04:00
context . start = function start ( ) { } ;
context . end = function end ( ) { } ;
2023-02-07 14:22:46 -05:00
let iterations = 10 ;
let budget = timeBudget * 1e6 ;
2022-04-20 15:06:39 -04:00
2023-04-13 13:43:23 -04:00
if ( ! async ) {
2023-02-07 14:22:46 -05:00
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
for ( let c = 0 ; c < lowPrecisionThresholdInNs ; c ++ ) {
fn ( context ) ;
}
2023-02-07 14:22:46 -05:00
const iterationTime = ( benchNow ( ) - t1 ) / lowPrecisionThresholdInNs ;
n ++ ;
avg += iterationTime ;
ArrayPrototypePush ( all , iterationTime ) ;
if ( iterationTime < min ) min = iterationTime ;
if ( iterationTime > max ) max = iterationTime ;
budget -= iterationTime * lowPrecisionThresholdInNs ;
2022-04-20 15:06:39 -04:00
}
} else {
2023-02-07 14:22:46 -05:00
while ( budget > 0 || iterations -- > 0 ) {
const t1 = benchNow ( ) ;
2023-07-31 06:02:59 -04:00
for ( let c = 0 ; c < lowPrecisionThresholdInNs ; c ++ ) {
await fn ( context ) ;
currentBenchUserExplicitStart = null ;
currentBenchUserExplicitEnd = null ;
}
2023-02-07 14:22:46 -05:00
const iterationTime = ( benchNow ( ) - t1 ) / lowPrecisionThresholdInNs ;
n ++ ;
avg += iterationTime ;
ArrayPrototypePush ( all , iterationTime ) ;
if ( iterationTime < min ) min = iterationTime ;
if ( iterationTime > max ) max = iterationTime ;
budget -= iterationTime * lowPrecisionThresholdInNs ;
2022-04-20 15:06:39 -04:00
}
2022-03-11 17:07:02 -05:00
}
2022-04-20 15:06:39 -04:00
}
2023-02-07 14:22:46 -05:00
all . sort ( compareMeasurements ) ;
fix(bench): explicit timers don't force high precision measurements (#20272)
Disables `BenchContext::start()` and `BenchContext::end()` for low
precision benchmarks (less than 0.01s per iteration). Prints a warning
when they are used in such benchmarks, suggesting to remove them.
```ts
Deno.bench("noop", { group: "noops" }, () => {});
Deno.bench("noop with start/end", { group: "noops" }, (b) => {
b.start();
b.end();
});
```
Before:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.2 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 2.63 ns/iter 380,674,131.4 (2.45 ns … 27.78 ns) 2.55 ns 4.03 ns 5.33 ns
noop with start and end 302.47 ns/iter 3,306,146.0 (200 ns … 151.2 µs) 300 ns 400 ns 400 ns
summary
noop
115.14x faster than noop with start and end
```
After:
```
cpu: 12th Gen Intel(R) Core(TM) i9-12900K
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)
file:///home/nayeem/projects/deno/temp3.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
----------------------------------------------------------------------------- -----------------------------
noop 3.01 ns/iter 332,565,561.7 (2.73 ns … 29.54 ns) 2.93 ns 5.29 ns 7.45 ns
noop with start and end 7.73 ns/iter 129,291,091.5 (6.61 ns … 46.76 ns) 7.87 ns 13.12 ns 15.32 ns
Warning start() and end() calls in "noop with start and end" are ignored because it averages less than 0.01s per iteration. Remove them for better results.
summary
noop
2.57x faster than noop with start and end
```
2023-08-26 05:29:45 -04:00
return benchStats (
n ,
wavg > lowPrecisionThresholdInNs ,
usedExplicitTimers ,
avg ,
min ,
max ,
all ,
) ;
2023-02-07 14:22:46 -05:00
}
2022-03-11 17:07:02 -05:00
2023-07-31 06:02:59 -04:00
/** @param desc {BenchDescription} */
function createBenchContext ( desc ) {
return {
[ SymbolToStringTag ] : "BenchContext" ,
name : desc . name ,
origin : desc . origin ,
start ( ) {
if ( currentBenchId !== desc . id ) {
throw new TypeError (
"The benchmark which this context belongs to is not being executed." ,
) ;
}
if ( currentBenchUserExplicitStart != null ) {
throw new TypeError ( "BenchContext::start() has already been invoked." ) ;
}
currentBenchUserExplicitStart = benchNow ( ) ;
} ,
end ( ) {
const end = benchNow ( ) ;
if ( currentBenchId !== desc . id ) {
throw new TypeError (
"The benchmark which this context belongs to is not being executed." ,
) ;
}
if ( currentBenchUserExplicitEnd != null ) {
throw new TypeError ( "BenchContext::end() has already been invoked." ) ;
}
currentBenchUserExplicitEnd = end ;
} ,
} ;
}
2023-04-13 13:43:23 -04:00
/** Wrap a user benchmark function in one which returns a structured result. */
function wrapBenchmark ( desc ) {
const fn = desc . fn ;
return async function outerWrapped ( ) {
let token = null ;
const originalConsole = globalThis . console ;
2023-07-31 06:02:59 -04:00
currentBenchId = desc . id ;
2022-03-11 17:07:02 -05:00
2023-04-13 13:43:23 -04:00
try {
globalThis . console = new Console ( ( s ) => {
ops . op _dispatch _bench _event ( { output : s } ) ;
2023-02-07 14:22:46 -05:00
} ) ;
2022-05-30 13:58:44 -04:00
2023-04-13 13:43:23 -04:00
if ( desc . permissions ) {
token = pledgePermissions ( desc . permissions ) ;
}
2021-07-14 15:05:16 -04:00
2023-04-13 13:43:23 -04:00
if ( desc . sanitizeExit ) {
setExitHandler ( ( exitCode ) => {
assert (
false ,
` Bench attempted to exit with exit code: ${ exitCode } ` ,
) ;
} ) ;
}
2022-03-11 17:07:02 -05:00
2023-04-13 13:43:23 -04:00
const benchTimeInMs = 500 ;
2023-07-31 06:02:59 -04:00
const context = createBenchContext ( desc ) ;
const stats = await benchMeasure ( benchTimeInMs , fn , desc . async , context ) ;
2022-03-11 17:07:02 -05:00
2023-04-13 13:43:23 -04:00
return { ok : stats } ;
} catch ( error ) {
return { failed : core . destructureError ( error ) } ;
} finally {
globalThis . console = originalConsole ;
2023-07-31 06:02:59 -04:00
currentBenchId = null ;
currentBenchUserExplicitStart = null ;
currentBenchUserExplicitEnd = null ;
2023-04-13 13:43:23 -04:00
if ( bench . sanitizeExit ) setExitHandler ( null ) ;
if ( token !== null ) restorePermissions ( token ) ;
}
} ;
2023-02-07 14:22:46 -05:00
}
function benchNow ( ) {
return ops . op _bench _now ( ) ;
}
function getFullName ( desc ) {
if ( "parent" in desc ) {
2023-03-25 15:32:11 -04:00
return ` ${ getFullName ( desc . parent ) } ... ${ desc . name } ` ;
2022-07-15 13:09:22 -04:00
}
2023-02-07 14:22:46 -05:00
return desc . name ;
}
2021-10-11 09:45:02 -04:00
2023-02-07 14:22:46 -05:00
function usesSanitizer ( desc ) {
return desc . sanitizeResources || desc . sanitizeOps || desc . sanitizeExit ;
}
2021-10-11 09:45:02 -04:00
2023-03-25 15:32:11 -04:00
function stepReportResult ( desc , result , elapsed ) {
2023-02-07 14:22:46 -05:00
const state = MapPrototypeGet ( testStates , desc . id ) ;
for ( const childDesc of state . children ) {
2023-03-25 15:32:11 -04:00
stepReportResult ( childDesc , { failed : "incomplete" } , 0 ) ;
2023-02-07 14:22:46 -05:00
}
2023-09-25 22:21:18 -04:00
if ( result === "ok" ) {
ops . op _test _event _step _result _ok ( desc . id , elapsed ) ;
} else if ( result === "ignored" ) {
ops . op _test _event _step _result _ignored ( desc . id , elapsed ) ;
} else {
ops . op _test _event _step _result _failed ( desc . id , result . failed , elapsed ) ;
}
2023-02-07 14:22:46 -05:00
}
/** @param desc {TestDescription | TestStepDescription} */
function createTestContext ( desc ) {
let parent ;
let level ;
let rootId ;
let rootName ;
if ( "parent" in desc ) {
parent = MapPrototypeGet ( testStates , desc . parent . id ) . context ;
level = desc . level ;
rootId = desc . rootId ;
rootName = desc . rootName ;
} else {
parent = undefined ;
level = 0 ;
rootId = desc . id ;
rootName = desc . name ;
2021-10-11 09:45:02 -04:00
}
2023-02-07 14:22:46 -05:00
return {
[ SymbolToStringTag ] : "TestContext" ,
/ * *
* The current test name .
* /
name : desc . name ,
/ * *
* Parent test context .
* /
parent ,
/ * *
* File Uri of the test code .
* /
origin : desc . origin ,
/ * *
* @ param nameOrFnOrOptions { string | TestStepDefinition | ( ( t : TestContext ) => void | Promise < void > ) }
* @ param maybeFn { ( ( t : TestContext ) => void | Promise < void > ) | undefined }
* /
async step ( nameOrFnOrOptions , maybeFn ) {
2023-03-25 15:32:11 -04:00
if ( MapPrototypeGet ( testStates , desc . id ) . completed ) {
2023-02-07 14:22:46 -05:00
throw new Error (
"Cannot run test step after parent scope has finished execution. " +
"Ensure any `.step(...)` calls are executed before their parent scope completes execution." ,
) ;
}
2021-10-11 09:45:02 -04:00
2023-02-07 14:22:46 -05:00
let stepDesc ;
if ( typeof nameOrFnOrOptions === "string" ) {
if ( ! ( ObjectPrototypeIsPrototypeOf ( FunctionPrototype , maybeFn ) ) ) {
throw new TypeError ( "Expected function for second argument." ) ;
2021-10-11 09:45:02 -04:00
}
2023-02-07 14:22:46 -05:00
stepDesc = {
name : nameOrFnOrOptions ,
fn : maybeFn ,
} ;
} else if ( typeof nameOrFnOrOptions === "function" ) {
if ( ! nameOrFnOrOptions . name ) {
throw new TypeError ( "The step function must have a name." ) ;
}
if ( maybeFn != undefined ) {
2022-07-15 13:09:22 -04:00
throw new TypeError (
2023-02-07 14:22:46 -05:00
"Unexpected second argument to TestContext.step()" ,
2022-07-15 13:09:22 -04:00
) ;
}
2023-02-07 14:22:46 -05:00
stepDesc = {
name : nameOrFnOrOptions . name ,
fn : nameOrFnOrOptions ,
2022-07-15 13:09:22 -04:00
} ;
2023-02-07 14:22:46 -05:00
} else if ( typeof nameOrFnOrOptions === "object" ) {
stepDesc = nameOrFnOrOptions ;
} else {
throw new TypeError (
"Expected a test definition or name and function." ,
2022-07-15 13:09:22 -04:00
) ;
2023-02-07 14:22:46 -05:00
}
stepDesc . ignore ? ? = false ;
stepDesc . sanitizeOps ? ? = desc . sanitizeOps ;
stepDesc . sanitizeResources ? ? = desc . sanitizeResources ;
stepDesc . sanitizeExit ? ? = desc . sanitizeExit ;
2023-09-25 21:01:56 -04:00
stepDesc . location = core . currentUserCallSite ( ) ;
2023-02-07 14:22:46 -05:00
stepDesc . level = level + 1 ;
stepDesc . parent = desc ;
stepDesc . rootId = rootId ;
2023-08-09 19:40:34 -04:00
stepDesc . name = escapeName ( stepDesc . name ) ;
stepDesc . rootName = escapeName ( rootName ) ;
2023-04-13 13:43:23 -04:00
stepDesc . fn = wrapTest ( stepDesc ) ;
2023-09-25 22:21:18 -04:00
const id = ops . op _register _test _step (
stepDesc . name ,
stepDesc . location . fileName ,
stepDesc . location . lineNumber ,
stepDesc . location . columnNumber ,
stepDesc . level ,
stepDesc . parent . id ,
stepDesc . rootId ,
stepDesc . rootName ,
) ;
2023-02-07 14:22:46 -05:00
stepDesc . id = id ;
2023-09-25 22:21:18 -04:00
stepDesc . origin = desc . origin ;
2023-02-07 14:22:46 -05:00
const state = {
context : createTestContext ( stepDesc ) ,
children : [ ] ,
2023-03-25 15:32:11 -04:00
failed : false ,
completed : false ,
2023-02-07 14:22:46 -05:00
} ;
MapPrototypeSet ( testStates , stepDesc . id , state ) ;
ArrayPrototypePush (
MapPrototypeGet ( testStates , stepDesc . parent . id ) . children ,
stepDesc ,
) ;
2021-10-11 09:45:02 -04:00
2023-09-25 22:21:18 -04:00
ops . op _test _event _step _wait ( stepDesc . id ) ;
2023-03-25 15:32:11 -04:00
const earlier = DateNow ( ) ;
2023-04-13 13:43:23 -04:00
const result = await stepDesc . fn ( stepDesc ) ;
2023-03-25 15:32:11 -04:00
const elapsed = DateNow ( ) - earlier ;
state . failed = ! ! result . failed ;
stepReportResult ( stepDesc , result , elapsed ) ;
return result == "ok" ;
2022-11-21 08:36:26 -05:00
} ,
2020-07-19 13:49:44 -04:00
} ;
2023-02-07 14:22:46 -05:00
}
/ * *
2023-04-13 13:43:23 -04:00
* Wrap a user test function in one which returns a structured result .
2023-02-07 14:22:46 -05:00
* @ template T { Function }
* @ param testFn { T }
2023-04-13 13:43:23 -04:00
* @ param desc { TestDescription | TestStepDescription }
2023-02-07 14:22:46 -05:00
* @ returns { T }
* /
2023-04-13 13:43:23 -04:00
function wrapTest ( desc ) {
let testFn = wrapInner ( desc . fn ) ;
if ( desc . sanitizeOps ) {
2023-02-07 14:22:46 -05:00
testFn = assertOps ( testFn ) ;
}
2023-04-13 13:43:23 -04:00
if ( desc . sanitizeResources ) {
2023-02-07 14:22:46 -05:00
testFn = assertResources ( testFn ) ;
}
2023-04-13 13:43:23 -04:00
if ( desc . sanitizeExit ) {
2023-02-07 14:22:46 -05:00
testFn = assertExit ( testFn , true ) ;
}
2023-04-13 13:43:23 -04:00
if ( ! ( "parent" in desc ) && desc . permissions ) {
testFn = withPermissions ( testFn , desc . permissions ) ;
}
return wrapOuter ( testFn , desc ) ;
2023-02-07 14:22:46 -05:00
}
2023-03-08 06:44:54 -05:00
import { denoNs } from "ext:runtime/90_deno_ns.js" ;
2023-02-07 14:22:46 -05:00
denoNs . bench = bench ;
denoNs . test = test ;