memcached.t 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Test for memcached backend.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. BEGIN { use FindBin; chdir($FindBin::Bin); }
  9. use lib 'lib';
  10. use Test::Nginx;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. eval { require Cache::Memcached; };
  15. plan(skip_all => 'Cache::Memcached not installed') if $@;
  16. my $t = Test::Nginx->new()->has(qw/http rewrite memcached/)
  17. ->has_daemon('memcached')->plan(4)
  18. ->write_file_expand('nginx.conf', <<'EOF');
  19. %%TEST_GLOBALS%%
  20. master_process off;
  21. daemon off;
  22. events {
  23. }
  24. http {
  25. %%TEST_GLOBALS_HTTP%%
  26. server {
  27. listen 127.0.0.1:8080;
  28. server_name localhost;
  29. location / {
  30. set $memcached_key $uri;
  31. memcached_pass 127.0.0.1:8081;
  32. }
  33. location /next {
  34. set $memcached_key $uri;
  35. memcached_next_upstream not_found;
  36. memcached_pass 127.0.0.1:8081;
  37. }
  38. }
  39. }
  40. EOF
  41. my $memhelp = `memcached -h`;
  42. my @memopts = ();
  43. if ($memhelp =~ /repcached/) {
  44. # repcached patch adds additional listen socket
  45. push @memopts, '-X', '8082';
  46. }
  47. if ($memhelp =~ /-U/) {
  48. # UDP port is on by default in memcached 1.2.7+
  49. push @memopts, '-U', '0';
  50. }
  51. $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts);
  52. $t->run();
  53. $t->waitforsocket('127.0.0.1:8081')
  54. or die "Can't start memcached";
  55. ###############################################################################
  56. my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
  57. $memd->set('/', 'SEE-THIS')
  58. or die "can't put value into memcached: $!";
  59. like(http_get('/'), qr/SEE-THIS/, 'memcached request');
  60. like(http_get('/notfound'), qr/404/, 'memcached not found');
  61. like(http_get('/next'), qr/404/, 'not found with memcached_next_upstream');
  62. unlike(http_head('/'), qr/SEE-THIS/, 'memcached no data in HEAD');
  63. ###############################################################################