comparison stream_split_clients.t @ 979:ef6be3201851

Tests: basic tests for stream split_clients module.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 13 Jul 2016 13:25:33 +0300
parents
children 3fc6817cd84a
comparison
equal deleted inserted replaced
978:04cb1849005a 979:ef6be3201851
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for stream split_client module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::Stream qw/ stream /;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/stream stream_split_clients stream_return/);
27
28 $t->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 stream {
38 split_clients $connection $variant {
39 51.2% "first";
40 10% "second";
41 * "third";
42 }
43
44 server {
45 listen 127.0.0.1:8080;
46 return $variant;
47 }
48 }
49
50 EOF
51
52 $t->try_run('no stream split_clients');
53 $t->plan(1);
54
55 ###############################################################################
56
57 # NB: split_clients distribution is a subject to implementation details
58
59 like(many('/', 20), qr/first: 12, second: 2, third: 6/, 'split');
60
61 ###############################################################################
62
63 sub many {
64 my ($uri, $count) = @_;
65 my %dist;
66
67 for (1 .. $count) {
68 if (my $data = stream()->read()) {
69 $dist{$data} = 0 unless defined $data;
70 $dist{$data}++;
71 }
72 }
73
74 return join ', ', map { $_ . ": " . $dist{$_} } sort keys %dist;
75 }
76
77 ###############################################################################