zeek/testing/btest/scripts/base/utils/dir.test
Christian Kreibich 3d87400f1d Fix race condition in scripts.base.utils.dir test
The sequence of

- adding a new file
- deleting an existing one
- waiting for Zeek to notice the addition
- re-adding the pre-existing file

was prone to a race: it could happen that Zeek's directory observation would see
the new file in one round, and by the time the next round happens the removal
and re-addition had already happened, thus missing the change and failing the
test.

This avoids the race by placing the removal of the existing file before the
addition of the new one, ensuring that when Zeek notices the addition (and
pushes the test to the next round), it has also seen the removal, so the
re-addition cannot get lost.
2022-07-11 16:26:45 -07:00

66 lines
1.3 KiB
Text

# @TEST-EXEC: btest-bg-run zeek zeek -b ../dirtest.zeek
# @TEST-EXEC: $SCRIPTS/wait-for-file zeek/next1 10 || (btest-bg-wait -k 1 && false)
# @TEST-EXEC: rm testdir/bye
# @TEST-EXEC: touch testdir/newone
# @TEST-EXEC: $SCRIPTS/wait-for-file zeek/next2 10 || (btest-bg-wait -k 1 && false)
# @TEST-EXEC: touch testdir/bye
# @TEST-EXEC: btest-bg-wait 20
# @TEST-EXEC: btest-diff zeek/.stdout
@TEST-START-FILE dirtest.zeek
@load base/utils/dir
redef exit_only_after_terminate = T;
global c: count = 0;
global initial_files: set[string] = set();
function check_initial_file(s: string)
{
if ( s in initial_files )
print "initial file", s;
else
print "didn't see initial file", s;
}
function new_file(fname: string)
{
++c;
if ( c <= 3 )
add initial_files[fname];
else
print "new_file", fname;
if ( c == 3 )
{
check_initial_file("../testdir/hi");
check_initial_file("../testdir/howsitgoing");
check_initial_file("../testdir/bye");
system("touch next1");
}
else if ( c == 4 )
system("touch next2");
else if ( c == 5 )
terminate();
}
event zeek_init()
{
Dir::monitor("../testdir", new_file, .25sec);
}
@TEST-END-FILE
@TEST-START-FILE testdir/hi
123
@TEST-END-FILE
@TEST-START-FILE testdir/howsitgoing
abc
@TEST-END-FILE
@TEST-START-FILE testdir/bye
!@#
@TEST-END-FILE