"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/22_stat_tie.t" (25 Jan 2023, 11601 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 => 66;
   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     my $stat = $zkh->stat() if (defined($zkh));
   35 
   36     skip 'no valid stat handle', 4 unless (defined($stat));
   37 
   38 
   39     ## DESTROY()
   40 
   41     my $attr = tied(%{$stat});
   42 
   43     my $ret = $attr->DESTROY();
   44     ok($ret,
   45        'stat DESTROY(): destroyed inner stat hash');
   46 
   47     $ret = $attr->DESTROY();
   48     ok(!$ret,
   49        'stat DESTROY(): no action on destroyed inner stat hash');
   50 
   51     $ret = $stat->DESTROY();
   52     ok(!$ret,
   53        'stat DESTROY(): no action on stat handle with destroyed inner hash');
   54 
   55     undef $stat;
   56     ok(!defined($stat),
   57        'undef: released stat handle with destroyed inner hash');
   58 }
   59 
   60 SKIP: {
   61     my $zkh = Net::ZooKeeper->new($hosts);
   62     my $stat = $zkh->stat() if (defined($zkh));
   63 
   64     skip 'no valid stat handle', 61 unless (defined($stat));
   65 
   66 
   67     ## TIEHASH(), UNTIE()
   68 
   69     eval {
   70         tie(%{$stat}, 'Net::ZooKeeper::Stat');
   71     };
   72     like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/,
   73          'tie(): tying stat hashes not supported');
   74 
   75     eval {
   76         Net::ZooKeeper::Stat::TIEHASH('Net::ZooKeeper::Stat');
   77     };
   78     like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/,
   79          'stat TIEHASH(): tying stat hashes not supported');
   80 
   81     eval {
   82         untie(%{$stat});
   83     };
   84     like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
   85          'untie(): untying stat hashes not supported');
   86 
   87     my $attr = tied(%{$stat});
   88 
   89     eval {
   90         $attr->UNTIE(0);
   91     };
   92     like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
   93          'stat UNTIE(): untying stat hashes not supported');
   94 
   95 
   96     ## FIRSTKEY(), NEXTKEY(), SCALAR()
   97 
   98     my $copy_stat;
   99     {
  100         my %copy_stat = %{$stat};
  101         $copy_stat = \%copy_stat;
  102     }
  103     bless($copy_stat, 'Net::ZooKeeper::Stat');
  104     is(ref($copy_stat), 'Net::ZooKeeper::Stat',
  105        'stat FIRSTKEY(), NEXTKEY(): copied dereferenced stat handle');
  106 
  107     eval {
  108         my $val = $copy_stat->FIRSTKEY();
  109     };
  110     like($@, qr/invalid handle/,
  111          'stat FETCHKEY(): invalid stat handle');
  112 
  113     eval {
  114         my $val = $copy_stat->NEXTKEY('czxid');
  115     };
  116     like($@, qr/invalid handle/,
  117          'stat NEXTKEY(): invalid stat handle');
  118 
  119     my @keys = keys(%{$stat});
  120     is(scalar(@keys), 11,
  121        'keys(): count of keys from stat handle');
  122 
  123     @keys = keys(%{$copy_stat});
  124     is(scalar(@keys), 11,
  125        'keys(): count of keys from copied dereferenced stat handle');
  126 
  127     is($attr->FIRSTKEY(), 'czxid',
  128        'stat FIRSTKEY(): retrieved first key using inner stat hash');
  129 
  130     is($attr->NEXTKEY('num_children'), 'children_zxid',
  131        'stat NEXTKEY(): retrieved last key using inner stat hash');
  132 
  133     is($attr->NEXTKEY('children_zxid'), undef,
  134        'NEXTKEY(): undef returned after last key using inner stat hash');
  135 
  136     ok(scalar(%{$stat}),
  137        'scalar(): true value returned for dereferenced stat handle');
  138 
  139     ok($stat->SCALAR(),
  140        'stat SCALAR(): true value returned');
  141 
  142 
  143     ## FETCH()
  144 
  145     eval {
  146         my $val = $copy_stat->FETCH('version');
  147     };
  148     like($@, qr/invalid handle/,
  149          'stat FETCH(): invalid stat handle');
  150 
  151     {
  152         my $msg;
  153 
  154         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  155 
  156         my $val = $stat->{'foo'};
  157         ok(!defined($val),
  158            'stat FETCH(): undef returned for invalid element');
  159 
  160         like($msg, qr/invalid element/,
  161              'stat FETCH(): invalid element');
  162     }
  163 
  164     is($stat->{'czxid'}, 0,
  165        'stat FETCH(): default node creation ZooKeeper transaction ID');
  166 
  167     is($stat->{'mzxid'}, 0,
  168        'stat FETCH(): default data last-modified ZooKeeper transaction ID');
  169 
  170     is($stat->{'ctime'}, 0,
  171        'stat FETCH(): default node creation time');
  172 
  173     is($stat->{'mtime'}, 0,
  174        'stat FETCH(): default data last-modified time');
  175 
  176     is($stat->{'version'}, 0,
  177        'stat FETCH(): default data version');
  178 
  179     is($stat->{'children_version'}, 0,
  180        'stat FETCH(): default child node list version');
  181 
  182     is($stat->{'acl_version'}, 0,
  183        'stat FETCH(): default ACL version');
  184 
  185     is($stat->{'ephemeral_owner'}, 0,
  186        'stat FETCH(): ephemeral node owner session ID');
  187 
  188     is($stat->{'data_len'}, 0,
  189        'stat FETCH(): default data length');
  190 
  191     is($stat->{'num_children'}, 0,
  192        'stat FETCH(): default child node list length');
  193 
  194     is($stat->{'children_zxid'}, 0,
  195        'stat FETCH(): default child node list last-modified ' .
  196        'ZooKeeper transaction ID');
  197 
  198     is($attr->FETCH('version'), 0,
  199        'stat FETCH(): default data version using inner stat hash');
  200 
  201 
  202     ## STORE()
  203 
  204     eval {
  205         my $val = $copy_stat->STORE('version', 'foo');
  206     };
  207     like($@, qr/invalid handle/,
  208          'stat STORE(): invalid stat handle');
  209 
  210     {
  211         my $msg;
  212 
  213         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  214 
  215         $stat->{'foo'} = 'foo';
  216         like($msg, qr/invalid element/,
  217              'stat STORE(): invalid element');
  218     }
  219 
  220     {
  221         my $msg;
  222 
  223         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  224 
  225         $stat->{'czxid'} = 'foo';
  226         like($msg, qr/read-only element: czxid/,
  227              'stat STORE(): read-only node creation ' .
  228              'ZooKeeper transaction ID element');
  229     }
  230 
  231     {
  232         my $msg;
  233 
  234         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  235 
  236         $stat->{'mzxid'} = 'foo';
  237         like($msg, qr/read-only element: mzxid/,
  238              'stat STORE(): read-only data last-modified ' .
  239              'ZooKeeper transaction ID element');
  240     }
  241 
  242     {
  243         my $msg;
  244 
  245         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  246 
  247         $stat->{'ctime'} = 'foo';
  248         like($msg, qr/read-only element: ctime/,
  249              'stat STORE(): read-only node creation time element');
  250     }
  251 
  252     {
  253         my $msg;
  254 
  255         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  256 
  257         $stat->{'mtime'} = 'foo';
  258         like($msg, qr/read-only element: mtime/,
  259              'stat STORE(): read-only data last-modified time element');
  260     }
  261 
  262     {
  263         my $msg;
  264 
  265         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  266 
  267         $stat->{'version'} = 'foo';
  268         like($msg, qr/read-only element: version/,
  269              'stat STORE(): read-only data version element');
  270     }
  271 
  272     {
  273         my $msg;
  274 
  275         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  276 
  277         $stat->{'children_version'} = 'foo';
  278         like($msg, qr/read-only element: children_version/,
  279              'stat STORE(): read-only child node list version element');
  280     }
  281 
  282     {
  283         my $msg;
  284 
  285         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  286 
  287         $stat->{'acl_version'} = 'foo';
  288         like($msg, qr/read-only element: acl_version/,
  289              'stat STORE(): read-only ACL version element');
  290     }
  291 
  292     {
  293         my $msg;
  294 
  295         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  296 
  297         $stat->{'ephemeral_owner'} = 'foo';
  298         like($msg, qr/read-only element: ephemeral_owner/,
  299              'stat STORE(): read-only ephemeral node owner ' .
  300              'session ID element');
  301     }
  302 
  303     {
  304         my $msg;
  305 
  306         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  307 
  308         $stat->{'data_len'} = 'foo';
  309         like($msg, qr/read-only element: data_len/,
  310              'stat STORE(): read-only data length element');
  311     }
  312 
  313     {
  314         my $msg;
  315 
  316         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  317 
  318         $stat->{'num_children'} = 'foo';
  319         like($msg, qr/read-only element: num_children/,
  320              'stat STORE(): read-only child node list length element');
  321     }
  322 
  323     {
  324         my $msg;
  325 
  326         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  327 
  328         $stat->{'children_zxid'} = 'foo';
  329         like($msg, qr/read-only element: children_zxid/,
  330              'stat STORE(): read-only child node list last-modified ' .
  331              'ZooKeeper transaction ID element');
  332     }
  333 
  334     {
  335         my $msg;
  336 
  337         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  338 
  339         $attr->STORE('version', 'foo');
  340         like($msg, qr/read-only element: version/,
  341              'stat STORE(): read-only data version element using ' .
  342              'inner stat hash');
  343     }
  344 
  345 
  346     ## EXISTS()
  347 
  348     eval {
  349         my $val = $copy_stat->EXISTS('version');
  350     };
  351     like($@, qr/invalid handle/,
  352          'stat EXISTS(): invalid stat handle');
  353 
  354     ok(!exists($stat->{'foo'}),
  355        'exists(): invalid element of stat handle');
  356 
  357     ok(exists($stat->{'czxid'}),
  358        'exists(): node creation ZooKeeper transaction ID');
  359 
  360     ok(exists($stat->{'mzxid'}),
  361        'exists(): data last-modified ZooKeeper transaction ID');
  362 
  363     ok(exists($stat->{'ctime'}),
  364        'exists(): node creation time');
  365 
  366     ok(exists($stat->{'mtime'}),
  367        'exists(): data last-modified time');
  368 
  369     ok(exists($stat->{'version'}),
  370        'exists(): data version');
  371 
  372     ok(exists($stat->{'children_version'}),
  373        'exists(): child node list version');
  374 
  375     ok(exists($stat->{'acl_version'}),
  376        'exists(): ACL version');
  377 
  378     ok(exists($stat->{'ephemeral_owner'}),
  379        'exists(): ephemeral node owner session ID');
  380 
  381     ok(exists($stat->{'data_len'}),
  382        'exists(): data length');
  383 
  384     ok(exists($stat->{'num_children'}),
  385        'exists(): child node list length');
  386 
  387     ok(exists($stat->{'children_zxid'}),
  388        'exists(): child node list last-modified ZooKeeper transaction ID');
  389 
  390     ok($attr->EXISTS('version'),
  391        'stat EXISTS(): data version using inner stat hash');
  392 
  393 
  394     ## DELETE(), CLEAR()
  395 
  396     {
  397         my $msg;
  398 
  399         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  400 
  401         delete($stat->{'version'});
  402         like($msg,
  403              qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/,
  404              'delete(): deleting stat hash elements not supported');
  405     }
  406 
  407     {
  408         my $msg;
  409 
  410         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  411 
  412         $stat->DELETE({'version'});
  413         like($msg,
  414              qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/,
  415              'stat DELETE(): deleting stat hash elements not supported');
  416     }
  417 
  418     {
  419         my $msg;
  420 
  421         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  422 
  423         %{$stat} = ();
  424         like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/,
  425              'assign: clearing stat hashes not supported');
  426     }
  427 
  428     {
  429         my $msg;
  430 
  431         $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  432 
  433         $stat->CLEAR();
  434         like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/,
  435              'stat CLEAR(): clearing stat hashes not supported');
  436     }
  437 }
  438