Skip to content

Commit

Permalink
add tests for EventTime
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Jun 2, 2016
1 parent 38ed788 commit 7938750
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
27 changes: 24 additions & 3 deletions t/04_live.t
Expand Up @@ -3,7 +3,7 @@ use warnings;
use Test::More;
use Test::TCP;
use Time::Piece;
use t::Util qw/ run_fluentd slurp_log /;
use t::Util qw/ run_fluentd slurp_log $ENABLE_TEST_EVENT_TIME /;
use POSIX qw/ setlocale LC_ALL /;
use Capture::Tiny qw/ capture /;

Expand All @@ -25,8 +25,7 @@ subtest tcp => sub {
ok $logger->post( $tag, { "foo" => "bar" }), "post ok";

my $time = time - int rand(3600);
my $time_str = localtime($time)->strftime("%Y-%m-%dT%H:%M:%S%z");
$time_str =~ s/(\d\d)$/:$1/; # TZ offset +0000 => +00:00
my $time_str = localtime($time)->strftime("%Y-%m-%dT%H:%M:%S.000000000%z");

ok $logger->post_with_time( $tag, { "FOO" => "BAR" }, $time ), "post_with_time ok";
sleep 1;
Expand All @@ -36,6 +35,28 @@ subtest tcp => sub {
like $log => qr{\Q$time_str\E\t$tag\t\{"FOO":"BAR"\}}, "match post_with_time log";
};

subtest tcp_event_time => sub {
plan skip_all => "installed fluentd not supports event_time"
unless $ENABLE_TEST_EVENT_TIME;

my $logger = Fluent::Logger->new( port => $port, event_time => 1 );
isa_ok $logger, "Fluent::Logger";
my $tag = "test.tcp";
ok $logger->post( $tag, { "event_time" => "foo" }), "post ok";

my $time = Time::HiRes::time;
my $time_i = int($time);
my $nanosec = sprintf("%09d", int(($time - $time_i) * 10 ** 9));
my $time_str = localtime($time)->strftime("%Y-%m-%dT%H:%M:%S.${nanosec}%z");

ok $logger->post_with_time( $tag, { "event_time" => "bar" }, $time ), "post_with_time ok";
sleep 1;
my $log = slurp_log $dir;
note $log;
like $log => qr{\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}[+-]\d{4}\t$tag\t\{"event_time":"foo"\}}, "match post log";
like $log => qr{\Q$time_str\E\t$tag\t\{"event_time":"bar"\}}, "match post_with_time log";
};

subtest error => sub {
my $logger = Fluent::Logger->new( port => $port );
ok $logger->post( "test.error" => { foo => "ok" } );
Expand Down
17 changes: 13 additions & 4 deletions t/Util.pm
Expand Up @@ -4,9 +4,11 @@ use warnings;
use File::Temp qw/ tempdir /;
use Path::Class qw/ dir /;
use Test::TCP;
use version;

use Exporter 'import';
our @EXPORT_OK = qw/ streaming_decode_mp run_fluentd slurp_log /;
our $ENABLE_TEST_EVENT_TIME;
our @EXPORT_OK = qw/ streaming_decode_mp run_fluentd slurp_log $ENABLE_TEST_EVENT_TIME /;

sub streaming_decode_mp {
my $sock = shift;
Expand All @@ -22,17 +24,23 @@ sub streaming_decode_mp {

sub slurp_log($) {
my $dir = shift;
my @file = grep { /test\.log/ } dir($dir)->children;
my @file = grep { !/\.meta$/ } grep { /test\.log/ } dir($dir)->children;
return join("", map { $_->slurp } @file);
}

sub run_fluentd {
my $fixed_port = shift;
my $input = shift || "forward";
if ( system("fluentd", "--version") != 0 ) {
my ($v) = ( `fluentd --version` =~ /^fluentd ([0-9.]+)/ );
if (!$v) {
Test::More::plan skip_all => "fluentd is not installed.";
}

if (version->parse($v) >= version->parse("0.14.0")) {
Test::More::note "fluentd version $v: enabling tests for event time.";
$ENABLE_TEST_EVENT_TIME = 1;
} else {
Test::More::note "fluentd version < 0.14.0: disabling tests for event time.";
}
my $dir = tempdir( CLEANUP => 1 );
my $code = sub {
my $port = shift;
Expand Down Expand Up @@ -64,6 +72,7 @@ _END_
<match test.*>
type file
path ${dir}/test.log
time_format %Y-%m-%dT%H:%M:%S.%N%z
</match>
_END_
exec "fluentd", "-c", "$dir/fluent.conf";
Expand Down
1 change: 1 addition & 0 deletions xt/04_pod-coverage.t
Expand Up @@ -9,5 +9,6 @@ all_pod_coverage_ok(
max_write_retry write_length socket_io
errors prefer_integer packer pending
connect_error_history owner_pid
event_time
/ ], },
);
2 changes: 1 addition & 1 deletion xt/06_benchmark.t
Expand Up @@ -15,7 +15,7 @@ require Number::Format;
use_ok "Fluent::Logger";
diag "starting benchmark...";
for my $size ( 10, 100, 1000 ) {
my $n = 10000;
my $n = 50000;
my $msg = "x" x $size;
my $start = time;
my $logger = Fluent::Logger->new( port => $port );
Expand Down

0 comments on commit 7938750

Please sign in to comment.