Mercurial > hg > nginx-tests
comparison memcached_keepalive_stale.t @ 250:0c9f15938545
Tests: upstream keepalive tests imported.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 24 Jan 2013 02:00:03 +0400 |
parents | |
children | 6cbcfb4ef7ee |
comparison
equal
deleted
inserted
replaced
249:6a0d934950bc | 250:0c9f15938545 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Test for stale events handling in upstream keepalive. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 eval { require Cache::Memcached; }; | |
25 plan(skip_all => 'Cache::Memcached not installed') if $@; | |
26 | |
27 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(2) | |
28 ->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 worker_processes 2; | |
35 | |
36 events { | |
37 } | |
38 | |
39 http { | |
40 %%TEST_GLOBALS_HTTP%% | |
41 | |
42 upstream memd { | |
43 server 127.0.0.1:8081; | |
44 keepalive 1; | |
45 } | |
46 | |
47 server { | |
48 listen 127.0.0.1:8080 sndbuf=32k; | |
49 server_name localhost; | |
50 | |
51 location / { | |
52 set $memcached_key $uri; | |
53 memcached_pass memd; | |
54 } | |
55 } | |
56 } | |
57 | |
58 EOF | |
59 | |
60 my $memhelp = `memcached -h`; | |
61 my @memopts1 = (); | |
62 | |
63 if ($memhelp =~ /repcached/) { | |
64 # repcached patches adds additional listen socket memcached | |
65 # that should be different too | |
66 | |
67 push @memopts1, '-X', '8091'; | |
68 } | |
69 if ($memhelp =~ /-U/) { | |
70 # UDP ports no longer off by default in memcached 1.2.7+ | |
71 | |
72 push @memopts1, '-U', '0'; | |
73 } | |
74 | |
75 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts1); | |
76 | |
77 $t->run(); | |
78 | |
79 $t->waitforsocket('127.0.0.1:8081') | |
80 or die "Unable to start memcached"; | |
81 | |
82 ############################################################################### | |
83 | |
84 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]); | |
85 | |
86 # It's possible that stale events occur, i.e. read event handler called | |
87 # for just saved upstream connection without any data available for | |
88 # read. We shouldn't close upstream connection in such situation. | |
89 # | |
90 # This happens due to reading from upstream connection on downstream write | |
91 # events. More likely to happen with multiple workers due to use of posted | |
92 # events. | |
93 # | |
94 # Stale event may only happen if reading response from upstream requires | |
95 # entering event loop, i.e. response should be big enough. On the other | |
96 # hand, it is less likely to occur with full client's connection output | |
97 # buffer. | |
98 # | |
99 # We use here 2 workers, 20k response and set output buffer on clients | |
100 # connection to 32k. This allows more or less reliably reproduce stale | |
101 # events at least on FreeBSD testbed here. | |
102 | |
103 $memd1->set('/big', 'X' x 20480); | |
104 | |
105 my $total = $memd1->stats()->{total}->{total_connections}; | |
106 | |
107 for (1 .. 100) { | |
108 http_get('/big'); | |
109 } | |
110 | |
111 cmp_ok($memd1->stats()->{total}->{total_connections}, '<=', $total + 2, | |
112 'only one connection per worker used'); | |
113 | |
114 $t->stop(); | |
115 | |
116 like(`grep -F '[alert]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no alerts'); | |
117 | |
118 ############################################################################### |