"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/45_class.t" (25 Jan 2023, 10706 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 => 47;
   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     skip 'no valid handle', 15 unless (defined($zkh));
   36 
   37     my $stat = $zkh->stat();
   38     my $watch = $zkh->watch();
   39 
   40 
   41     ## DESTROY() on reblessed handle
   42 
   43     bless($zkh, 'My::ZooKeeper');
   44     is(ref($zkh), 'My::ZooKeeper',
   45        'bless(): reblessed handle');
   46 
   47     eval {
   48         $zkh->EXISTS();
   49     };
   50     like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper"/,
   51          'EXISTS(): not defined on reblessed handle');
   52 
   53     my $attr = tied(%{$zkh});
   54 
   55     my $ret = $attr->DESTROY();
   56     ok($ret,
   57        'DESTROY(): destroyed inner hash of reblessed handle');
   58 
   59     $ret = $attr->DESTROY();
   60     ok(!$ret,
   61        'DESTROY(): no action on destroyed inner hash of reblessed handle');
   62 
   63     undef $zkh;
   64     ok(!defined($zkh),
   65        'undef: released reblessed handle');
   66 
   67 
   68     ## DESTROY() on reblessed stat handle
   69 
   70     bless($stat, 'My::ZooKeeper::Stat');
   71     is(ref($stat), 'My::ZooKeeper::Stat',
   72        'bless(): reblessed stat handle');
   73 
   74     eval {
   75         $stat->EXISTS(1);
   76     };
   77     like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper::Stat"/,
   78          'stat EXISTS(): not defined on reblessed stat handle');
   79 
   80     $attr = tied(%{$stat});
   81 
   82     $ret = $attr->DESTROY();
   83     ok($ret,
   84        'stat DESTROY(): destroyed inner hash of reblessed stat handle');
   85 
   86     $ret = $attr->DESTROY();
   87     ok(!$ret,
   88        'stat DESTROY(): no action on destroyed inner hash of ' .
   89        'reblessed stat handle');
   90 
   91     undef $stat;
   92     ok(!defined($stat),
   93        'undef: released reblessed stat handle');
   94 
   95 
   96     ## DESTROY() on reblessed watch handle
   97 
   98     bless($watch, 'My::ZooKeeper::Watch');
   99     is(ref($watch), 'My::ZooKeeper::Watch',
  100        'bless(): reblessed watch handle');
  101 
  102     eval {
  103         $watch->EXISTS(1);
  104     };
  105     like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper::Watch"/,
  106          'watch EXISTS(): not defined on reblessed watch handle');
  107 
  108     $attr = tied(%{$watch});
  109 
  110     $ret = $attr->DESTROY();
  111     ok($ret,
  112        'watch DESTROY(): destroyed inner hash of reblessed watch handle');
  113 
  114     $ret = $attr->DESTROY();
  115     ok(!$ret,
  116        'watch DESTROY(): no action on destroyed inner hash of ' .
  117        'reblessed watch handle');
  118 
  119     undef $watch;
  120     ok(!defined($watch),
  121        'undef: released reblessed watch handle');
  122 }
  123 
  124 SKIP: {
  125     my $zkh = Net::ZooKeeper->new($hosts);
  126 
  127     skip 'no valid handle', 9 unless (defined($zkh));
  128 
  129     my $stat = $zkh->stat();
  130     my $watch = $zkh->watch();
  131 
  132 
  133     ## UNTIE() on reblessed handle
  134 
  135     bless($zkh, 'My::ZooKeeper');
  136     is(ref($zkh), 'My::ZooKeeper',
  137        'bless(): reblessed handle');
  138 
  139     eval {
  140         untie(%{$zkh});
  141     };
  142     like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
  143          'untie(): untying hashes from reblessed handle not supported');
  144 
  145     my $attr = tied(%{$zkh});
  146 
  147     eval {
  148         $attr->UNTIE(0);
  149     };
  150     like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
  151          'UNTIE(): untying hashes from reblessed handle not supported');
  152 
  153 
  154     ## UNTIE() on reblessed stat handle
  155 
  156     bless($stat, 'My::ZooKeeper::Stat');
  157     is(ref($stat), 'My::ZooKeeper::Stat',
  158        'bless(): reblessed stat handle');
  159 
  160     eval {
  161         untie(%{$stat});
  162     };
  163     like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
  164          'untie(): untying hashes from reblessed stat handle not supported');
  165 
  166     $attr = tied(%{$stat});
  167 
  168     eval {
  169         $attr->UNTIE(0);
  170     };
  171     like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
  172          'stat UNTIE(): untying hashes from reblessed stat handle ' .
  173          'not supported');
  174 
  175 
  176     ## UNTIE() on reblessed watch handle
  177 
  178     bless($watch, 'My::ZooKeeper::Watch');
  179     is(ref($watch), 'My::ZooKeeper::Watch',
  180        'bless(): reblessed watch handle');
  181 
  182     eval {
  183         untie(%{$watch});
  184     };
  185     like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
  186          'untie(): untying hashes from reblessed watch handle not supported');
  187 
  188     $attr = tied(%{$watch});
  189 
  190     eval {
  191         $attr->UNTIE(0);
  192     };
  193     like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
  194          'watch UNTIE(): untying hashes from reblessed watch handle ' .
  195          'not supported');
  196 }
  197 
  198 
  199 package Net::ZooKeeper::Test;
  200 
  201 use Net::ZooKeeper qw(:acls);
  202 
  203 our @ISA = qw(Net::ZooKeeper);
  204 
  205 sub create
  206 {
  207     my($self, $path, $buf) = @_;
  208 
  209     return $self->SUPER::create($path, $buf,
  210                                 'path_read_len' => length($path),
  211                                 'acl' => ZOO_OPEN_ACL_UNSAFE);
  212 }
  213 
  214 sub get_first_child
  215 {
  216     my($self, $path) = @_;
  217 
  218     my @child_paths = $self->get_children($path);
  219 
  220     if (@child_paths > 0) {
  221         return $path . (($path =~ /\/$/) ? '' : '/') . $child_paths[0];
  222     }
  223 
  224     return undef;
  225 }
  226 
  227 sub stat
  228 {
  229     my $self = shift;
  230 
  231     my $stat = $self->SUPER::stat();
  232 
  233     return bless($stat, 'Net::ZooKeeper::Test::Stat');
  234 }
  235 
  236 
  237 sub watch
  238 {
  239     my $self = shift;
  240 
  241     my $watch = $self->SUPER::watch();
  242 
  243     return bless($watch, 'Net::ZooKeeper::Test::Watch');
  244 }
  245 
  246 
  247 package Net::ZooKeeper::Test::Stat;
  248 
  249 our @ISA = qw(Net::ZooKeeper::Stat);
  250 
  251 sub get_ctime
  252 {
  253     my $self = shift;
  254 
  255     return $self->{'ctime'};
  256 }
  257 
  258 
  259 package Net::ZooKeeper::Test::Watch;
  260 
  261 our @ISA = qw(Net::ZooKeeper::Watch);
  262 
  263 sub get_timeout
  264 {
  265     my $self = shift;
  266 
  267     return $self->{'timeout'};
  268 }
  269 
  270 
  271 package main;
  272 
  273 my $sub_zkh = Net::ZooKeeper::Test->new($hosts);
  274 isa_ok($sub_zkh, 'Net::ZooKeeper::Test',
  275        'new(): created subclassed handle');
  276 
  277 SKIP: {
  278     skip 'no valid subclassed handle', 21 unless (defined($sub_zkh));
  279 
  280     is($sub_zkh->{'data_read_len'}, 1023,
  281        'FETCH(): default data read length using subclassed handle');
  282 
  283     my $path;
  284 
  285     SKIP: {
  286         my $ret = $sub_zkh->exists($root_path);
  287 
  288         skip 'no connection to ZooKeeper', 1 unless
  289             (defined($ret) and $ret);
  290 
  291         $path = $sub_zkh->create($node_path, 'foo',
  292                                  'acl' => ZOO_OPEN_ACL_UNSAFE);
  293         is($path, $node_path,
  294            'create(): created node with subclassed handle');
  295     }
  296 
  297     SKIP: {
  298         skip 'no connection to ZooKeeper', 1 unless
  299             (defined($path) and $path eq $node_path);
  300 
  301         my $child_path = $sub_zkh->get_first_child($root_path);
  302         is($child_path, $node_path,
  303            'get_first_child(): retrieved first child with subclassed handle');
  304     }
  305 
  306     my $sub_stat = $sub_zkh->stat();
  307     isa_ok($sub_stat, 'Net::ZooKeeper::Test::Stat',
  308            'stat(): created subclassed stat handle');
  309 
  310     SKIP: {
  311         skip 'no valid subclassed stat handle', 6 unless
  312             (defined($sub_stat));
  313 
  314         is($sub_stat->{'ctime'}, 0,
  315            'stat FETCH(): default ctime using subclassed stat handle');
  316 
  317         SKIP: {
  318             my $ret = $sub_zkh->exists($node_path, 'stat' => $sub_stat) if
  319                 (defined($path) and $path eq $node_path);
  320 
  321             skip 'no connection to ZooKeeper', 2 unless
  322                 (defined($ret) and $ret);
  323 
  324             my $ctime = $sub_stat->get_ctime();
  325             ok($ctime > 0,
  326                'get_ctime(): retrieved ctime with subclassed stat handle');
  327 
  328             is($sub_stat->{'ctime'}, $ctime,
  329                'stat FETCH(): ctime using subclassed stat handle');
  330         }
  331 
  332         my $ret = $sub_stat->DESTROY();
  333         ok($ret,
  334            'stat DESTROY(): destroyed subclassed stat handle');
  335 
  336         $ret = $sub_stat->DESTROY();
  337         ok(!$ret,
  338            'stat DESTROY(): no action on destroyed subclassed stat handle');
  339 
  340         undef $sub_stat;
  341         ok(!defined($sub_stat),
  342            'undef: released subclassed stat handle');
  343     }
  344 
  345     my $sub_watch = $sub_zkh->watch();
  346     isa_ok($sub_watch, 'Net::ZooKeeper::Test::Watch',
  347            'watch(): created subclassed watch handle');
  348 
  349     SKIP: {
  350         skip 'no valid subclassed watch handle', 6 unless
  351             (defined($sub_watch));
  352 
  353         SKIP: {
  354             my $ret = $sub_zkh->exists($root_path, 'watch' => $sub_watch);
  355 
  356             skip 'no connection to ZooKeeper', 3 unless
  357                 (defined($ret) and $ret);
  358 
  359             $sub_watch->{'timeout'} = 50;
  360 
  361             is($sub_watch->get_timeout(), 50,
  362                'get_timeout(): retrieved timeout with subclassed ' .
  363                'watch handle');
  364 
  365             is($sub_watch->{'timeout'}, 50,
  366                'watch FETCH(): timeout using subclassed stat handle');
  367 
  368             $ret = $sub_watch->wait();
  369             ok(!$ret,
  370                'wait(): watch after checking node existence timed out with ' .
  371                'subclassed watch handle');
  372         }
  373 
  374         my $ret = $sub_watch->DESTROY();
  375         ok($ret,
  376            'watch DESTROY(): destroyed subclassed watch handle');
  377 
  378         $ret = $sub_watch->DESTROY();
  379         ok(!$ret,
  380            'watch DESTROY(): no action on destroyed subclassed watch handle');
  381 
  382         undef $sub_watch;
  383         ok(!defined($sub_watch),
  384            'undef: released subclassed watch handle');
  385     }
  386 
  387     SKIP: {
  388         skip 'no connection to ZooKeeper', 1 unless
  389             (defined($path) and $path eq $node_path);
  390 
  391         my $ret = $sub_zkh->delete($node_path);
  392         ok($ret,
  393            'delete(): deleted node with subclassed handle');
  394     }
  395 
  396     my $ret = $sub_zkh->DESTROY();
  397     ok($ret,
  398        'DESTROY(): destroyed subclassed handle');
  399 
  400     $ret = $sub_zkh->DESTROY();
  401     ok(!$ret,
  402        'DESTROY(): no action on destroyed subclassed handle');
  403 
  404     undef $sub_zkh;
  405     ok(!defined($sub_zkh),
  406        'undef: released subclassed handle');
  407 }
  408