~juju/pyjuju/zookeeper-vendor

« back to all changes in this revision

Viewing changes to contrib/zkperl/t/20_tie.t

  • Committer: Gustavo Niemeyer
  • Date: 2011-05-24 20:53:37 UTC
  • Revision ID: gustavo@niemeyer.net-20110524205337-i11yow5biap5xapo
Importing stock Zookeeper 3.3.3 without jars.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 => 54;
 
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
 
 
35
    skip 'no valid handle', 4 unless (defined($zkh));
 
36
 
 
37
 
 
38
    ## DESTROY()
 
39
 
 
40
    my $attr = tied(%{$zkh});
 
41
 
 
42
    my $ret = $attr->DESTROY();
 
43
    ok($ret,
 
44
       'DESTROY(): destroyed inner hash');
 
45
 
 
46
    $ret = $attr->DESTROY();
 
47
    ok(!$ret,
 
48
       'DESTROY(): no action on destroyed inner hash');
 
49
 
 
50
    $ret = $zkh->DESTROY();
 
51
    ok(!$ret,
 
52
       'DESTROY(): no action on handle with destroyed inner hash');
 
53
 
 
54
    undef $zkh;
 
55
    ok(!defined($zkh),
 
56
       'undef: released handle with destroyed inner hash');
 
57
}
 
58
 
 
59
SKIP: {
 
60
    my $zkh = Net::ZooKeeper->new($hosts);
 
61
 
 
62
    skip 'no valid handle', 49 unless (defined($zkh));
 
63
 
 
64
 
 
65
    ## TIEHASH(), UNTIE()
 
66
 
 
67
    eval {
 
68
        tie(%{$zkh}, 'Net::ZooKeeper');
 
69
    };
 
70
    like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
 
71
         'tie(): tying hashes not supported');
 
72
 
 
73
    eval {
 
74
        Net::ZooKeeper::TIEHASH('Net::ZooKeeper');
 
75
    };
 
76
    like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
 
77
         'TIEHASH(): tying hashes not supported');
 
78
 
 
79
    eval {
 
80
        untie(%{$zkh});
 
81
    };
 
82
    like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
 
83
         'untie(): untying hashes not supported');
 
84
 
 
85
    my $attr = tied(%{$zkh});
 
86
 
 
87
    eval {
 
88
        $attr->UNTIE(0);
 
89
    };
 
90
    like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
 
91
         'UNTIE(): untying hashes not supported');
 
92
 
 
93
 
 
94
    ## FIRSTKEY(), NEXTKEY(), SCALAR()
 
95
 
 
96
    my $copy_zkh;
 
97
    {
 
98
        my %copy_zkh = %{$zkh};
 
99
        $copy_zkh = \%copy_zkh;
 
100
    }
 
101
    bless($copy_zkh, 'Net::ZooKeeper');
 
