"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/40_basic.t" (25 Jan 2023, 9574 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 => 35;
   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 my $zkh = Net::ZooKeeper->new($hosts);
   33 my $path;
   34 
   35 SKIP: {
   36     my $ret = $zkh->exists($root_path) if (defined($zkh));
   37 
   38     skip 'no connection to ZooKeeper', 1 unless
   39         (defined($ret) and $ret);
   40 
   41     $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE);
   42     is($path, $node_path,
   43        'create(): created node');
   44 }
   45 
   46 SKIP: {
   47     skip 'no connection to ZooKeeper', 21 unless
   48         (defined($path) and $path eq $node_path);
   49 
   50 
   51     ## exists()
   52 
   53     my $ret = $zkh->exists($node_path);
   54     ok($ret,
   55        'exists(): checked node existence');
   56 
   57     $ret = $zkh->exists($node_path . '/NONE');
   58     ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
   59        'exists(): checked node non-existence');
   60 
   61     my $stat = $zkh->stat();
   62 
   63     $ret = $zkh->exists($node_path, 'stat' => $stat);
   64     ok(($ret and $stat->{'data_len'} == 3),
   65        'exists(): checked node existence with stat handle');
   66 
   67 
   68     ## get()
   69 
   70     my $node = $zkh->get($node_path);
   71     is($node, 'foo',
   72        'get(): retrieved node value');
   73 
   74     $node = $zkh->get($node_path . '/NONE');
   75     ok((!defined($node) and $zkh->get_error() == ZNONODE and $! eq ''),
   76        'get(): undef returned for non-extant node');
   77 
   78     $node = $zkh->get($node_path, 'data_read_len', 2);
   79     is($node, 'fo',
   80        'get(): retrieved truncated node value');
   81 
   82     $node = $zkh->get($node_path, 'data_read_len' => 0);
   83     is($node, '',
   84        'get(): retrieved zero-length node value');
   85 
   86     $node = $zkh->get($node_path, 'stat' => $stat);
   87     ok(($node eq 'foo' and $stat->{'data_len'} == 3),
   88        'get(): retrieved node value with stat handle');
   89 
   90 
   91     ## set()
   92 
   93     $ret = $zkh->set($node_path, 'foo');
   94     ok($ret,
   95        'set(): set node value');
   96 
   97     SKIP: {
   98         my $ret = $zkh->exists($node_path, 'stat' => $stat);
   99 
  100         skip 'invalid node data', 2 unless
  101             ($ret and $stat->{'version'} == 1);
  102 
  103         $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'});
  104         ok($ret,
  105            'set(): set node value with matching version');
  106 
  107         $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'});
  108         ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  109            'set(): node value unchanged if non-matching version');
  110     }
  111 
  112     $ret = $zkh->set($node_path, 'foobaz', 'stat' => $stat);
  113     ok(($ret and $stat->{'data_len'} == 6),
  114        'set(): retrieved node value with stat handle');
  115 
  116 
  117     ## create(), delete()
  118 
  119     $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE);
  120     ok((!defined($path) and $zkh->get_error() == ZNODEEXISTS and $! eq ''),
  121        'create(): undef when attempting to create extant node');
  122 
  123     $ret = $zkh->delete($node_path . '/NONE');
  124     ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  125        'delete(): no deletion of non-extant node');
  126 
  127     $ret = $zkh->delete($node_path);
  128     ok($ret,
  129        'delete(): deleted node');
  130 
  131     my $path_read_len = length($node_path) - 2;
  132 
  133     $path = $zkh->create($node_path, 'foo',
  134                          'path_read_len' => $path_read_len,
  135                          'acl' => ZOO_OPEN_ACL_UNSAFE);
  136     is($path, substr($node_path, 0, -2),
  137        'create(): created node with small return path buffer');
  138 
  139     $path = $zkh->create("$node_path/s", 'foo',
  140                          'flags' => ZOO_SEQUENCE,
  141                          'acl' => ZOO_OPEN_ACL_UNSAFE);
  142     like($path, qr/^$node_path\/s[0-9]+$/,
  143        'create(): created sequential node');
  144 
  145     SKIP: {
  146         my $ret = $zkh->exists($path, 'stat' => $stat);
  147 
  148         unless ($ret and $stat->{'version'} == 0) {
  149             my $ret = $zkh->delete($path);
  150             diag(sprintf('unable to delete node %s: %d, %s',
  151                          $path, $zkh->get_error(), $!)) unless ($ret);
  152 
  153             skip 'invalid node data', 2;
  154         }
  155 
  156         $ret = $zkh->delete($path, 'version' => ($stat->{'version'} + 1));
  157         ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  158            'delete(): node not deleted if non-matching version');
  159 
  160         $ret = $zkh->delete($path, 'version' => $stat->{'version'});
  161         ok($ret,
  162            'delete(): deleted sequential node with matching version');
  163     }
  164 
  165     $path = $zkh->create("$node_path/e", 'foo',
  166                          'flags' => ZOO_EPHEMERAL,
  167                          'acl' => ZOO_OPEN_ACL_UNSAFE);
  168     is($path, "$node_path/e",
  169        'create(): created ephemeral node');
  170 
  171     $path = $zkh->create("$node_path/es", 'foo',
  172                          'flags' => (ZOO_SEQUENCE | ZOO_EPHEMERAL),
  173                          'acl' => ZOO_OPEN_ACL_UNSAFE);
  174     like($path, qr/^$node_path\/es[0-9]+$/,
  175        'create(): created ephemeral sequential node');
  176 
  177     undef $zkh;
  178 }
  179 
  180 $zkh = Net::ZooKeeper->new($hosts);
  181 
  182 SKIP: {
  183     my $ret = $zkh->exists($node_path) if (defined($zkh));
  184 
  185     skip 'no connection to ZooKeeper', 12 unless
  186         (defined($ret) and $ret);
  187 
  188     $ret = $zkh->exists("$node_path/e");
  189     ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  190        'exists(): checked ephemeral node non-extant after reconnection');
  191 
  192     $ret = $zkh->exists($path);
  193     ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  194        'exists(): checked ephemeral sequential node non-extant ' .
  195        'after reconnection');
  196 
  197 
  198     ## get_children()
  199 
  200     my @child_paths = ('abc');
  201     @child_paths = $zkh->get_children($node_path);
  202     ok((@child_paths == 0 and $zkh->get_error() == ZOK),
  203        'get_children(): retrieved empty list of child nodes');
  204 
  205     my $num_children = $zkh->get_children($node_path);
  206     ok((defined($num_children) and $num_children == 0),
  207        'get_children(): retrieved zero count of child nodes');
  208 
  209     @child_paths = $zkh->get_children($node_path . '/NONE');
  210     ok((@child_paths == 0 and $zkh->get_error() == ZNONODE and $! eq ''),
  211        'get_children(): empty list returned for non-extant node');
  212 
  213     $num_children = $zkh->get_children($node_path . '/NONE');
  214     ok((!defined($num_children) and $zkh->get_error() == ZNONODE and $! eq ''),
  215        'get_children(): undef returned for non-extant node');
  216 
  217     SKIP: {
  218         my $path = $zkh->create("$node_path/c1", 'foo',
  219                                 'acl' => ZOO_OPEN_ACL_UNSAFE);
  220 
  221         skip 'no connection to ZooKeeper', 6 unless
  222             (defined($path) and $path eq "$node_path/c1");
  223 
  224         my @child_paths = ('abc');
  225         @child_paths = $zkh->get_children($node_path);
  226         ok((@child_paths == 1 and $child_paths[0] eq 'c1'),
  227            'get_children(): retrieved list of single child node');
  228 
  229         my $num_children = $zkh->get_children($node_path);
  230         ok((defined($num_children) and $num_children == 1),
  231            'get_children(): retrieved count of single child node');
  232 
  233         SKIP: {
  234             my $path = $zkh->create("$node_path/c2", 'foo',
  235                                     'acl' => ZOO_OPEN_ACL_UNSAFE);
  236 
  237             skip 'no connection to ZooKeeper', 2 unless
  238                 (defined($path) and $path eq "$node_path/c2");
  239 
  240             my @child_paths = ('abc');
  241             @child_paths = $zkh->get_children($node_path);
  242             ok((@child_paths == 2 and $child_paths[0] eq 'c1' and
  243                 $child_paths[1] eq 'c2'),
  244                'get_children(): retrieved list of two child nodes');
  245 
  246             my $num_children = $zkh->get_children($node_path);
  247             ok((defined($num_children) and $num_children == 2),
  248                'get_children(): retrieved count of two child nodes');
  249 
  250             my $ret = $zkh->delete("$node_path/c2");
  251             diag(sprintf('unable to delete node %s: %d, %s',
  252                          "$node_path/c2", $zkh->get_error(), $!)) unless
  253                 ($ret);
  254         }
  255 
  256         @child_paths = ('abc');
  257         @child_paths = $zkh->get_children($node_path);
  258         ok((@child_paths == 1 and $child_paths[0] eq 'c1'),
  259            'get_children(): retrieved list of single child node');
  260 
  261         $num_children = $zkh->get_children($node_path);
  262         ok((defined($num_children) and $num_children == 1),
  263            'get_children(): retrieved count of single child node');
  264 
  265         my $ret = $zkh->delete("$node_path/c1");
  266         diag(sprintf('unable to delete node %s: %d, %s',
  267                      "$node_path/c1", $zkh->get_error(), $!)) unless ($ret);
  268     }
  269 
  270 
  271     ## cleanup
  272 
  273     $ret = $zkh->delete($node_path);
  274     diag(sprintf('unable to delete node %s: %d, %s',
  275                  $node_path, $zkh->get_error(), $!)) unless ($ret);
  276 }
  277