Improve an input framework test

A race condition could cause unstable output: if the thread reading the
file is fast, often you see both "pred" functions execute and then both
"line" events execute with both entries already in the table, but if the
thread reading the file is slow, you see pred, event, pred, event, with
only one entry available in the first event.
This commit is contained in:
Jon Siwek 2020-02-07 19:58:35 -08:00
parent a3b1d202a5
commit e50a8848ae
7 changed files with 123 additions and 182 deletions

View file

@ -3,7 +3,9 @@
# @TEST-EXEC: $SCRIPTS/wait-for-file zeek/got1 15|| (btest-bg-wait -k 1 && false)
# @TEST-EXEC: mv input2.log input.log
# @TEST-EXEC: btest-bg-wait 30
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff servers.out
# @TEST-EXEC: btest-diff events.out
# @TEST-EXEC: btest-diff preds.out
@TEST-START-FILE input1.log
#separator \x09
@ -33,54 +35,65 @@ type Val: record {
ss: string;
};
global servers: table[int] of Val = table();
type servers_type: table[int] of Val;
global servers: servers_type = table();
global outfile: file;
global servers_file = open("../servers.out");
global events_file = open("../events.out");
global predicates_file = open("../preds.out");
global try: count;
event line(description: Input::TableDescription, tpe: Input::Event, left: Idx, right: Val)
{
print outfile, "============EVENT============";
print outfile, "Description";
print outfile, description;
print outfile, "Type";
print outfile, tpe;
print outfile, "Left";
print outfile, left;
print outfile, "Right";
print outfile, right;
print events_file, "============EVENT============";
print events_file, "Description";
print events_file, " source", description$source;
print events_file, " reader", description$reader;
print events_file, " mode", description$mode;
print events_file, " name", description$name;
print events_file, fmt(" destination[left = %s]", left$i),
(description$destination as servers_type)[left$i];
print events_file, " idx", description$idx;
print events_file, " val", description$val;
print events_file, " want_record", description$want_record;
print events_file, "Type", tpe;
print events_file, "Left", left;
print events_file, "Right", right;
}
event zeek_init()
{
outfile = open("../out");
try = 0;
# first read in the old stuff into the table...
Input::add_table([$source="../input.log", $mode=Input::REREAD, $name="ssh", $idx=Idx, $val=Val, $destination=servers, $ev=line,
$pred(typ: Input::Event, left: Idx, right: Val) = {
print outfile, "============PREDICATE============";
print outfile, typ;
print outfile, left;
print outfile, right;
return T;
}
Input::add_table([$source="../input.log", $mode=Input::REREAD, $name="ssh",
$idx=Idx, $val=Val, $destination=servers, $ev=line,
$pred(typ: Input::Event, left: Idx, right: Val) = {
print predicates_file, "============PREDICATE============";
print predicates_file, typ;
print predicates_file, left;
print predicates_file, right;
return T;
}
]);
}
event Input::end_of_data(name: string, source: string)
{
print outfile, "==========SERVERS============";
print outfile, servers;
print servers_file, "==========SERVERS============";
print servers_file, servers;
try = try + 1;
if ( try == 1 )
system("touch got1");
else if ( try == 2 )
{
print outfile, "done";
close(outfile);
print servers_file, "done";
close(servers_file);
close(events_file);
close(predicates_file);
Input::remove("input");
terminate();
}