"Fossies" - the Fresh Open Source Software Archive 
Member "apache-zookeeper-3.8.1/zookeeper-contrib/zookeeper-contrib-zkperl/t/30_connect.t" (25 Jan 2023, 6097 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.
See also the last
Fossies "Diffs" side-by-side code changes report for "30_connect.t":
3.6.2_vs_3.7.0.
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 => 29;
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 ## new(), DESTROY()
33
34 Net::ZooKeeper::set_deterministic_conn_order(1);
35
36 my $zkh = Net::ZooKeeper->new($hosts);
37 isa_ok($zkh, 'Net::ZooKeeper',
38 'new(): created handle');
39
40 SKIP: {
41 skip 'no valid handle', 3 unless (defined($zkh));
42
43 my $ret = $zkh->DESTROY();
44 ok($ret,
45 'DESTROY(): destroyed handle');
46
47 $ret = $zkh->DESTROY();
48 ok(!$ret,
49 'DESTROY(): no action on destroyed handle');
50
51 undef $zkh;
52 ok(!defined($zkh),
53 'undef: released handle');
54 }
55
56 Net::ZooKeeper::set_deterministic_conn_order(0);
57
58 SKIP: {
59 my $zkh = Net::ZooKeeper->new($hosts);
60
61 skip 'no valid handle', 10 unless (defined($zkh));
62
63 my $copy_zkh = $zkh;
64 isa_ok($copy_zkh, 'Net::ZooKeeper',
65 'assign: copied handle');
66
67 my $ret = $zkh->exists($root_path);
68 ok(defined($ret),
69 'exists(): no error from original handle');
70
71 undef $zkh;
72 ok(!defined($zkh),
73 'undef: released original handle');
74
75 $ret = $copy_zkh->exists($root_path);
76 ok(defined($ret),
77 'exists(): no error from first copy of handle');
78
79 $zkh = $copy_zkh;
80 isa_ok($zkh, 'Net::ZooKeeper',
81 'assign: re-copied handle');
82
83 $ret = $copy_zkh->DESTROY();
84 ok($ret,
85 'DESTROY(): destroyed first copy of handle');
86
87 eval {
88 $zkh->exists($root_path);
89 };
90 like($@, qr/invalid handle/,
91 'exists(): invalid second copy of handle');
92
93 undef $copy_zkh;
94 ok(!defined($copy_zkh),
95 'undef: released first copy of handle');
96
97 $ret = $zkh->DESTROY();
98 ok(!$ret,
99 'DESTROY(): no action on second copy of destroyed handle');
100
101 undef $zkh;
102 ok(!defined($zkh),
103 'undef: released second copy of handle');
104 }
105
106 SKIP: {
107 my $zkh = Net::ZooKeeper->new($hosts);
108
109 skip 'no valid handle', 6 unless (defined($zkh));
110
111 my $copy_zkh;
112 {
113 my %copy_zkh = %{$zkh};
114 $copy_zkh = \%copy_zkh;
115 }
116 bless($copy_zkh, 'Net::ZooKeeper');
117 isa_ok($copy_zkh, 'Net::ZooKeeper',
118 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
119
120 eval {
121 $copy_zkh->exists($root_path);
122 };
123 like($@, qr/invalid handle/,
124 'exists(): invalid copy of dereferenced handle');
125
126 $ret = $copy_zkh->DESTROY();
127 ok(!$ret,
128 'DESTROY(): no action on copy of dereferenced handle');
129
130 undef $copy_zkh;
131 ok(!defined($copy_zkh),
132 'undef: released copy of dereferenced handle');
133
134 my $ret = $zkh->exists($root_path);
135 ok(defined($ret),
136 'exists(): no error from original handle');
137
138 undef $zkh;
139 ok(!defined($zkh),
140 'undef: released original handle');
141 }
142
143 Net::ZooKeeper::set_deterministic_conn_order(1);
144
145 my $zkh1 = Net::ZooKeeper->new($hosts, 'session_timeout' => 0x3FFF_FFFF);
146 isa_ok($zkh1, 'Net::ZooKeeper',
147 'new(): created handle with maximum session timeout');
148
149 SKIP: {
150 my $ret = $zkh1->exists($root_path) if (defined($zkh1));
151
152 skip 'no connection to ZooKeeper', 7 unless
153 (defined($ret) and $ret);
154
155
156 ## FETCH() of read-only attributes
157
158 ok(($zkh1->{'session_timeout'} > 0 and
159 $zkh1->{'session_timeout'} <= 0x3FFF_FFFF),
160 'FETCH(): session timeout reset after connection');
161
162 my $session_id1 = $zkh1->{'session_id'};
163 ok((length($session_id1) > 0),
164 'FETCH(): non-empty session ID after connection');
165
166 SKIP: {
167 skip 'no session ID after connection', 1 unless
168 (length($session_id1) > 0);
169
170 my @nonzero_bytes = grep($_ != 0, unpack('c' x length($session_id1),
171 $session_id1));
172 ok((@nonzero_bytes > 0),
173 'FETCH(): non-zero session ID after connection');
174 }
175
176 ## NOTE: to test re-connections with saved session IDs we create a second
177 ## connection with the same ID while the first is still active;
178 ## this is bad practice in normal usage
179 ##
180 ## Test disabled because it breaks with current ZooKeeper servers:
181 ## $zkh1's connection gets closed as soon as $zkh2 connects, which
182 ## causes it to reconnect, which kills $zkh2's connection, etc.
183 ## TODO: figure out a way to test this.
184 SKIP: {
185 skip 'does not work with current ZK servers', 4;
186
187 my $zkh2 = Net::ZooKeeper->new($hosts,
188 'session_id' => $session_id1,
189 'session_timeout' => 20000);
190 isa_ok($zkh2, 'Net::ZooKeeper',
191 'new(): created handle with session ID and valid session timeout');
192
193 $ret = $zkh2->exists($root_path);
194 ok($ret,
195 'new(): reconnection with session ID');
196
197 SKIP: {
198 skip 'no connection to ZooKeeper', 2 unless ($ret);
199
200 is($zkh2->{'session_timeout'}, 20000,
201 'FETCH(): session timeout unchanged after connection');
202
203 my $session_id2 = $zkh2->{'session_id'};
204 ok((length($session_id2) == length($session_id1)
205 and $session_id2 eq $session_id1),
206 'FETCH(): reconnect with session ID');
207 }
208 }
209 }
210