测试Mojo - 过早连接关闭

时间:2015-05-20 11:46:42

标签: mojolicious

我正在使用以下代码测试我的应用:

use Carp; use Carp::Heavy;
use Mojo::Base -strict;

use Test::More;
use Test::Mojo;

use FindBin;
require "$FindBin::Bin/../script/MyApp";
my $t = Test::Mojo->new( 'MyDb' );
$t->ua->max_redirects(10);
$t->ua->inactivity_timeout(3600);
$t->ua->connect_timeout(3600);

$t->get_ok('/int/ap/profit/2015-01-01/2015-03-31/overview')
  ->status_is(200)
  ->text_is('html body h3' => 'Überblick')
  ->text_is('tr:nth-child(2) td:nth-child(8)' => '2.000,00')
  ->text_is('tr:nth-child(5) td:nth-child(8)' => '4.000,00')
;

done_testing();

此测试因过早连接关闭而失败。然而,在morbo上运行相同的请求会将预期的html代码返回给浏览器。似乎$t->ua->inactivity_timeout(3600);在这里没有任何效果。

2 个答案:

答案 0 :(得分:2)

长时间操作需要{strong>客户端端和{strong>服务器端inactivity_timeout。 此外,如果您编写非阻止代码,则必须具有render_later

有一天我需要选项max_connections,但我不记得为什么。 所以,在这一天之后,我总是将max_connections(0)添加到我的测试中:)

参见示例here

当你发布你的问题时,你应该编写简单的mojolicious lite示例,其中包含所有有错误的逻辑。所以,现在你展示正确的测试代码,并询问为什么没有主逻辑它不工作:)这是非常奇怪的。当你有具体的事情时,没有人会回答你或提出可能的事件。

答案 1 :(得分:0)

基于@Logioniz answergist 这是他的MWE的叉子

use Mojo::Base -strict;

use Mojo::IOLoop;
use Mojolicious::Lite;
use Test::More;
use Test::Mojo;

get '/delayed' => sub {
  my $c = shift->render_later;
  $c->inactivity_timeout(5);
  Mojo::IOLoop->timer(3 => sub {
    $c->render(json => {result => 'ok'});
  });
};

get '/premature' => sub {
  my $c = shift;
  #$c->render(text => 'To be continued...', status => 206);
  $c->tx->res->headers->connection('close');
  $c->rendered(123);
};

my $t = Test::Mojo->new;

subtest 'enough timeout' => sub {
  $t->ua->inactivity_timeout(5);
  $t->get_ok('/delayed')
    ->status_is(200)
    ->json_is('/result' => 'ok')
  ;
};

subtest 'not enough timeout' => sub {
  $t->ua->inactivity_timeout(1);
  $t->get_fail('/delayed')
    ->status_isnt(200)
    ->content_is('', 'no content')
  ;
};

subtest 'sudden close' => sub {
  $t->ua->inactivity_timeout(5);
  $t->ua->max_connections(0);
  $t->get_fail('/premature')
    ->status_isnt(200)
    ->content_is('', 'no content')
  ;
};

done_testing();

package Test::Mojo;

use Test::More;

sub get_fail {
  my ($self, $uri) = @_;
  TODO: {
    local $TODO = 'get_fail';
    return $self->get_ok($uri);
  }
}

相应的输出是:

# Subtest: enough timeout
    ok 1 - GET /delayed
    ok 2 - 200 OK
    ok 3 - exact match for JSON Pointer "/result"
    1..3
ok 1 - enough timeout
# Subtest: not enough timeout
    # Inactivity timeout
    not ok 1 - GET /delayed # TODO get_fail
    #   Failed (TODO) test 'GET /delayed'
    #   at D:/TMP-aux/MSP-reports2/1-fork-quick.pl line 60.
    ok 2 - not 200 OK
    ok 3 - no content
    1..3
ok 2 - not enough timeout
# Subtest: sudden close
    # Premature connection close
    not ok 1 - GET /premature # TODO get_fail
    #   Failed (TODO) test 'GET /premature'
    #   at D:/TMP-aux/MSP-reports2/1-fork-quick.pl line 60.
    ok 2 - not 200 OK
    ok 3 - no content
    1..3
ok 3 - sudden close
1..3