You are viewing a plain text version of this content. The canonical link for it is here.
Posted to test-commits@perl.apache.org by to...@apache.org on 2011/03/05 13:17:18 UTC

svn commit: r1078277 - in /perl/Apache-Test/trunk: Changes lib/Apache/TestUtil.pm t/log_watch_for_broken_lines.t

Author: torsten
Date: Sat Mar  5 12:17:18 2011
New Revision: 1078277

URL: http://svn.apache.org/viewvc?rev=1078277&view=rev
Log:
add t_file_watch_for() to Apache::TestUtil

Added:
    perl/Apache-Test/trunk/t/log_watch_for_broken_lines.t
Modified:
    perl/Apache-Test/trunk/Changes
    perl/Apache-Test/trunk/lib/Apache/TestUtil.pm

Modified: perl/Apache-Test/trunk/Changes
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/Changes?rev=1078277&r1=1078276&r2=1078277&view=diff
==============================================================================
--- perl/Apache-Test/trunk/Changes (original)
+++ perl/Apache-Test/trunk/Changes Sat Mar  5 12:17:18 2011
@@ -8,6 +8,8 @@ Changes - Apache::Test change logfile
 
 =item 1.37-dev
 
+Add t_file_watch_for to Apache::TestUtil [Torsten Foertsch]
+
 Add $boolean parameter to Apache::TestHandler::ok and Apache::TestHandler::ok1
 Add a few bits of documentation [Torsten Foertsch]
 

Modified: perl/Apache-Test/trunk/lib/Apache/TestUtil.pm
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/lib/Apache/TestUtil.pm?rev=1078277&r1=1078276&r2=1078277&view=diff
==============================================================================
--- perl/Apache-Test/trunk/lib/Apache/TestUtil.pm (original)
+++ perl/Apache-Test/trunk/lib/Apache/TestUtil.pm Sat Mar  5 12:17:18 2011
@@ -43,7 +43,7 @@ $VERSION = '0.02';
 );
 
 @EXPORT_OK = qw(t_write_perl_script t_write_shell_script t_chown
-                t_catfile_apache t_catfile
+                t_catfile_apache t_catfile t_file_watch_for
                 t_start_error_log_watch t_finish_error_log_watch
                 t_start_file_watch t_read_file_watch t_finish_file_watch);
 
@@ -101,6 +101,33 @@ use constant INDENT     => 4;
         return readline $fh;
     }
 
+    sub t_file_watch_for ($$$) {
+	my ($name, $re, $timeout) = @_;
+	local $/ = "\n";
+	$re = qr/$re/ unless ref $re;
+	$timeout *= 10;
+	my $buf = '';
+	my @acc;
+	while ($timeout >= 0) {
+	    my $line = t_read_file_watch $name;
+	    unless (defined $line) { # EOF
+		select undef, undef, undef, 0.1;
+		$timeout--;
+		next;
+	    }
+	    $buf .= $line;
+	    next unless $buf =~ /\n$/; # incomplete line
+
+	    # found a complete line
+	    $line = $buf;
+	    $buf = '';
+
+	    push @acc, $line;
+	    return wantarray ? @acc : $line if $line =~ $re;
+	}
+	return;
+    }
+
     sub t_start_error_log_watch {
         t_start_file_watch;
     }
@@ -917,6 +944,36 @@ record length use this:
     @lines=t_finish_file_watch($name);
   }
 
+=item t_file_watch_for()
+
+  @lines=Apache::TestUtil::t_file_watch_for('access_log',
+                                            qr/condition/,
+                                            $timeout);
+
+This function reads the file from the current position and looks for the
+first line that matches C<qr/condition/>. If no such line could be found
+until end of file the function pauses and retries until either such a line
+is found or the timeout (in seconds) is reached.
+
+In scalar or void context only the matching line is returned. In list
+context all read lines are returned with the matching one in last position.
+
+The function uses C<\n> and end-of-line marker and waits for complete lines.
+
+The timeout although it can be specified with sub-second precision is not very
+accurate. It is simply multiplied by 10. The result is used as a maximum loop
+count. For the intented purpose this should be good enough.
+
+Use this function to check for logfile entries when you cannot be sure that
+they are already written when the test program reaches the point, for example
+to check for messages that are written in a PerlCleanupHandler or a
+PerlLogHandler.
+
+ ok t_file_watch_for 'access_log', qr/expected log entry/, 2;
+
+This call reads the C<access_log> and waits for maximum 2 seconds for the
+expected entry to appear.
+
 =back
 
 =head1 AUTHOR

Added: perl/Apache-Test/trunk/t/log_watch_for_broken_lines.t
URL: http://svn.apache.org/viewvc/perl/Apache-Test/trunk/t/log_watch_for_broken_lines.t?rev=1078277&view=auto
==============================================================================
--- perl/Apache-Test/trunk/t/log_watch_for_broken_lines.t (added)
+++ perl/Apache-Test/trunk/t/log_watch_for_broken_lines.t Sat Mar  5 12:17:18 2011
@@ -0,0 +1,40 @@
+#!perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Apache::Test;
+use Apache::TestUtil qw/t_start_file_watch t_file_watch_for
+			t_cmp t_catfile t_append_file/;
+
+plan tests => 5;
+
+my $fn=t_catfile(Apache::Test::vars->{t_logs}, 'watch');
+unlink $fn;
+
+t_start_file_watch 'watch';
+
+my $pid;
+select undef, undef, undef, 0.1 until defined($pid=fork);
+unless ($pid) {			# child
+    t_append_file $fn, "\nhuhu\n4 5 6 \nblabla\n";
+    for(1..3) {
+	select undef, undef, undef, 0.3;
+	t_append_file $fn, "$_ ";
+    }
+    t_append_file $fn, "\nhuhu\n4 5 6 \nblabla";
+    exit 0;
+}
+
+ok t_cmp t_file_watch_for('watch', qr/^1 2 3 $/, 2),
+    "1 2 3 \n", 'incomplete line';
+
+my @lines=t_file_watch_for('watch', qr/^\d \d \d $/, 2);
+ok t_cmp @lines, 2, '2 lines';
+ok t_cmp $lines[0], "huhu\n", '1st line';
+ok t_cmp $lines[1], "4 5 6 \n", 'found it';
+
+ok t_cmp t_file_watch_for('watch', qr/^\d \d \d $/, 0.3),
+    undef, 'timeout';
+
+waitpid $pid, 0;