123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- # 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 => 54;
- 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);
- skip 'no valid handle', 4 unless (defined($zkh));
- ## DESTROY()
- my $attr = tied(%{$zkh});
- my $ret = $attr->DESTROY();
- ok($ret,
- 'DESTROY(): destroyed inner hash');
- $ret = $attr->DESTROY();
- ok(!$ret,
- 'DESTROY(): no action on destroyed inner hash');
- $ret = $zkh->DESTROY();
- ok(!$ret,
- 'DESTROY(): no action on handle with destroyed inner hash');
- undef $zkh;
- ok(!defined($zkh),
- 'undef: released handle with destroyed inner hash');
- }
- SKIP: {
- my $zkh = Net::ZooKeeper->new($hosts);
- skip 'no valid handle', 49 unless (defined($zkh));
- ## TIEHASH(), UNTIE()
- eval {
- tie(%{$zkh}, 'Net::ZooKeeper');
- };
- like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
- 'tie(): tying hashes not supported');
- eval {
- Net::ZooKeeper::TIEHASH('Net::ZooKeeper');
- };
- like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
- 'TIEHASH(): tying hashes not supported');
- eval {
- untie(%{$zkh});
- };
- like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
- 'untie(): untying hashes not supported');
- my $attr = tied(%{$zkh});
- eval {
- $attr->UNTIE(0);
- };
- like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
- 'UNTIE(): untying hashes not supported');
- ## FIRSTKEY(), NEXTKEY(), SCALAR()
- my $copy_zkh;
- {
- my %copy_zkh = %{$zkh};
- $copy_zkh = \%copy_zkh;
- }
- bless($copy_zkh, 'Net::ZooKeeper');
- is(ref($copy_zkh), 'Net::ZooKeeper',
- 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
- eval {
- my $val = $copy_zkh->FIRSTKEY();
- };
- like($@, qr/invalid handle/,
- 'FETCHKEY(): invalid handle');
- eval {
- my $val = $copy_zkh->NEXTKEY('data_read_len');
- };
- like($@, qr/invalid handle/,
- 'NEXTKEY(): invalid handle');
- my @keys = keys(%{$zkh});
- is(scalar(@keys), 7,
- 'keys(): count of keys from handle');
- @keys = keys(%{$copy_zkh});
- is(scalar(@keys), 7,
- 'keys(): count of keys from copied dereferenced handle');
- is($attr->FIRSTKEY(), 'data_read_len',
- 'FIRSTKEY(): retrieved first key using inner hash');
- is($attr->NEXTKEY('session_id'), 'pending_watches',
- 'NEXTKEY(): retrieved last key using inner hash');
- is($attr->NEXTKEY('pending_watches'), undef,
- 'NEXTKEY(): undef returned after last key using inner hash');
- ok(scalar(%{$zkh}),
- 'scalar(): true value returned for dereferenced handle');
- ok($zkh->SCALAR(),
- 'SCALAR(): true value returned');
- ## FETCH()
- eval {
- my $val = $copy_zkh->FETCH('data_read_len');
- };
- like($@, qr/invalid handle/,
- 'FETCH(): invalid handle');
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- my $val = $zkh->{'foo'};
- ok(!defined($val),
- 'FETCH(): undef returned for invalid element');
- like($msg, qr/invalid element/,
- 'FETCH(): invalid element');
- }
- is($zkh->{'data_read_len'}, 1023,
- 'FETCH(): default data read length');
- is($zkh->{'path_read_len'}, 1023,
- 'FETCH(): default path read length');
- is($zkh->{'hosts'}, $hosts,
- 'FETCH(): server hosts');
- is($zkh->{'session_timeout'}, 10000,
- 'FETCH(): default session timeout');
- ok(defined($zkh->{'session_id'}),
- 'FETCH(): session ID');
- SKIP: {
- my $zkh = Net::ZooKeeper->new('0.0.0.0:0');
- skip 'no valid handle with invalid host', 1 unless (defined($zkh));
- is($zkh->{'session_id'}, '',
- 'FETCH(): empty session ID with invalid host');
- }
- is($zkh->{'pending_watches'}, 0,
- 'FETCH(): default pending watch list length');
- is($attr->FETCH('data_read_len'), 1023,
- 'FETCH(): default data read length using inner hash');
- ## STORE()
- eval {
- my $val = $copy_zkh->STORE('data_read_len', 'foo');
- };
- like($@, qr/invalid handle/,
- 'STORE(): invalid handle');
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->{'foo'} = 'foo';
- like($msg, qr/invalid element/,
- 'STORE(): invalid element');
- }
- eval {
- $zkh->{'data_read_len'} = -3;
- };
- like($@, qr/invalid data read length/,
- 'STORE(): invalid data read length');
- eval {
- $zkh->{'path_read_len'} = -3;
- };
- like($@, qr/invalid path read length/,
- 'STORE(): invalid path read length');
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->{'hosts'} = 'foo';
- like($msg, qr/read-only element: hosts/,
- 'STORE(): read-only server hosts element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->{'session_timeout'} = 0;
- like($msg, qr/read-only element: session_timeout/,
- 'STORE(): read-only session timeout element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->{'session_id'} = 'foo';
- like($msg, qr/read-only element: session_id/,
- 'STORE(): read-only session ID element');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->{'pending_watches'} = 0;
- like($msg, qr/read-only element: pending_watches/,
- 'STORE(): read-only pending watch list length element');
- }
- $zkh->{'data_read_len'} = 200;
- is($zkh->{'data_read_len'}, 200,
- 'STORE(): updated data read length');
- $zkh->{'path_read_len'} = 100;
- is($zkh->{'path_read_len'}, 100,
- 'STORE(): updated path read length');
- $attr->STORE('data_read_len', 100);
- is($zkh->{'data_read_len'}, 100,
- 'STORE(): updated data read length using inner hash');
- ## EXISTS()
- eval {
- my $val = $copy_zkh->EXISTS('data_read_len');
- };
- like($@, qr/invalid handle/,
- 'EXISTS(): invalid handle');
- ok(!exists($zkh->{'foo'}),
- 'exists(): invalid element of handle');
- ok(exists($zkh->{'data_read_len'}),
- 'exists(): data read length');
- ok(exists($zkh->{'path_read_len'}),
- 'exists(): path read length');
- ok(exists($zkh->{'hosts'}),
- 'exists(): server hosts');
- ok(exists($zkh->{'session_timeout'}),
- 'exists(): session timeout');
- ok(exists($zkh->{'session_id'}),
- 'exists(): session ID');
- ok(exists($zkh->{'pending_watches'}),
- 'exists(): pending watch list length');
- ok($attr->EXISTS('data_read_len'),
- 'EXISTS(): data read length using inner hash');
- ## DELETE(), CLEAR()
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- delete($zkh->{'data_read_len'});
- like($msg,
- qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
- 'delete(): deleting hash elements not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->DELETE({'data_read_len'});
- like($msg,
- qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
- 'DELETE(): deleting hash elements not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- %{$zkh} = ();
- like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
- 'assign: clearing hashes not supported');
- }
- {
- my $msg;
- $SIG{'__WARN__'} = sub { $msg = $_[0]; };
- $zkh->CLEAR();
- like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
- 'CLEAR(): clearing hashes not supported');
- }
- }
|