Sergey Kandaurov | 97ea6a2 | 2013-10-10 13:35:55 +0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Sergey Kandaurov |
| 4 | # (C) Nginx, Inc. |
| 5 | |
| 6 | # Tests for 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 | |
| 20 | ############################################################################### |
| 21 | |
| 22 | select STDERR; $| = 1; |
| 23 | select STDOUT; $| = 1; |
| 24 | |
| 25 | my $t = Test::Nginx->new()->has(qw/http split_clients/)->plan(1); |
| 26 | |
| 27 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 28 | |
| 29 | %%TEST_GLOBALS%% |
| 30 | |
| 31 | daemon off; |
| 32 | |
| 33 | events { |
| 34 | } |
| 35 | |
| 36 | http { |
| 37 | %%TEST_GLOBALS_HTTP%% |
| 38 | |
| 39 | split_clients $connection $variant { |
| 40 | 51.2% ".one"; |
| 41 | 10% ".two"; |
| 42 | * ".three"; |
| 43 | } |
| 44 | |
| 45 | server { |
Andrey Zelenkov | e59bf36 | 2016-07-12 17:39:03 +0300 | [diff] [blame] | 46 | listen 127.0.0.1:8080; |
Sergey Kandaurov | 97ea6a2 | 2013-10-10 13:35:55 +0400 | [diff] [blame] | 47 | server_name localhost; |
| 48 | |
| 49 | location / { |
| 50 | index index${variant}.html; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | EOF |
| 56 | |
| 57 | $t->write_file('index.one.html', 'first'); |
| 58 | $t->write_file('index.two.html', 'second'); |
| 59 | $t->write_file('index.three.html', 'third'); |
| 60 | |
| 61 | $t->run(); |
| 62 | |
| 63 | ############################################################################### |
| 64 | |
| 65 | # NB: split_clients distribution is a subject to implementation details |
| 66 | |
| 67 | like(many('/', 20), qr/first: 12, second: 2, third: 6/, 'split'); |
| 68 | |
| 69 | ############################################################################### |
| 70 | |
| 71 | sub many { |
| 72 | my ($uri, $count) = @_; |
| 73 | my %dist; |
| 74 | |
| 75 | for (1 .. $count) { |
| 76 | if (http_get($uri) =~ /(first|second|third)/) { |
| 77 | $dist{$1} = 0 unless defined $dist{$1}; |
| 78 | $dist{$1}++; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return join ', ', map { $_ . ": " . $dist{$_} } sort keys %dist; |
| 83 | } |
| 84 | |
| 85 | ############################################################################### |