"Fossies" - the Fresh Open Source Software Archive 
Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/24_watch_tie.t" (25 Jan 2023, 7686 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 => 42;
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 $watch = $zkh->watch() if (defined($zkh));
35
36 skip 'no valid watch handle', 4 unless (defined($watch));
37
38
39 ## DESTROY()
40
41 my $attr = tied(%{$watch});
42
43 my $ret = $attr->DESTROY();
44 ok($ret,
45 'watch DESTROY(): destroyed inner watch hash');
46
47 $ret = $attr->DESTROY();
48 ok(!$ret,
49 'watch DESTROY(): no action on destroyed inner watch hash');
50
51 $ret = $watch->DESTROY();
52 ok(!$ret,
53 'watch DESTROY(): no action on watch handle with destroyed inner hash');
54
55 undef $watch;
56 ok(!defined($watch),
57 'undef: released watch handle with destroyed inner hash');
58 }
59
60 SKIP: {
61 my $zkh = Net::ZooKeeper->new($hosts);
62 my $watch = $zkh->watch() if (defined($zkh));
63
64 skip 'no valid watch handle', 37 unless (defined($watch));
65
66
67 ## TIEHASH(), UNTIE()
68
69 eval {
70 tie(%{$watch}, 'Net::ZooKeeper::Watch');
71 };
72 like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/,
73 'tie(): tying watch hashes not supported');
74
75 eval {
76 Net::ZooKeeper::Watch::TIEHASH('Net::ZooKeeper::Watch');
77 };
78 like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/,
79 'watch TIEHASH(): tying watch hashes not supported');
80
81 eval {
82 untie(%{$watch});
83 };
84 like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
85 'untie(): untying watch hashes not supported');
86
87 my $attr = tied(%{$watch});
88
89 eval {
90 $attr->UNTIE(0);
91 };
92 like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
93 'watch UNTIE(): untying watch hashes not supported');
94
95
96 ## FIRSTKEY(), NEXTKEY(), SCALAR()
97
98 my $copy_watch;
99 {
100 my %copy_watch = %{$watch};
101 $copy_watch = \%copy_watch;
102 }
103 bless($copy_watch, 'Net::ZooKeeper::Watch');
104 is(ref($copy_watch), 'Net::ZooKeeper::Watch',
105 'watch FIRSTKEY(), NEXTKEY(): copied dereferenced watch handle');
106
107 eval {
108 my $val = $copy_watch->FIRSTKEY();
109 };
110 like($@, qr/invalid handle/,
111 'watch FETCHKEY(): invalid watch handle');
112
113 eval {
114 my $val = $copy_watch->NEXTKEY('czxid');
115 };
116 like($@, qr/invalid handle/,
117 'watch NEXTKEY(): invalid watch handle');
118
119 my @keys = keys(%{$watch});
120 is(scalar(@keys), 3,
121 'keys(): count of keys from watch handle');
122
123 @keys = keys(%{$copy_watch});
124 is(scalar(@keys), 3,
125 'keys(): count of keys from copied dereferenced watch handle');
126
127 is($attr->FIRSTKEY(), 'timeout',
128 'watch FIRSTKEY(): retrieved first key using inner watch hash');
129
130 is($attr->NEXTKEY('event'), 'state',
131 'watch NEXTKEY(): retrieved last key using inner watch hash');
132
133 is($attr->NEXTKEY('state'), undef,
134 'NEXTKEY(): undef returned after last key using inner watch hash');
135
136 ok(scalar(%{$watch}),
137 'scalar(): true value returned for dereferenced watch handle');
138
139 ok($watch->SCALAR(),
140 'watch SCALAR(): true value returned');
141
142
143 ## FETCH()
144
145 eval {
146 my $val = $copy_watch->FETCH('version');
147 };
148 like($@, qr/invalid handle/,
149 'watch FETCH(): invalid watch handle');
150
151 {
152 my $msg;
153
154 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
155
156 my $val = $watch->{'foo'};
157 ok(!defined($val),
158 'watch FETCH(): undef returned for invalid element');
159
160 like($msg, qr/invalid element/,
161 'watch FETCH(): invalid element');
162 }
163
164 is($watch->{'timeout'}, 60000,
165 'watch FETCH(): default timeout');
166
167 is($watch->{'event'}, 0,
168 'watch FETCH(): default event');
169
170 is($watch->{'state'}, 0,
171 'watch FETCH(): default state');
172
173 is($attr->FETCH('timeout'), 60000,
174 'watch FETCH(): default timeout using inner watch hash');
175
176
177 ## STORE()
178
179 eval {
180 my $val = $copy_watch->STORE('version', 'foo');
181 };
182 like($@, qr/invalid handle/,
183 'watch STORE(): invalid watch handle');
184
185 {
186 my $msg;
187
188 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
189
190 $watch->{'foo'} = 'foo';
191 like($msg, qr/invalid element/,
192 'watch STORE(): invalid element');
193 }
194
195 {
196 my $msg;
197
198 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
199
200 $watch->{'event'} = 'foo';
201 like($msg, qr/read-only element: event/,
202 'watch STORE(): read-only event element');
203 }
204
205 {
206 my $msg;
207
208 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
209
210 $watch->{'state'} = 'foo';
211 like($msg, qr/read-only element: state/,
212 'watch STORE(): read-only state element');
213 }
214
215 $watch->{'timeout'} = 100;
216 is($watch->{'timeout'}, 100,
217 'watch STORE(): updated timeout');
218
219 $attr->STORE('timeout', 200);
220 is($watch->{'timeout'}, 200,
221 'watch STORE(): updated timeout using inner hash');
222
223
224 ## EXISTS()
225
226 eval {
227 my $val = $copy_watch->EXISTS('version');
228 };
229 like($@, qr/invalid handle/,
230 'watch EXISTS(): invalid watch handle');
231
232 ok(!exists($watch->{'foo'}),
233 'exists(): invalid element of watch handle');
234
235 ok(exists($watch->{'timeout'}),
236 'exists(): timeout');
237
238 ok(exists($watch->{'event'}),
239 'exists(): event');
240
241 ok(exists($watch->{'state'}),
242 'exists(): state');
243
244 ok($attr->EXISTS('timeout'),
245 'watch EXISTS(): timeout using inner watch hash');
246
247
248 ## DELETE(), CLEAR()
249
250 {
251 my $msg;
252
253 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
254
255 delete($watch->{'version'});
256 like($msg,
257 qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/,
258 'delete(): deleting watch hash elements not supported');
259 }
260
261 {
262 my $msg;
263
264 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
265
266 $watch->DELETE({'version'});
267 like($msg,
268 qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/,
269 'watch DELETE(): deleting watch hash elements not supported');
270 }
271
272 {
273 my $msg;
274
275 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
276
277 %{$watch} = ();
278 like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/,
279 'assign: clearing watch hashes not supported');
280 }
281
282 {
283 my $msg;
284
285 $SIG{'__WARN__'} = sub { $msg = $_[0]; };
286
287 $watch->CLEAR();
288 like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/,
289 'watch CLEAR(): clearing watch hashes not supported');
290 }
291 }
292