File: 300_robust.t

package info (click to toggle)
libyahc-perl 0.035-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 440 kB
  • sloc: perl: 3,661; makefile: 2
file content (235 lines) | stat: -rw-r--r-- 7,480 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env perl

use strict;
use warnings;

use YAHC;
use POSIX;
use FindBin;
use HTTP::Tiny;
use Test::More;
use Data::Dumper;
use JSON qw/decode_json/;
use Time::HiRes qw/time sleep nanosleep/;
use List::Util qw/shuffle/;

use lib "$FindBin::Bin/..";
use t::Utils;

unless ($ENV{TEST_ROBUST}) {
    plan skip_all => "Enable robust testing by setting env: TEST_ROBUST=1";
}

unless (YAHC::SSL) {
    plan skip_all => 'IO::Socket::SSL 1.94+ required for this test!'
}

unless (t::Utils::_check_toxyproxy_and_reset()) {
    plan skip_all => 'Toxyproxy is not responsive, use $ENV{TOXYPROXY} to specify its address';
}

my $len = 50;
my $nrequests = 16;
note("generating $nrequests requests structures");
my @requests = map {
    $len *= 2;
    {
        path => '/record',
        body => t::Utils::_generate_sequence(int($len / 2) + int(rand($len))),
        ssl_options => {
            SSL_cert_file   => t::Utils::SSL_CRT,
            SSL_key_file    => t::Utils::SSL_KEY,
            SSL_verify_mode => 0, # SSL_VERIFY_NONE
        },
    }
} (1..$nrequests);

# [ $latency_ms, jitter_ms ]
# final latency = $latency +- rand($jitter)
my @latencies = ([10, 5], [50, 20], [100, 50], [500, 250]);
push @latencies, ([1000, 500], [10000, 5000]) if $ENV{TEST_ROBUST_LONG};

foreach my $proto ('http', 'https') {
    my $ssl = $proto eq 'https' ? 1 : 0;
    my ($spid, $chost, $cport) = t::Utils::_start_plack_server_on_random_port({ ssl => $ssl });
    my $caddr = "${chost}:${cport}";

    foreach my $settings (@latencies) {
        my ($latency, $jitter) = @{ $settings };
        subtest "robustness of $proto in case of latency $latency ms" => sub {
            my $addr = t::Utils::new_toxic(
                "yahc_robust_${proto}_latency_${latency}_jitter_${jitter}",
                $caddr,
                {
                    type        => 'latency',
                    attributes  => {
                        latency => int($latency),
                        jitter  => int($jitter),
                    },
                },
            );

            _do_requests_and_verify($addr, $ssl, @requests);
        }
    }

    note("killing web server $spid");
    delete t::Utils::_pids->{$spid};
    kill 'KILL', $spid;
}

# rate in kilobytes per second
my @rates = (100000, 10000, 1000);
push @rates, 100 if $ENV{TEST_ROBUST_LONG};

foreach my $proto ('http', 'https') {
    my $ssl = $proto eq 'https' ? 1 : 0;
    my ($spid, $chost, $cport) = t::Utils::_start_plack_server_on_random_port({ ssl => $ssl });
    my $caddr = "${chost}:${cport}";

    foreach my $rate (@rates) {
        subtest "robustness of $proto in case of $rate kilobytes per second bandwidth" => sub {
            my $addr = t::Utils::new_toxic(
                "yahc_robust_${proto}_bandwidth_${rate}",
                $caddr,
                {
                    type        => 'bandwidth',
                    attributes  => { rate => int($rate) },
                },
            );

            _do_requests_and_verify($addr, $ssl, @requests);
        }
    }

    note("killing web server $spid");
    delete t::Utils::_pids->{$spid};
    kill 'KILL', $spid;
}

my @signal_requests = shuffle (@requests, @requests, @requests,
                               @requests, @requests, @requests,
                               @requests, @requests, @requests);
my $nsignal_requests = scalar @signal_requests;

