"Fossies" - the Fresh Open Source Software Archive

Member "memcached-1.6.9/t/restart.t" (21 Nov 2020, 4205 Bytes) of package /linux/www/memcached-1.6.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "restart.t": 1.6.8_vs_1.6.9.

    1 #!/usr/bin/perl
    2 
    3 use strict;
    4 use warnings;
    5 use Test::More;
    6 use FindBin qw($Bin);
    7 use lib "$Bin/lib";
    8 use MemcachedTest;
    9 
   10 # NOTE: Do not use this feature on top of a filesystem, please use a ram disk!
   11 # These tests use /tmp/ as some systems do not have or have a weirdly small
   12 # /dev/shm.
   13 my $mem_path = "/tmp/mc_restart.$$";
   14 
   15 # read a invalid metadata file
   16 {
   17     my $meta_path = "$mem_path.meta";
   18     open(my $f, "> $meta_path") || die("Can't open a metadata file.");
   19     eval {  new_memcached("-e $mem_path"); };
   20     unlink($meta_path);
   21     ok($@, "Died with an empty metadata file");
   22 }
   23 
   24 my $server = new_memcached("-m 128 -e $mem_path -I 2m");
   25 my $sock = $server->sock;
   26 
   27 diag "restart basic stats";
   28 {
   29     my $stats = mem_stats($server->sock, ' settings');
   30     is($stats->{memory_file}, $mem_path);
   31 }
   32 
   33 diag "Set some values, various sizes.";
   34 {
   35     my $cur = 2;
   36     my $cnt = 0;
   37     my $end = 2**20;
   38     while ($cur <= $end) {
   39         my $val = 'x' x $cur;
   40         print $sock "set foo${cnt} 0 0 $cur\r\n$val\r\n";
   41         like(scalar <$sock>, qr/STORED/, "stored $cur size item");
   42         $cur *= 2;
   43         $cnt++;
   44     }
   45 }
   46 
   47 diag "load enough items to change hash power level";
   48 {
   49     my $stats = mem_stats($sock);
   50     is($stats->{hash_power_level}, 16, "starting hash level is 16");
   51     my $todo = 2**17;
   52     my $good = 1;
   53     while ($todo--) {
   54         print $sock "set z${todo} 0 0 0\r\n\r\n";
   55         my $res = <$sock>;
   56         $good = 0 if ($res !~ m/STORED/);
   57     }
   58 
   59     is($good, 1, "set responses were all STORED");
   60     sleep 3; # sigh.
   61     $stats = mem_stats($sock);
   62     is($stats->{hash_power_level}, 17, "new hash power level is 17");
   63 
   64     # Now delete all these items, so the auto-restore won't cause the hash
   65     # table to re-inflate, but the restart code should restore the hash table
   66     # to where it was regardless.
   67     $todo = 2**17;
   68     $good = 1;
   69     while ($todo--) {
   70         print $sock "delete z${todo}\r\n";
   71         my $res = <$sock>;
   72         $good = 0 if ($res !~ m/DELETED/);
   73     }
   74     is($good, 1, "delete responses were all DELETED");
   75 }
   76 
   77 diag "Load a couple chunked items";
   78 my $deleted_chunked_item = 0;
   79 {
   80     my $cur = 768000;
   81     my $cnt = 0;
   82     my $end = $cur + 1024;
   83     while ($cur <= $end) {
   84         my $val = 'x' x $cur;
   85         print $sock "set chunk${cnt} 0 0 $cur\r\n$val\r\n";
   86         like(scalar <$sock>, qr/STORED/, "stored $cur size item");
   87         $cur += 50;
   88         $cnt++;
   89     }
   90     # delete the last one.
   91     $cnt--;
   92     $deleted_chunked_item = $cnt;
   93     print $sock "delete chunk${cnt}\r\n";
   94     like(scalar <$sock>, qr/DELETED/, "deleted $cnt from large chunked items");
   95 }
   96 
   97 diag "Data that should expire while stopped.";
   98 {
   99     print $sock "set low1 0 5 2\r\nbo\r\n";
  100     like(scalar <$sock>, qr/STORED/, "stored low ttl item");
  101     # This one should stay.
  102     print $sock "set low2 0 20 2\r\nmo\r\n";
  103     like(scalar <$sock>, qr/STORED/, "stored low ttl item");
  104 }
  105 
  106 # make sure it's okay to stop with a logger watcher enabled.
  107 {
  108     my $wsock = $server->new_sock;
  109     print $wsock "watch fetchers mutations\n";
  110     my $res = <$wsock>;
  111     is($res, "OK\r\n", "watcher enabled");
  112 }
  113 
  114 $server->graceful_stop();
  115 diag "killed, waiting";
  116 # TODO: add way to wait for server to fully exit..
  117 sleep 10;
  118 
  119 {
  120     $server = new_memcached("-m 128 -e $mem_path -I 2m");
  121     $sock = $server->sock;
  122     diag "reconnected";
  123 
  124     my $stats = mem_stats($sock);
  125     is($stats->{hash_power_level}, 17, "restarted hash power level is 17");
  126 }
  127 
  128 diag "low TTL item should be gone";
  129 {
  130     mem_get_is($sock, 'low1', undef);
  131     # but this one should exist.
  132     mem_get_is($sock, 'low2', 'mo');
  133 }
  134 
  135 # initially inserted items.
  136 {
  137     my $cur = 2;
  138     my $cnt = 0;
  139     my $end = 1000000;
  140     while ($cur < $end) {
  141         my $val = 'x' x $cur;
  142         mem_get_is($sock, 'foo' . $cnt, $val);
  143         $cur *= 2;
  144         $cnt++;
  145     }
  146 }
  147 
  148 # chunked items.
  149 {
  150     my $cur = 768000;
  151     my $cnt = 0;
  152     my $end = $cur + 1024;
  153     while ($cur <= $end) {
  154         my $val = 'x' x $cur;
  155         if ($cnt != $deleted_chunked_item) {
  156             mem_get_is($sock, 'chunk' . $cnt, $val);
  157         }
  158         $cur += 50;
  159         $cnt++;
  160     }
  161 }
  162 
  163 done_testing();
  164 
  165 END {
  166     unlink $mem_path if $mem_path;
  167 }