"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/60_watch.t" (25 Jan 2023, 9124 Bytes) of package /linux/misc/apache-zookeeper-3.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Net::ZooKeeper - Perl extension for Apache ZooKeeper
    2 #
    3 # Licensed to the Apache Software Foundation (ASF) under one
    4 # or more contributor license agreements.  See the NOTICE file
    5 # distributed with this work for additional information
    6 # regarding copyright ownership.  The ASF licenses this file
    7 # to you under the Apache License, Version 2.0 (the
    8 # "License"); you may not use this file except in compliance
    9 # with the License.  You may obtain a copy of the License at
   10 #
   11 #   http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 # Unless required by applicable law or agreed to in writing, software
   14 # distributed under the License is distributed on an "AS IS" BASIS,
   15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16 # See the License for the specific language governing permissions and
   17 # limitations under the License.
   18 
   19 use File::Spec;
   20 use Test::More tests => 30;
   21 
   22 BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
   23 
   24 
   25 my $test_dir;
   26 (undef, $test_dir, undef) = File::Spec->splitpath($0);
   27 require File::Spec->catfile($test_dir, 'util.pl');
   28 
   29 my($hosts, $root_path, $node_path) = zk_test_setup(0);
   30 
   31 
   32 SKIP: {
   33     my $zkh = Net::ZooKeeper->new($hosts);
   34 
   35     my $path = $zkh->create($node_path, 'foo',
   36                             'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh));
   37 
   38     skip 'no connection to ZooKeeper', 20 unless
   39         (defined($path) and $path eq $node_path);
   40 
   41 
   42     ## exists()
   43 
   44     $zkh->{'watch_timeout'} = 100;
   45 
   46     my $watch = $zkh->watch();
   47 
   48     my $ret = $zkh->exists($node_path, 'watch' => $watch);
   49     ok($ret,
   50        'exists(): checked node existence with watch handle');
   51 
   52     $ret = $watch->wait();
   53     ok(!$ret,
   54        'wait(): watch after checking node existence timed out');
   55 
   56     $ret = $zkh->exists($node_path, 'watch' => $watch);
   57     ok($ret,
   58        'exists(): checked node existence with renewed watch handle');
   59 
   60     $ret = $watch->wait();
   61     ok(!$ret,
   62        'wait(): watch after checking node existence timed out with ' .
   63        'renewed watch handle');
   64 
   65     undef $watch;
   66     ok(!defined($watch),
   67        'undef: released watch handle');
   68 
   69     my $pending_watches = $zkh->{'pending_watches'};
   70     is($pending_watches, 2,
   71        '_zk_release_watches(): report pending watches');
   72 
   73 
   74     ## get_children()
   75 
   76     $watch = $zkh->watch('timeout' => 50);
   77 
   78     my $num_children = $zkh->get_children($node_path, 'watch' => $watch);
   79     ok((defined($num_children) and $num_children == 0),
   80        'get_children(): retrieved zero count of child nodes with ' .
   81        'watch handle');
   82 
   83     $ret = $watch->wait();
   84     ok(!$ret,
   85        'wait(): watch after retrieving child nodes timed out with ' .
   86        'watch handle');
   87 
   88     $watch->{'timeout'} = 100;
   89 
   90     my @child_paths = $zkh->get_children($node_path, 'watch' => $watch);
   91     ok((@child_paths == 0),
   92        'get_children(): retrieved empty list of child nodes with ' .
   93        'renewed watch handle');
   94 
   95     $ret = $watch->wait();
   96     ok(!$ret,
   97        'wait(): watch after retrieving child nodes timed out with ' .
   98        'renewed watch handle');
   99 
  100     $pending_watches = $zkh->{'pending_watches'};
  101     is($pending_watches, 4,
  102        '_zk_release_watches(): report pending watches');
  103 
  104 
  105     ## get()
  106 
  107     $watch = $zkh->watch();
  108 
  109     my $node = $zkh->get($node_path, 'watch' => $watch);
  110     is($node, 'foo',
  111        'get(): retrieved node value with watch handle');
  112 
  113     $ret = $watch->wait('timeout' => 0);
  114     ok(!$ret,
  115        'wait(): watch after retrieving node value timed out with ' .
  116        'watch handle');
  117 
  118     $node = $zkh->get($node_path, 'watch' => $watch);
  119     is($node, 'foo',
  120        'get(): retrieved node value with renewed watch handle');
  121 
  122     $ret = $watch->wait();
  123     ok(!$ret,
  124        'wait(): watch after retrieving node value timed out with ' .
  125        'renewed watch handle');
  126 
  127     $pending_watches = $zkh->{'pending_watches'};
  128     is($pending_watches, 6,
  129        '_zk_release_watches(): all watches pending');
  130 
  131 
  132     ## _zk_release_watches()
  133 
  134     $ret = $zkh->DESTROY();
  135     ok($ret,
  136        'DESTROY(): destroyed handle with pending watches');
  137 
  138     my $event = $watch->{'event'};
  139     is($event, 0,
  140        '_zk_release_watches(): watch not destroyed when tied to watch handle');
  141 
  142     $zkh = Net::ZooKeeper->new($hosts);
  143 
  144     SKIP: {
  145         my $ret = $zkh->exists($node_path, 'watch' => $watch);
  146 
  147         skip 'no connection to ZooKeeper', 2 unless
  148             (defined($ret) and $ret);
  149 
  150         ok($ret,
  151            'exists(): checked node existence with renewed watch handle ' .
  152            'from prior connection');
  153 
  154         $ret = $watch->wait();
  155         ok(!$ret,
  156            'wait(): watch after checking node existence timed out with ' .
  157            'renewed watch handle from prior connection');
  158 
  159 
  160     }
  161 }
  162 
  163 my $pid = fork();
  164 
  165 SKIP: {
  166     skip 'unable to fork', 4 unless (defined($pid));
  167 
  168     my $zkh = Net::ZooKeeper->new($hosts);
  169 
  170     my $ret = $zkh->exists($node_path) if (defined($zkh));
  171 
  172     if ($pid == 0) {
  173         ## child process
  174 
  175         my $code = 0;
  176 
  177         if (defined($ret) and $ret) {
  178             sleep(1);
  179 
  180             my $ret = $zkh->set($node_path, 'foo');
  181 
  182             diag(sprintf('set(): failed in child process: %d, %s',
  183                          $zkh->get_error(), $!)) unless ($ret);
  184 
  185             $code = !$ret;
  186 
  187             sleep(1);
  188 
  189             my $path = $zkh->create("$node_path/c", 'foo',
  190                                     'acl' => ZOO_OPEN_ACL_UNSAFE);
  191 
  192             diag(sprintf('create(): failed in child process: %d, %s',
  193                          $zkh->get_error(), $!)) unless
  194                 (defined($path) and $path eq "$node_path/c");
  195 
  196             $code &= !$ret;
  197 
  198             sleep(1);
  199 
  200             $ret = $zkh->delete("$node_path/c");
  201 
  202             diag(sprintf('delete(): failed in child process: %d, %s',
  203                          $zkh->get_error(), $!)) unless ($ret);
  204 
  205             $code &= !$ret;
  206 
  207             sleep(1);
  208 
  209             $ret = $zkh->set($node_path, 'foo');
  210 
  211             diag(sprintf('set(): failed in child process: %d, %s',
  212                          $zkh->get_error(), $!)) unless ($ret);
  213 
  214             $code &= !$ret;
  215         }
  216 
  217         exit($code);
  218     }
  219     else {
  220         ## parent process
  221 
  222         SKIP: {
  223             skip 'no connection to ZooKeeper', 9 unless
  224                 (defined($ret) and $ret);
  225 
  226             my $watch = $zkh->watch('timeout' => 5000);
  227 
  228 
  229             ## wait()
  230 
  231             my $ret = $zkh->exists($node_path, 'watch' => $watch);
  232             ok($ret,
  233                'exists(): checked node existence with watch handle ' .
  234                'in parent');
  235 
  236             $ret = $watch->wait();
  237             ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and
  238                 $watch->{'state'} == ZOO_CONNECTED_STATE),
  239                'wait(): waited for event after checking node existence');
  240 
  241             my $num_children = $zkh->get_children($node_path,
  242                                                  'watch' => $watch);
  243             ok((defined($num_children) and $num_children == 0),
  244                'get_children(): retrieved zero count of child nodes with ' .
  245                'watch handle in parent');
  246 
  247             $ret = $watch->wait();
  248             ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and
  249                 $watch->{'state'} == ZOO_CONNECTED_STATE),
  250                'wait(): waited for create child event after ' .
  251                'retrieving child nodes');
  252 
  253             my @child_paths = $zkh->get_children($node_path,
  254                                                 'watch' => $watch);
  255             ok((@child_paths == 1 and $child_paths[0] eq 'c'),
  256                'get_children(): retrieved list of child nodes with ' .
  257                'watch handle in parent');
  258 
  259             $ret = $watch->wait();
  260             ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and
  261                 $watch->{'state'} == ZOO_CONNECTED_STATE),
  262                'wait(): waited for delete child event after ' .
  263                'retrieving child nodes');
  264 
  265             my $node = $zkh->get($node_path, 'watch' => $watch);
  266             is($node, 'foo',
  267                'get(): retrieved node value with watch handle in parent');
  268 
  269             $ret = $watch->wait();
  270             ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and
  271                 $watch->{'state'} == ZOO_CONNECTED_STATE),
  272                'wait(): waited for event after retrieving node value');
  273 
  274             undef $watch;
  275 
  276             my $pending_watches = $zkh->{'pending_watches'};
  277             is($pending_watches, 0,
  278                '_zk_release_watches(): no watches pending');
  279         }
  280 
  281         my $reap = waitpid($pid, 0);
  282 
  283         diag(sprintf('child process failed: exit %d, signal %d%s',
  284                      ($? >> 8), ($? & 127),
  285                      (($? & 128) ? ', core dump' : ''))) if
  286             ($reap == $pid and $? != 0);
  287     }
  288 }
  289 
  290 
  291 ## cleanup
  292 
  293 {
  294     my $zkh = Net::ZooKeeper->new($hosts);
  295 
  296     my $ret = $zkh->exists($node_path) if (defined($zkh));
  297 
  298     if (defined($ret) and $ret) {
  299         $ret = $zkh->delete($node_path);
  300         diag(sprintf('unable to delete node %s: %d, %s',
  301                      $node_path, $zkh->get_error(), $!)) unless ($ret);
  302     }
  303 }
  304