foreach my $proto ('http', 'https') {
    my $ssl = $proto eq 'https' ? 1 : 0;
    my ($spid, $chost, $cport) = t::Utils::_start_plack_server_on_random_port({ ssl => $ssl });
    my $caddr = "${chost}:${cport}";

    subtest "robustness of $proto in case of storm of signals" => sub {
        pipe(my $rh, my $wh) or die "failed to pipe: $!";

        my $ht = t::Utils::_get_http_tiny();
        ok($ht->get("$proto://$caddr/reset")->{success}, "reset server counters")
          or return;

        my $pid = t::Utils::_fork(sub {
            my $sigcnt = 0;
            local $SIG{HUP}  = 'IGNORE';
            local $SIG{USR1} = sub { $sigcnt++ };
            local $SIG{USR2} = sub { $sigcnt++ };

            my ($yahc, $yahc_storage) = YAHC->new({ account_for_signals => 1 });

            syswrite($wh, '1', 1);
            close($wh); # signal parent process
            close($rh);

            note("$$ client process start sending requests to $proto://$caddr");
            foreach my $request (@signal_requests) {
                my $c = $yahc->request({
                    host         => $caddr,
                    scheme       => $proto,
                    query_string => "sigcnt=$sigcnt",
                    %{ $request }
                });

                $yahc->run;

                die "we didn't get 200\n" unless ($c->{response}{status} || 0) == 200;
                die "body didn't match\n" unless ($c->{response}{body} || '') eq $request->{body};
            }

            note("$$ client process is done");
        });

        note("waiting for client to be ready");
        sysread($rh, my $b = '', 1);
        close($wh);
        close($rh);

        my $exit_code = 1024;
        my @signals = ('HUP', 'USR1', 'USR2', 'USR1', 'USR2' );
        note("start spaming client with signals " . join(',', @signals));

        my $t0 = time;
        while ($t0 + 10 >= time) {
            for (my $t1 = time; $t1 + 0.1 >= time;) {
                my $sig = $signals[int(rand(scalar @signals))];
                # note("send $sig to $pid");
                kill $sig, $pid;
                nanosleep 100000; # 100 microseconds
            }

            if (waitpid($pid, WNOHANG) != 0) {
                $exit_code = ($? >> 8);
                last;
            }
        }

        cmp_ok($exit_code, '==', 0, "client process exited with success")
            or return;

        note("analizing report");
        my $resp = $ht->get("$proto://$caddr/report");
        my @report = @{ decode_json($resp->{content} || '{}') };
        cmp_ok(scalar @report, '==', $nsignal_requests , "got $nsignal_requests reports");

        my $total_sigcnt = 0;
        my @report_body_lengths;
        my @body_length = map { length $_->{body} } @signal_requests;

        my $i = 1;
        foreach my $r (@report) {
            my $len = $r->{body_length} || 0;
            push @report_body_lengths, $len;
            my (undef, $sigcnt) = split(/=/, $r->{query_string} || '');
            $sigcnt ||= 0;
            note("client received $sigcnt signals during #$i request of $len bytes");
            $total_sigcnt += $sigcnt;
            $i++;
        }

        cmp_ok($total_sigcnt, '>', 0, "client process received signals");
        is_deeply(\@report_body_lengths, \@body_length, "bodies' length match");
    };

    note("killing web server $spid");
    delete t::Utils::_pids->{$spid};
    kill 'KILL', $spid;
}

sub _do_requests_and_verify {
    my ($addr, $ssl, @requests) = @_;
    my ($yahc, $yahc_storage) = YAHC->new;
    foreach my $request (@requests) {
        note(sprintf("request with body of %s bytes", length($request->{body})));

        my $c = $yahc->request({
            host   => $addr,
            scheme => $ssl ? 'https' : 'http',
            %{ $request }
        });
        $yahc->run;

        cmp_ok($c->{response}{status}, '==', 200, "We got 200 OK response");
        ok($c->{response}{body} eq $request->{body}, "Bodies match");
    }
}

END {
    kill 'KILL', $_ foreach keys %{ t::Utils::_pids() };
}

done_testing;