summaryrefslogtreecommitdiff
path: root/t/tests/logging.t
blob: 390dd912c1a671eb2faadf9e82ae1ede90994e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!perl 
use strict;
use warnings;
 
use IO::Async::PSGI;
 
use IO::Async::Loop;
use IO::Async::Test;
use HTTP::Request;
use HTTP::Message::PSGI;
use Future;
use Plack::Util;
use Plack::Middleware::RequestId;
use Plack::Builder;
use Log::Any::Test;
use Log::Any qw($log);
 
use Test2::Bundle::Extended;
use Data::Printer;
 
my $app_f = sub {
    my ($env) = @_;
    my $loop = $env->{'io.async.loop'};
 
    $log->info('starting');
    $loop
        ->delay_future(after=>0.1)
        ->then(sub{
                   $log->info('first step');
                   $loop->delay_future(after=>0.1);
               })
        ->then(sub{
                   $log->info('second step');
                   $loop->delay_future(after=>0.1);
               })
        ->then(sub{
                   $log->info('third step');
                   Future->done([200,[],['']]);
               });
};
 
my $loop = IO::Async::Loop->new;
testing_loop $loop;
 
my $psgi = IO::Async::PSGI->new({
    app => $app_f,
});
my $app = builder {
    enable 'RequestId';
    $psgi->psgi_app;
};
 
IO::Async::PSGI->logging_requestid_cb(
    sub {
        my ($req_id) = @_;
        if ($req_id) { $log->{prefix} = "$req_id " }
        else { $log->{prefix} = undef }
    }
);
 
my @res;
for my $i (0..2) {
    my $req = HTTP::Request->new(GET => 'http://localhost/')->to_psgi;
    $req->{'io.async.loop'}=$loop;
 
    my $raw_res = $app->($req);
    $raw_res->(sub{$res[$i]=shift});
}
 
wait_for { $res[0] && $res[1] && $res[2] };
 
is($res[$_]->[0],200,"response $_ ok")
    for 0..2;
 
my @ids = map { Plack::Util::header_get($_->[1],'X-Request-Id') } @res;
 
my @log_lines =
    map { [$_->{category},split / /,$_->{message},2] }
    @{ $log->msgs };
 
my %log_lines_for_req;
push @{$log_lines_for_req{$_->[1]}},$_ for @log_lines;
 
is(
    \@ids,
    bag { item $_ for keys %log_lines_for_req; end },
    'request ids match',
);
 
for my $req_id (keys %log_lines_for_req) {
    subtest "request $req_id" => sub {
        is(
            $log_lines_for_req{$req_id},
            [
                ['main',$req_id,'starting'],
                ['main',$req_id,'first step'],
                ['main',$req_id,'second step'],
                ['main',$req_id,'third step'],
            ],
            'correct events',
        );
    };
}
 
done_testing;