102
    is(ref($copy_zkh), 'Net::ZooKeeper',
 
103
       'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
 
104
 
 
105
    eval {
 
106
        my $val = $copy_zkh->FIRSTKEY();
 
107
    };
 
108
    like($@, qr/invalid handle/,
 
109
         'FETCHKEY(): invalid handle');
 
110
 
 
111
    eval {
 
112
        my $val = $copy_zkh->NEXTKEY('data_read_len');
 
113
    };
 
114
    like($@, qr/invalid handle/,
 
115
         'NEXTKEY(): invalid handle');
 
116
 
 
117
    my @keys = keys(%{$zkh});
 
118
    is(scalar(@keys), 7,
 
119
       'keys(): count of keys from handle');
 
120
 
 
121
    @keys = keys(%{$copy_zkh});
 
122
    is(scalar(@keys), 7,
 
123
       'keys(): count of keys from copied dereferenced handle');
 
124
 
 
125
    is($attr->FIRSTKEY(), 'data_read_len',
 
126
       'FIRSTKEY(): retrieved first key using inner hash');
 
127
 
 
128
    is($attr->NEXTKEY('session_id'), 'pending_watches',
 
129
       'NEXTKEY(): retrieved last key using inner hash');
 
130
 
 
131
    is($attr->NEXTKEY('pending_watches'), undef,
 
132
       'NEXTKEY(): undef returned after last key using inner hash');
 
133
 
 
134
    ok(scalar(%{$zkh}),
 
135
       'scalar(): true value returned for dereferenced handle');
 
136
 
 
137
    ok($zkh->SCALAR(),
 
138
       'SCALAR(): true value returned');
 
139
 
 
140
 
 
141
    ## FETCH()
 
142
 
 
143
    eval {
 
144
        my $val = $copy_zkh->FETCH('data_read_len');
 
145
    };
 
146
    like($@, qr/invalid handle/,
 
147
         'FETCH(): invalid handle');
 
148
 
 
149
    {
 
150
        my $msg;
 
151
 
 
152
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
153
 
 
154
        my $val = $zkh->{'foo'};
 
155
        ok(!defined($val),
 
156
           'FETCH(): undef returned for invalid element');
 
157
 
 
158
        like($msg, qr/invalid element/,
 
159
             'FETCH(): invalid element');
 
160
    }
 
161
 
 
162
    is($zkh->{'data_read_len'}, 1023,
 
163
       'FETCH(): default data read length');
 
164
 
 
165
    is($zkh->{'path_read_len'}, 1023,
 
166
       'FETCH(): default path read length');
 
167
 
 
168
    is($zkh->{'hosts'}, $hosts,
 
169
       'FETCH(): server hosts');
 
170
 
 
171
    is($zkh->{'session_timeout'}, 10000,
 
172
       'FETCH(): default session timeout');
 
173
 
 
174
    ok(defined($zkh->{'session_id'}),
 
175
       'FETCH(): session ID');
 
176
 
 
177
    SKIP: {
 
178
        my $zkh = Net::ZooKeeper->new('0.0.0.0:0');
 
179
 
 
180
        skip 'no valid handle with invalid host', 1 unless (defined($zkh));
 
181
 
 
182
        is($zkh->{'session_id'}, '',
 
183
           'FETCH(): empty session ID with invalid host');
 
184
    }
 
185
 
 
186
    is($zkh->{'pending_watches'}, 0,
 
187
       'FETCH(): default pending watch list length');
 
188
 
 
189
    is($attr->FETCH('data_read_len'), 1023,
 
190
       'FETCH(): default data read length using inner hash');
 
191
 
 
192
 
 
193
    ## STORE()
 
194
 
 
195
    eval {
 
196
        my $val = $copy_zkh->STORE('data_read_len', 'foo');
 
197
    };
 
198
    like($@, qr/invalid handle/,
 
199
         'STORE(): invalid handle');
 
200
 
 
201
    {
 
202
        my $msg;
 
203
 
 
204
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
205
 
 
206
        $zkh->{'foo'} = 'foo';
 
207
        like($msg, qr/invalid element/,
 
208
             'STORE(): invalid element');
 
209
    }
 
210
 
 
211
    eval {
 
212
        $zkh->{'data_read_len'} = -3;
 
213
    };
 
214
    like($@, qr/invalid data read length/,
 
215
         'STORE(): invalid data read length');
 
216
 
 
217
    eval {
 
218
        $zkh->{'path_read_len'} = -3;
 
219
    };
 
220
    like($@, qr/invalid path read length/,
 
221
         'STORE(): invalid path read length');
 
222
 
 
223
    {
 
224
        my $msg;
 
225
 
 
226
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
227
 
 
228
        $zkh->{'hosts'} = 'foo';
 
229
        like($msg, qr/read-only element: hosts/,
 
230
             'STORE(): read-only server hosts element');
 
231
    }
 
232
 
 
233
    {
 
234
        my $msg;
 
235
 
 
236
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
237
 
 
238
        $zkh->{'session_timeout'} = 0;
 
239
        like($msg, qr/read-only element: session_timeout/,
 
240
             'STORE(): read-only session timeout element');
 
241
    }
 
242
 
 
243
    {
 
244
        my $msg;
 
245
 
 
246
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
247
 
 
248
        $zkh->{'session_id'} = 'foo';
 
249
        like($msg, qr/read-only element: session_id/,
 
250
             'STORE(): read-only session ID element');
 
251
    }
 
252
 
 
253
    {
 
254
        my $msg;
 
255
 
 
256
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
257
 
 
258
        $zkh->{'pending_watches'} = 0;
 
259
        like($msg, qr/read-only element: pending_watches/,
 
260
             'STORE(): read-only pending watch list length element');
 
261
    }
 
262
 
 
263
    $zkh->{'data_read_len'} = 200;
 
264
    is($zkh->{'data_read_len'}, 200,
 
265
       'STORE(): updated data read length');
 
266
 
 
267
    $zkh->{'path_read_len'} = 100;
 
268
    is($zkh->{'path_read_len'}, 100,
 
269
       'STORE(): updated path read length');
 
270
 
 
271
    $attr->STORE('data_read_len', 100);
 
272
    is($zkh->{'data_read_len'}, 100,
 
273
       'STORE(): updated data read length using inner hash');
 
274
 
 
275
 
 
276
    ## EXISTS()
 
277
 
 
278
    eval {
 
279
        my $val = $copy_zkh->EXISTS('data_read_len');
 
280
    };
 
281
    like($@, qr/invalid handle/,
 
282
         'EXISTS(): invalid handle');
 
283
 
 
284
    ok(!exists($zkh->{'foo'}),
 
285
       'exists(): invalid element of handle');
 
286
 
 
287
    ok(exists($zkh->{'data_read_len'}),
 
288
       'exists(): data read length');
 
289
 
 
290
    ok(exists($zkh->{'path_read_len'}),
 
291
       'exists(): path read length');
 
292
 
 
293
    ok(exists($zkh->{'hosts'}),
 
294
       'exists(): server hosts');
 
295
 
 
296
    ok(exists($zkh->{'session_timeout'}),
 
297
       'exists(): session timeout');
 
298
 
 
299
    ok(exists($zkh->{'session_id'}),
 
300
       'exists(): session ID');
 
301
 
 
302
    ok(exists($zkh->{'pending_watches'}),
 
303
       'exists(): pending watch list length');
 
304
 
 
305
    ok($attr->EXISTS('data_read_len'),
 
306
       'EXISTS(): data read length using inner hash');
 
307
 
 
308
 
 
309
    ## DELETE(), CLEAR()
 
310
 
 
311
    {
 
312
        my $msg;
 
313
 
 
314
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
315
 
 
316
        delete($zkh->{'data_read_len'});
 
317
        like($msg,
 
318
             qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
 
319
             'delete(): deleting hash elements not supported');
 
320
    }
 
321
 
 
322
    {
 
323
        my $msg;
 
324
 
 
325
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
326
 
 
327
        $zkh->DELETE({'data_read_len'});
 
328
        like($msg,
 
329
             qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
 
330
             'DELETE(): deleting hash elements not supported');
 
331
    }
 
332
 
 
333
    {
 
334
        my $msg;
 
335
 
 
336
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
337
 
 
338
        %{$zkh} = ();
 
339
        like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
 
340
             'assign: clearing hashes not supported');
 
341
    }
 
342
 
 
343
    {
 
344
        my $msg;
 
345
 
 
346
        $SIG{'__WARN__'} = sub { $msg = $_[0]; };
 
347
 
 
348
        $zkh->CLEAR();
 
349
        like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
 
350
             'CLEAR(): clearing hashes not supported');
 
351
    }
 
352
}
 
353