App-plackbench

 view release on metacpan or  search on metacpan

t/03_plackbench_pm.t  view on Meta::CPAN

        post_data => [ 'a', 'bb', 'ccc' ],
    );
    $bench->app()->_clear_requests();
    $bench->run();

    my @non_post = grep {
        $_->{REQUEST_METHOD} ne 'POST'
    } @{ $bench->app()->_get_requests() };
    ok(!@non_post, 'all requests should be POST requests when post data is specified');

    my @lengths = map { $_->{CONTENT_LENGTH} } @{ $bench->app()->_get_requests() };
    cmp_deeply(\@lengths,
        [1, 2, 3, 1, 2], 'should cycle through POST data in order');
    return;
}

sub test_warm {
    my $count = 5;
    my $bench = App::plackbench->new(
        psgi_path => $psgi_path,
        count     => $count,
        uri       => '/ok',
        warm      => 1,
    );
    $bench->app()->_clear_requests();
    my $stats = $bench->run();

    is($stats->count(), $count, 'should put as many requests into the stats as asked for');
    is(scalar(@{$bench->app()->_get_requests()}), $count + 1, 'should make an extra request when "warm" is enabled');

    $bench->warm(0);
    $bench->app()->_clear_requests();
    $stats = $bench->run();

    is($stats->count(), $count, 'should put as many requests into the stats as asked for');
    is(scalar(@{$bench->app()->_get_requests()}), $count, 'should not make an extra request when "warm" is disabled');

    return;
}

sub test_fixup_from_file {
    my $bench = App::plackbench->new(
        psgi_path => $psgi_path,
        uri       => '/ok',
    );

    ok($bench->run()->mean(), 'should run ok to begin with');

    $bench->add_fixup_from_file("$Bin/fail_redirect");

    eval {
        $bench->run();
    };
    like($@, qr/failed/, 'should eval the file and use it\'s sub');

    $bench->fixup(undef);
    $bench->add_fixup_from_file("$Bin/fail_redirect");
    is(Scalar::Util::reftype($bench->fixup()->[0]), 'CODE', 'should initialize fixup() if necessary');

    eval {
        $bench->add_fixup_from_file("$Bin/does_not_exist");
    };
    
    # Don't try and check that the error contains "No such file", cause that's
    # a different error in German (just sayin', it's not it came up or
    # anything...)
    ok($@, 'should die when file doesn\'t exist');

    eval {
        $bench->add_fixup_from_file("$Bin/syntax_error");
    };
    like($@, qr#\Q$Bin/syntax_error#, 'should die when file doesn\'t compile');

    eval {
        $bench->add_fixup_from_file("$Bin/non_sub");
    };
    like($@, qr/does not return a subroutine/, 'should die when file doesn\'t return a subroutine reference');

    return;
}



( run in 2.508 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )