123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- # Net::ZooKeeper - Perl extension for Apache ZooKeeper
- #
- # Licensed to the Apache Software Foundation (ASF) under one
- # or more contributor license agreements. See the NOTICE file
- # distributed with this work for additional information
- # regarding copyright ownership. The ASF licenses this file
- # to you under the Apache License, Version 2.0 (the
- # "License"); you may not use this file except in compliance
- # with the License. You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- use File::Spec;
- use Test::More tests => 66;
- BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
- my $test_dir;
- (undef, $test_dir, undef) = File::Spec->splitpath($0);
- require File::Spec->catfile($test_dir, 'util.pl');
- my($hosts, $root_path, $node_path) = zk_test_setup(0);
- SKIP: {
- my $zkh = Net::ZooKeeper->new($hosts);
- my $stat = $zkh->stat() if (defined($zkh));
- skip 'no valid stat handle', 4 unless (defined($stat));
- ## DESTROY()
- my $attr = tied(%{$stat});
- my $ret = $attr->DESTROY();
- ok($ret,
- 'stat DESTROY(): destroyed inner stat hash');
- $ret = $attr->DESTROY();
- ok(!$ret,
- 'stat DESTROY(): no action on destroyed inner stat hash');
- $ret = $stat->DESTROY();
- ok(!$ret,
- 'stat DESTROY(): no action on stat handle with destroyed inner hash');
- undef $stat;
- ok(!defined($stat),
- 'undef: released stat handle with destroyed inner hash');
- }
- SKIP: {
- my $zkh = Net::ZooKeeper->new($hosts);
- my $stat = $zkh->stat() if (defined($zkh));
- skip 'no valid stat handle', 61 unless (defined($stat));
- ## TIEHASH(), UNTIE()
- eval {
- tie(%{$stat}, 'Net::ZooKeeper::Stat');
- };
- like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/,
- 'tie(): tying stat hashes not supported');
- eval {
- Net::ZooKeeper::Stat::TIEHASH('Net::ZooKeeper::Stat');
- };
- like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/,
- 'stat TIEHASH(): tying stat hashes not supported');
- eval {
- untie(%{$stat});
- };
- like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
- 'untie(): untying stat hashes not supported');
- my $attr = tied(%{$stat});
- eval {
- $attr->UNTIE(0);
- };
- like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/,
- 'stat UNTIE(): untying stat hashes not supported');
- ## FIRSTKEY(), NEXTKEY(), SCALAR()
- my $copy_stat;
- {
- my %copy_stat = %{$stat};
- $copy_stat = \%copy_stat;
- }
- bless($copy_stat, 'Net::ZooKeeper::Stat');
- is(ref($copy_stat), 'Net::ZooKeeper::Stat',
- 'stat FIRSTKEY(), NEXTKEY(): copied dereferenced stat handle');
- eval {
- my $val = $copy_stat->FIRSTKEY();
- };
- like($@, qr/invalid handle/,
- 'stat FETCHKEY(): invalid stat handle');
- eval {
- my $val = $copy_stat->NEXTKEY('czxid');
- };
- like($@, qr/invalid handle/,
- 'stat NEXTKEY(): invalid stat handle');
- my @keys = keys(%{$stat});
- is(scalar(@keys), 11,
- 'keys(): count of keys from stat handle');
- @keys = keys(%{$copy_stat});
- is(scalar(@keys), 11,
- 'keys(): count of keys from copied dereferenced stat handle');
- is($attr->FIRSTKEY(), 'czxid',
- 'stat FIRSTKEY(): retrieved first key using inner stat hash');
- is($attr->NEXTKEY('num_children'), 'children_zxid',
- 'stat NEXTKEY(): retrieved last key using inner stat hash');
- is($attr->NEXTKEY('children_zxid'), undef,
- 'NEXTKEY(): undef returned after last key using inner stat hash');
- ok(scalar(%{$stat}),
- 'scalar(): true value returned for dereferenced stat handle');
- ok($stat->SCALAR(),
- 'stat SCALAR(): true value returned');
- ## FETCH()
- eval {
- my $val = $copy_stat->FETCH('version');
- };
- like($@, qr/invalid handle/,
- 'stat FETCH(): invalid stat handle');
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- my $val = $stat->{'foo'};
- ok(!defined($val),
- 'stat FETCH(): undef returned for invalid element');
- like($msg, qr/invalid element/,
- 'stat FETCH(): invalid element');
- }
- is($stat->{'czxid'}, 0,
- 'stat FETCH(): default node creation ZooKeeper transaction ID');
- is($stat->{'mzxid'}, 0,
- 'stat FETCH(): default data last-modified ZooKeeper transaction ID');
- is($stat->{'ctime'}, 0,
- 'stat FETCH(): default node creation time');
- is($stat->{'mtime'}, 0,
- 'stat FETCH(): default data last-modified time');
- is($stat->{'version'}, 0,
- 'stat FETCH(): default data version');
- is($stat->{'children_version'}, 0,
- 'stat FETCH(): default child node list version');
- is($stat->{'acl_version'}, 0,
- 'stat FETCH(): default ACL version');
- is($stat->{'ephemeral_owner'}, 0,
- 'stat FETCH(): ephemeral node owner session ID');
- is($stat->{'data_len'}, 0,
- 'stat FETCH(): default data length');
- is($stat->{'num_children'}, 0,
- 'stat FETCH(): default child node list length');
- is($stat->{'children_zxid'}, 0,
- 'stat FETCH(): default child node list last-modified ' .
- 'ZooKeeper transaction ID');
- is($attr->FETCH('version'), 0,
- 'stat FETCH(): default data version using inner stat hash');
- ## STORE()
- eval {
- my $val = $copy_stat->STORE('version', 'foo');
- };
- like($@, qr/invalid handle/,
- 'stat STORE(): invalid stat handle');
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'foo'} = 'foo';
- like($msg, qr/invalid element/,
- 'stat STORE(): invalid element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'czxid'} = 'foo';
- like($msg, qr/read-only element: czxid/,
- 'stat STORE(): read-only node creation ' .
- 'ZooKeeper transaction ID element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'mzxid'} = 'foo';
- like($msg, qr/read-only element: mzxid/,
- 'stat STORE(): read-only data last-modified ' .
- 'ZooKeeper transaction ID element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'ctime'} = 'foo';
- like($msg, qr/read-only element: ctime/,
- 'stat STORE(): read-only node creation time element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'mtime'} = 'foo';
- like($msg, qr/read-only element: mtime/,
- 'stat STORE(): read-only data last-modified time element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'version'} = 'foo';
- like($msg, qr/read-only element: version/,
- 'stat STORE(): read-only data version element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'children_version'} = 'foo';
- like($msg, qr/read-only element: children_version/,
- 'stat STORE(): read-only child node list version element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'acl_version'} = 'foo';
- like($msg, qr/read-only element: acl_version/,
- 'stat STORE(): read-only ACL version element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'ephemeral_owner'} = 'foo';
- like($msg, qr/read-only element: ephemeral_owner/,
- 'stat STORE(): read-only ephemeral node owner ' .
- 'session ID element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'data_len'} = 'foo';
- like($msg, qr/read-only element: data_len/,
- 'stat STORE(): read-only data length element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'num_children'} = 'foo';
- like($msg, qr/read-only element: num_children/,
- 'stat STORE(): read-only child node list length element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->{'children_zxid'} = 'foo';
- like($msg, qr/read-only element: children_zxid/,
- 'stat STORE(): read-only child node list last-modified ' .
- 'ZooKeeper transaction ID element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $attr->STORE('version', 'foo');
- like($msg, qr/read-only element: version/,
- 'stat STORE(): read-only data version element using ' .
- 'inner stat hash');
- }
- ## EXISTS()
- eval {
- my $val = $copy_stat->EXISTS('version');
- };
- like($@, qr/invalid handle/,
- 'stat EXISTS(): invalid stat handle');
- ok(!exists($stat->{'foo'}),
- 'exists(): invalid element of stat handle');
- ok(exists($stat->{'czxid'}),
- 'exists(): node creation ZooKeeper transaction ID');
- ok(exists($stat->{'mzxid'}),
- 'exists(): data last-modified ZooKeeper transaction ID');
- ok(exists($stat->{'ctime'}),
- 'exists(): node creation time');
- ok(exists($stat->{'mtime'}),
- 'exists(): data last-modified time');
- ok(exists($stat->{'version'}),
- 'exists(): data version');
- ok(exists($stat->{'children_version'}),
- 'exists(): child node list version');
- ok(exists($stat->{'acl_version'}),
- 'exists(): ACL version');
- ok(exists($stat->{'ephemeral_owner'}),
- 'exists(): ephemeral node owner session ID');
- ok(exists($stat->{'data_len'}),
- 'exists(): data length');
- ok(exists($stat->{'num_children'}),
- 'exists(): child node list length');
- ok(exists($stat->{'children_zxid'}),
- 'exists(): child node list last-modified ZooKeeper transaction ID');
- ok($attr->EXISTS('version'),
- 'stat EXISTS(): data version using inner stat hash');
- ## DELETE(), CLEAR()
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- delete($stat->{'version'});
- like($msg,
- qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/,
- 'delete(): deleting stat hash elements not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->DELETE({'version'});
- like($msg,
- qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/,
- 'stat DELETE(): deleting stat hash elements not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- %{$stat} = ();
- like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/,
- 'assign: clearing stat hashes not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $stat->CLEAR();
- like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/,
- 'stat CLEAR(): clearing stat hashes not supported');
- }
- }
|