<div dir="ltr"><div>Hi all,<br><br></div><div>I have an issue executing hooks in dancer2, The code is running twice but I'm just calling once.<br></div><div><br></div><div>This is a sample code (minimum)<br></div><div><br>File: lib/Dancer2/Plugin/MyPluginTest.pm<br>:::::::::::::::::::::::::<br>MyPluginTest.pm<br>:::::::::::::::::::::::::<br><br>package Dancer2::Plugin::MyPluginTest;<br>use Dancer2::Plugin;<br><br>register_hook 'my_new_hook_after';<br><br>register new_function => sub {<br> my $dsl = shift;<br> execute_hook 'my_new_hook_after';<br>};<br><br>register_plugin;<br><br>:::::::::::::::::::::::::<br><br><br></div><div>Test File: <a href="http://plugin.pl">plugin.pl</a><br></div><div>::::::::::::::<br><a href="http://plugin.pl">plugin.pl</a><br>::::::::::::::<br>use strict;<br>use warnings;<br><br>use Test::More;<br>use Plack::Test;<br>use HTTP::Request::Common;<br>{<br> package MyTestApp;<br> use Dancer2;<br> use Dancer2::Plugin::MyPluginTest;<br><br> my $var = 0;<br> <br> get '/' => sub {<br> new_function;<br> $var;<br> };<br><br> hook 'my_new_hook_after' => sub {<br> $var++;<br> debug "Executing hook - $var";<br> return;<br> };<br>};<br><br>my $test = Plack::Test->create( MyTestApp->to_app );<br><br>my $res = $test->request( GET '/' );<br>is( $res->content, '1', 'Var is 1 after one call on /' );<br>$res = $test->request( GET '/' );<br>is( $res->content, '2', 'Var is 1 after one call on /' );<br>done_testing();<br>::::::::::::::<br><br><br></div><div>The result after : prove -l lib/ <a href="http://plugin.pl">plugin.pl</a> -v<br><br><br><a href="http://plugin.pl">plugin.pl</a> .. [MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook - 1 in <a href="http://plugin.pl">plugin.pl</a> l. 21<br>[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook - 2 in <a href="http://plugin.pl">plugin.pl</a> l. 21<br><br>not ok 1 - Var is 1 after one call on /<br><br># Failed test 'Var is 1 after one call on /'<br># at <a href="http://plugin.pl">plugin.pl</a> line 28.<br># got: '2'<br># expected: '1'<br>[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook - 3 in <a href="http://plugin.pl">plugin.pl</a> l. 21<br>[MyTestApp:14688] debug @2015-10-01 09:25:43> Executing hook - 4 in <a href="http://plugin.pl">plugin.pl</a> l. 21<br>not ok 2 - Var is 1 after one call on /<br><br># Failed test 'Var is 1 after one call on /'<br># at <a href="http://plugin.pl">plugin.pl</a> line 30.<br># got: '4'<br># expected: '2'<br>1..2<br># Looks like you failed 2 tests of 2.<br>Dubious, test returned 2 (wstat 512, 0x200)<br>Failed 2/2 subtests <br><br>Test Summary Report<br>-------------------<br><a href="http://plugin.pl">plugin.pl</a> (Wstat: 512 Tests: 2 Failed: 2)<br> Failed tests: 1-2<br> Non-zero exit status: 2<br>Files=1, Tests=2, 2 wallclock secs ( 0.04 usr 0.00 sys + 0.44 cusr 0.02 csys = 0.50 CPU)<br>Result: FAIL<br><br><br></div><div>Does someone an idea what i'm doing wrong? or if is it something about the hooks?<br></div><div><br></div><div>Thanks in advance!<br></div><div><br><br></div>Ruben.<br clear="all"><div><div><br>-- <br><div><div dir="ltr">____________________________<br>Rubén Darío Amórtegui Medina<br>MCE - Ingeniero de Sistemas</div></div>
</div></div></div>