|
18 | 18 | # Optionally, SEARCH_ABORT can be set to "FOUND" or "NOT FOUND" and this
|
19 | 19 | # will abort if the search result doesn't match the requested one.
|
20 | 20 | #
|
| 21 | +# Optionally, SEARCH_WAIT can be set to "FOUND" or "NOT FOUND", and this |
| 22 | +# will wait for the condition to occur. The timeout can be set in |
| 23 | +# SEARCH_TIMEOUT, default is 60 seconds. |
| 24 | +# |
21 | 25 | # Optionally, SEARCH_OUTPUT can be set to control the format of output.
|
22 | 26 | # Supported formats:
|
23 | 27 | # - (default) : "FOUND n /pattern/ in FILE " or "NOT FOUND ..."
|
@@ -55,31 +59,50 @@ perl;
|
55 | 59 | my @search_files= glob($ENV{SEARCH_FILE});
|
56 | 60 | my $search_pattern= $ENV{SEARCH_PATTERN} or die "SEARCH_PATTERN not set";
|
57 | 61 | my $search_range= $ENV{SEARCH_RANGE};
|
58 |
| - my $content; |
59 |
| - foreach my $search_file (@search_files) { |
60 |
| - open(FILE, '<', $search_file) || die("Can't open file $search_file: $!"); |
61 |
| - my $file_content; |
62 |
| - if ($search_range > 0) { |
63 |
| - read(FILE, $file_content, $search_range, 0); |
64 |
| - } elsif ($search_range < 0) { |
65 |
| - my $size= -s $search_file; |
66 |
| - $search_range = -$size if $size > -$search_range; |
67 |
| - seek(FILE, $search_range, 2); |
68 |
| - read(FILE, $file_content, -$search_range, 0); |
69 |
| - } else { |
70 |
| - while(<FILE>) { # error log |
71 |
| - if (/^CURRENT_TEST:/) { |
72 |
| - $content=''; |
| 62 | + my $timeout= $ENV{SEARCH_TIMEOUT} || 60; |
| 63 | + my @matches; |
| 64 | + my $res; |
| 65 | +
|
| 66 | + my $start_time= time(); |
| 67 | + for (;;) { |
| 68 | + my $content; |
| 69 | + foreach my $search_file (@search_files) { |
| 70 | + open(FILE, '<', $search_file) || die("Can't open file $search_file: $!"); |
| 71 | + my $file_content; |
| 72 | + if ($search_range > 0) { |
| 73 | + read(FILE, $file_content, $search_range, 0); |
| 74 | + } elsif ($search_range < 0) { |
| 75 | + my $size= -s $search_file; |
| 76 | + $search_range = -$size if $size > -$search_range; |
| 77 | + seek(FILE, $search_range, 2); |
| 78 | + read(FILE, $file_content, -$search_range, 0); |
73 | 79 | } else {
|
74 |
| - $content.=$_; |
| 80 | + while(<FILE>) { # error log |
| 81 | + if (/^CURRENT_TEST:/) { |
| 82 | + $content=''; |
| 83 | + } else { |
| 84 | + $content.=$_; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + close(FILE); |
| 89 | + $content.= $file_content; |
| 90 | + } |
| 91 | + @matches= ($content =~ /$search_pattern/gs); |
| 92 | + $res=@matches ? "FOUND " . scalar(@matches) : "NOT FOUND"; |
| 93 | +
|
| 94 | + if (($ENV{SEARCH_WAIT} eq 'FOUND' && $res eq 'NOT FOUND') || |
| 95 | + ($ENV{SEARCH_WAIT} eq 'NOT FOUND' && $res =~ m{^FOUND })) { |
| 96 | + if (time() - $start_time < $timeout) { |
| 97 | + # Millisceond sleep emulated with select |
| 98 | + select(undef, undef, undef, 0.1); |
| 99 | + next; |
75 | 100 | }
|
76 |
| - } |
| 101 | + die "Timeout waiting for $ENV{SEARCH_WAIT} ". |
| 102 | + "for /$search_pattern/ in $ENV{SEARCH_FILE}\n"; |
77 | 103 | }
|
78 |
| - close(FILE); |
79 |
| - $content.= $file_content; |
| 104 | + last; |
80 | 105 | }
|
81 |
| - my @matches= ($content =~ /$search_pattern/gs); |
82 |
| - my $res=@matches ? "FOUND " . scalar(@matches) : "NOT FOUND"; |
83 | 106 |
|
84 | 107 | $ENV{SEARCH_FILE} =~ s{^.*?([^/\\]+)$}{$1};
|
85 | 108 |
|
|
0 commit comments