File Coverage

blib/lib/Plack/Middleware/Delay.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 4 75.0
condition 2 4 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 41 44 93.1


line stmt bran cond sub pod time code
1             ## no critic (RequireUseStrict)
2             package Plack::Middleware::Delay;
3             BEGIN {
4 3     3   2147 $Plack::Middleware::Delay::VERSION = '0.01';
5             }
6              
7             ## use critic (RequireUseStrict)
8 3     3   23 use strict;
  3         4  
  3         190  
9 3     3   15 use warnings;
  3         5  
  3         93  
10 3     3   16 use parent 'Plack::Middleware';
  3         5  
  3         27  
11              
12 3     3   182 use Plack::Util;
  3         6  
  3         631  
13              
14             sub call {
15 4     4 1 29286 my ( $self, $env ) = @_;
16              
17 4         29 my $app = $self->app;
18 4   50     155 my $delay = $self->{'delay'} || 0;
19             my $sleep_fn = $self->{'sleep_fn'} || sub {
20 4     4   5 my ( $delay, $invoke ) = @_;
21              
22 4         8001282 sleep $delay;
23              
24 4         35 $invoke->();
25 4   50     68 };
26              
27             return sub {
28 4     4   57 my ( $respond ) = @_;
29              
30             $sleep_fn->($delay, sub {
31 4         44 my $res = $app->($env);
32              
33 4 100       100 if(ref($res) eq 'ARRAY') {
    50          
34 2         22 $respond->($res);
35             } elsif(ref($res) eq 'CODE') {
36 2         9 $res->($respond);
37             }
38 4         17 });
39 4         47 };
40             }
41              
42             1;
43              
44              
45              
46             =pod
47              
48             =head1 NAME
49              
50             Plack::Middleware::Delay - Put delays on your requests
51              
52             =head1 VERSION
53              
54             version 0.01
55              
56             =head1 SYNOPSIS
57              
58             use Plack::Builder;
59              
60             builder {
61             enable 'Delay', delay => 5; # delays the response by five seconds
62             $app;
63             };
64              
65             # or, if you're in an AnyEvent-based PSGI server...
66              
67             builder {
68             enable 'Delay', delay => 5, sleep_fn => sub {
69             my ( $delay, $invoke ) = @_;
70              
71             my $timer;
72             $timer = AnyEvent->timer(
73             after => $delay,
74             cb => sub {
75             undef $timer;
76             $invoke->();
77             },
78             );
79             };
80             $app;
81             };
82              
83             =head1 DESCRIPTION
84              
85             This middleware imposes an artifical delay on requests, for purposes of
86             testing. It could also be used to implement L.
87              
88             =head1 OPTIONS
89              
90             =head2 delay
91              
92             The number of seconds to sleep. It can be an integer or a float; however, the
93             default sleep_fn only works on integers.
94              
95             =head2 sleep_fn
96              
97             A subroutine reference that will be called when it's time to go to sleep. The
98             subroutine reference will be provided two arguments: the number of seconds to
99             sleep (ie. the value you provided to L), and a subroutine reference
100             that will continue the PSGI application as normal (think of it as a
101             continuation).
102              
103             =head1 SEE ALSO
104              
105             L
106              
107             =head1 AUTHOR
108              
109             Rob Hoelz
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             This software is copyright (c) 2011 by Rob Hoelz.
114              
115             This is free software; you can redistribute it and/or modify it under
116             the same terms as the Perl 5 programming language system itself.
117              
118             =head1 BUGS
119              
120             Please report any bugs or feature requests on the bugtracker website
121             http://github.com/hoelzro/plack-middleware-delay/issues
122              
123             When submitting a bug or request, please include a test-file or a
124             patch to an existing test-file that illustrates the bug or desired
125             feature.
126              
127             =cut
128              
129              
130             __END__