Debian Codegraph @

t/issues/memleak/die_in_hooks.t

raw
1 # reported memory leak without GH issue or RT ticket
2 use strict;
3 use warnings;
4 use Test::More tests => 6;
5 use Plack::Test;
6 use Capture::Tiny 'capture_stderr';
7 use HTTP::Request::Common;
8
9 my $called;
10 { package Foo::Destroy; sub DESTROY { $called++ } } ## no critic
11
12 {
13 package App; ## no critic
14 use Dancer2;
15 my $env_key = 'psgix.ignoreme.refleak';
16
17 hook before => sub {
18 request->env->{$env_key} = bless {}, 'Foo::Destroy';
19 };
20
21 hook before => sub {
22 ::ok( request->env->{$env_key}, 'Object exists' );
23 ::isa_ok( request->env->{$env_key}, 'Foo::Destroy', 'It is an object' );
24
25 die "whoops";
26 };
27
28 get '/' => sub {'OK'};
29 }
30
31 my $test = Plack::Test->create( App->to_app );
32 my $res;
33 my $stderr = capture_stderr { $res = $test->request( GET '/' ) };
34
35 ok( ! $res->is_success, 'Request failed' );
36 is( $res->code, 500, 'Failure status' );
37 is( $called, 1, 'Memory cleaned' );
38
39 # double check stderr
40 # '[App:21992] error @2015-03-03 16:39:07> Exception caught in 'core.app.before_request' filter: Hook error: whoops at t/issues/memleak/die_in_hooks.t line 25.
41 # at lib/Dancer2/Core/App.pm line 848. in (eval 117) l. 1
42 # at ...
43 # '
44 like(
45 $stderr,
46 qr{
47 ^
48 \[App:\d+\] \s error \s [\@\-\d\s:]+> \s
49 \QException caught in 'core.app.before_request' filter:\E \s
50 \QHook error: whoops\E \s
51 [^\n]+ \n \s* # everything until newline + newline
52 at [^\n]+ \n # another such line (there could be more)
53 }x,
54 'Correct error',
55 );