1
# Net::ZooKeeper - Perl extension for Apache ZooKeeper
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
11
# http://www.apache.org/licenses/LICENSE-2.0
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.
20
use Test::More tests => 54;
22
BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
26
(undef, $test_dir, undef) = File::Spec->splitpath($0);
27
require File::Spec->catfile($test_dir, 'util.pl');
29
my($hosts, $root_path, $node_path) = zk_test_setup(0);
33
my $zkh = Net::ZooKeeper->new($hosts);
35
skip 'no valid handle', 4 unless (defined($zkh));
40
my $attr = tied(%{$zkh});
42
my $ret = $attr->DESTROY();
44
'DESTROY(): destroyed inner hash');
46
$ret = $attr->DESTROY();
48
'DESTROY(): no action on destroyed inner hash');
50
$ret = $zkh->DESTROY();
52
'DESTROY(): no action on handle with destroyed inner hash');
56
'undef: released handle with destroyed inner hash');
60
my $zkh = Net::ZooKeeper->new($hosts);
62
skip 'no valid handle', 49 unless (defined($zkh));
68
tie(%{$zkh}, 'Net::ZooKeeper');
70
like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
71
'tie(): tying hashes not supported');
74
Net::ZooKeeper::TIEHASH('Net::ZooKeeper');
76
like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
77
'TIEHASH(): tying hashes not supported');
82
like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
83
'untie(): untying hashes not supported');
85
my $attr = tied(%{$zkh});
90
like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
91
'UNTIE(): untying hashes not supported');
94
## FIRSTKEY(), NEXTKEY(), SCALAR()
98
my %copy_zkh = %{$zkh};
99
$copy_zkh = \%copy_zkh;
101
bless($copy_zkh, 'Net::ZooKeeper');
102
is(ref($copy_zkh), 'Net::ZooKeeper',
103
'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
106
my $val = $copy_zkh->FIRSTKEY();
108
like($@, qr/invalid handle/,
109
'FETCHKEY(): invalid handle');
112
my $val = $copy_zkh->NEXTKEY('data_read_len');
114
like($@, qr/invalid handle/,
115
'NEXTKEY(): invalid handle');
117
my @keys = keys(%{$zkh});
119
'keys(): count of keys from handle');
121
@keys = keys(%{$copy_zkh});
123
'keys(): count of keys from copied dereferenced handle');
125
is($attr->FIRSTKEY(), 'data_read_len',
126
'FIRSTKEY(): retrieved first key using inner hash');
128
is($attr->NEXTKEY('session_id'), 'pending_watches',
129
'NEXTKEY(): retrieved last key using inner hash');
131
is($attr->NEXTKEY('pending_watches'), undef,
132
'NEXTKEY(): undef returned after last key using inner hash');
135
'scalar(): true value returned for dereferenced handle');
138
'SCALAR(): true value returned');
144
my $val = $copy_zkh->FETCH('data_read_len');
146
like($@, qr/invalid handle/,
147
'FETCH(): invalid handle');
152
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
154
my $val = $zkh->{'foo'};
156
'FETCH(): undef returned for invalid element');
158
like($msg, qr/invalid element/,
159
'FETCH(): invalid element');
162
is($zkh->{'data_read_len'}, 1023,
163
'FETCH(): default data read length');
165
is($zkh->{'path_read_len'}, 1023,
166
'FETCH(): default path read length');
168
is($zkh->{'hosts'}, $hosts,
169
'FETCH(): server hosts');
171
is($zkh->{'session_timeout'}, 10000,
172
'FETCH(): default session timeout');
174
ok(defined($zkh->{'session_id'}),
175
'FETCH(): session ID');
178
my $zkh = Net::ZooKeeper->new('0.0.0.0:0');
180
skip 'no valid handle with invalid host', 1 unless (defined($zkh));
182
is($zkh->{'session_id'}, '',
183
'FETCH(): empty session ID with invalid host');
186
is($zkh->{'pending_watches'}, 0,
187
'FETCH(): default pending watch list length');
189
is($attr->FETCH('data_read_len'), 1023,
190
'FETCH(): default data read length using inner hash');
196
my $val = $copy_zkh->STORE('data_read_len', 'foo');
198
like($@, qr/invalid handle/,
199
'STORE(): invalid handle');
204
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
206
$zkh->{'foo'} = 'foo';
207
like($msg, qr/invalid element/,
208
'STORE(): invalid element');
212
$zkh->{'data_read_len'} = -3;
214
like($@, qr/invalid data read length/,
215
'STORE(): invalid data read length');
218
$zkh->{'path_read_len'} = -3;
220
like($@, qr/invalid path read length/,
221
'STORE(): invalid path read length');
226
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
228
$zkh->{'hosts'} = 'foo';
229
like($msg, qr/read-only element: hosts/,
230
'STORE(): read-only server hosts element');
236
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
238
$zkh->{'session_timeout'} = 0;
239
like($msg, qr/read-only element: session_timeout/,
240
'STORE(): read-only session timeout element');
246
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
248
$zkh->{'session_id'} = 'foo';
249
like($msg, qr/read-only element: session_id/,
250
'STORE(): read-only session ID element');
256
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
258
$zkh->{'pending_watches'} = 0;
259
like($msg, qr/read-only element: pending_watches/,
260
'STORE(): read-only pending watch list length element');
263
$zkh->{'data_read_len'} = 200;
264
is($zkh->{'data_read_len'}, 200,
265
'STORE(): updated data read length');
267
$zkh->{'path_read_len'} = 100;
268
is($zkh->{'path_read_len'}, 100,
269
'STORE(): updated path read length');
271
$attr->STORE('data_read_len', 100);
272
is($zkh->{'data_read_len'}, 100,
273
'STORE(): updated data read length using inner hash');
279
my $val = $copy_zkh->EXISTS('data_read_len');
281
like($@, qr/invalid handle/,
282
'EXISTS(): invalid handle');
284
ok(!exists($zkh->{'foo'}),
285
'exists(): invalid element of handle');
287
ok(exists($zkh->{'data_read_len'}),
288
'exists(): data read length');
290
ok(exists($zkh->{'path_read_len'}),
291
'exists(): path read length');
293
ok(exists($zkh->{'hosts'}),
294
'exists(): server hosts');
296
ok(exists($zkh->{'session_timeout'}),
297
'exists(): session timeout');
299
ok(exists($zkh->{'session_id'}),
300
'exists(): session ID');
302
ok(exists($zkh->{'pending_watches'}),
303
'exists(): pending watch list length');
305
ok($attr->EXISTS('data_read_len'),
306
'EXISTS(): data read length using inner hash');
314
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
316
delete($zkh->{'data_read_len'});
318
qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
319
'delete(): deleting hash elements not supported');
325
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
327
$zkh->DELETE({'data_read_len'});
329
qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
330
'DELETE(): deleting hash elements not supported');
336
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
339
like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
340
'assign: clearing hashes not supported');
346
$SIG{'__WARN__'} = sub { $msg = $_[0]; };
349
like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
350
'CLEAR(): clearing hashes not supported');