summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/t/50server-starter.t
blob: 9be37d378e92b163d47ced12ee945ace5407e658 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
use File::Temp qw(tempdir);
use Test::More;
use t::Util;

plan skip_all => 'start_server not found'
    unless prog_exists('start_server');

my $tempdir = tempdir(CLEANUP => 1);

subtest "master-mode" => sub {
    my $server = spawn_h2o({
        opts => [ qw(--mode=master) ],
        conf => << "EOT",
pid-file: $tempdir/h2o.pid
hosts:
  default:
    paths:
      /:
        file.dir: @{[ DOC_ROOT ]}
EOT
    });

    subtest 'before-HUP' => sub {
        is read_file("$tempdir/h2o.pid"), "$server->{pid}\n", "pid";
        fetch_test($server->{port}, $server->{tls_port});
    };
    kill 'HUP', $server->{pid};
    sleep 1;
    subtest 'after-HUP' => sub {
        fetch_test($server->{port}, $server->{tls_port});
        is read_file("$tempdir/h2o.pid"), "$server->{pid}\n", "pid unchanged";
    };

    undef $server;

    ok ! stat("$tempdir/h2o.pid"), "pid-file is unlinked";
};

subtest "daemon-mode" => sub {
    my $server = spawn_h2o({
        opts => [ qw(--mode=daemon) ],
        conf => << "EOT",
pid-file: $tempdir/h2o.pid
error-log: $tempdir/h2o.error
hosts:
  default:
    paths:
      /:
        file.dir: @{[ DOC_ROOT ]}
EOT
    });
    my ($port, $tls_port) = map { $server->{$_} } qw(port tls_port);

    sleep 1;
    undef $server; # should have performed a double-fork by now

    my $pid = read_file("$tempdir/h2o.pid");
    chomp $pid;

    subtest 'before-HUP' => sub {
        fetch_test($port, $tls_port);
    };
    kill 'HUP', $pid;
    sleep 1;
    subtest 'after-HUP' => sub {
        fetch_test($port, $tls_port);
        is read_file("$tempdir/h2o.pid"), "$pid\n", "pid unchanged";
    };

    kill 'TERM', $pid;
    sleep 1;
    ok ! stat("$tempdir/h2o.pid"), "pid-file is unlinked";
};

done_testing;

sub fetch_test {
    my ($port, $tls_port) = @_;

    plan skip_all => 'curl not found'
        unless prog_exists('curl');
    my $curl = "curl --insecure";
    $curl .= " --http1.1"
        if curl_supports_http2();

    my $doit = sub {
        my ($proto, $port) = @_;
        my $content = `$curl --silent --show-error $proto://127.0.0.1:$port/`;
        is md5_hex($content), md5_file(DOC_ROOT . "/index.txt"), $proto;
    };
    $doit->("http", $port);
    $doit->("https", $tls_port);
}

sub read_file {
    my $fn = shift;
    open my $fh, '<', $fn
        or die "failed to open file:$fn:$!";
    join '', <$fh>;
}