File: restarter.t

package info (click to toggle)
libplack-perl 0.9989-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,556 kB
  • sloc: perl: 6,890; python: 6; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use strict;
use Test::More;
use Test::TCP;
use LWP::UserAgent;
use HTTP::Request::Common;
use Plack::Loader::Restarter;

plan skip_all => 'Flakey test: developer only' unless -e '.git';

my @return_bodies = ('Hi first', 'Hi second', 'Hi third');
my @restartertestfiles = ('t/restartertestfile1.pl', 't/restartertestfile2.pl');
unlink $_ for @restartertestfiles;

my $builder = sub {
    my $idx = 0;
    for my $file (@restartertestfiles) {
        $idx++ if -e $file;
    }

    my $return_body = $return_bodies[$idx];
    my $app = sub {
        return [ 200, [], [ $return_body ] ];
    };
};



test_tcp(
    client => sub {
        my $port = shift;
        my $ua = LWP::UserAgent->new;
        my $cb = sub {
            my $req = HTTP::Request->new(GET => sprintf('http://127.0.0.1:%s/', $port));
            return $ua->request($req);
        };

        is $cb->()->content, $return_bodies[0];

        touch($restartertestfiles[0]);
        sleep 2;
        wait_port($port);

        is $cb->()->content, $return_bodies[1];

        touch($restartertestfiles[1]);
        sleep 2;
        wait_port($port);

        is $cb->()->content, $return_bodies[2];
    },
    server => sub {
        my $port = shift;

        my $loader = Plack::Loader::Restarter->new;
        my $server = $loader->auto(port => $port);
        $loader->preload_app($builder);
        $loader->watch('t');
        $loader->run($server);
    },
);

sub touch {
    my $file = shift;
    open my $fh, ">", $file or die $!;
    print $fh time;
    close $fh;
}

unlink $_ for @restartertestfiles;

done_testing;