Add sleep() BiF.

Yes, really. :-) We've hit the need for this on occasion in very specific
settings and always worked around it via ugly nested loops or similars.
This has ample warning that folks normally won't want to use this.

Not sure that ZAM btest should baseline the number of BiFs.
This commit is contained in:
Christian Kreibich 2024-12-02 13:35:25 -08:00
parent ead6134501
commit e6d0c8aa04
6 changed files with 46 additions and 1 deletions

View file

@ -431,6 +431,7 @@ static std::unordered_map<std::string, unsigned int> func_attrs = {
{"skip_further_processing", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"skip_http_entity_data", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"skip_smtp_data", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"sleep", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"split_string", ATTR_FOLDABLE},
{"split_string1", ATTR_FOLDABLE},
{"split_string_all", ATTR_FOLDABLE},

View file

@ -600,6 +600,27 @@ function piped_exec%(program: string, to_write: string%): bool
return zeek::val_mgr->True();
%}
## Sleeps for the given amount of time.
##
## i: The time interval to sleep for.
##
## Returns: The :zeek:type:`interval` Zeek actually slept for.
##
## .. note::
##
## This is a blocking sleep! Zeek will not run most of its processing
## during that time. You almost certainly DO NOT WANT THIS outside
## of specific testing/troubleshooting scenarios. To sleep asynchronously,
## :zeek:see:`schedule` an event, or consider :zeek:id:`Exec::run`.
function sleep%(i: interval%): interval
%{
const auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::duration<double>(i));
const auto end = std::chrono::high_resolution_clock::now();
const auto slept = std::chrono::duration<double>(end - start).count();
return zeek::make_intrusive<zeek::IntervalVal>(slept);
%}
%%{
#include "zeek/OpaqueVal.h"
%%}

View file

@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
539 seen BiFs, 0 unseen BiFs (), 0 new BiFs ()
540 seen BiFs, 0 unseen BiFs (), 0 new BiFs ()

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,21 @@
# Verifies sleep()'s reported latencies.
#
# @TEST-EXEC: zeek -b %INPUT 2>out
# @TEST-EXEC: btest-diff out
function test_sleep(i: interval)
{
local start = current_time();
local sleep_delay = sleep(i);
local script_delay = current_time() - start;
assert script_delay >= i, fmt("sleep() took %s, less than %s", script_delay, i);
assert sleep_delay >= i, fmt("slept for %s, less than %s", script_delay, i);
assert sleep_delay <= script_delay, fmt("sleep() claims %s, longer than %s", sleep_delay, script_delay);
}
event zeek_init()
{
test_sleep(100msec);
test_sleep(1sec);
}

View file

@ -464,6 +464,7 @@ global known_BiFs = set(
"skip_further_processing",
"skip_http_entity_data",
"skip_smtp_data",
"sleep",
"sort",
"split_string",
"split_string1",