20_tie.t 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. use File::Spec;
  19. use Test::More tests => 54;
  20. BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
  21. my $test_dir;
  22. (undef, $test_dir, undef) = File::Spec->splitpath($0);
  23. require File::Spec->catfile($test_dir, 'util.pl');
  24. my($hosts, $root_path, $node_path) = zk_test_setup(0);
  25. SKIP: {
  26. my $zkh = Net::ZooKeeper->new($hosts);
  27. skip 'no valid handle', 4 unless (defined($zkh));
  28. ## DESTROY()
  29. my $attr = tied(%{$zkh});
  30. my $ret = $attr->DESTROY();
  31. ok($ret,
  32. 'DESTROY(): destroyed inner hash');
  33. $ret = $attr->DESTROY();
  34. ok(!$ret,
  35. 'DESTROY(): no action on destroyed inner hash');
  36. $ret = $zkh->DESTROY();
  37. ok(!$ret,
  38. 'DESTROY(): no action on handle with destroyed inner hash');
  39. undef $zkh;
  40. ok(!defined($zkh),
  41. 'undef: released handle with destroyed inner hash');
  42. }
  43. SKIP: {
  44. my $zkh = Net::ZooKeeper->new($hosts);
  45. skip 'no valid handle', 49 unless (defined($zkh));
  46. ## TIEHASH(), UNTIE()
  47. eval {
  48. tie(%{$zkh}, 'Net::ZooKeeper');
  49. };
  50. like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
  51. 'tie(): tying hashes not supported');
  52. eval {
  53. Net::ZooKeeper::TIEHASH('Net::ZooKeeper');
  54. };
  55. like($@, qr/tying hashes of class Net::ZooKeeper not supported/,
  56. 'TIEHASH(): tying hashes not supported');
  57. eval {
  58. untie(%{$zkh});
  59. };
  60. like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
  61. 'untie(): untying hashes not supported');
  62. my $attr = tied(%{$zkh});
  63. eval {
  64. $attr->UNTIE(0);
  65. };
  66. like($@, qr/untying hashes of class Net::ZooKeeper not supported/,
  67. 'UNTIE(): untying hashes not supported');
  68. ## FIRSTKEY(), NEXTKEY(), SCALAR()
  69. my $copy_zkh;
  70. {
  71. my %copy_zkh = %{$zkh};
  72. $copy_zkh = \%copy_zkh;
  73. }
  74. bless($copy_zkh, 'Net::ZooKeeper');
  75. is(ref($copy_zkh), 'Net::ZooKeeper',
  76. 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
  77. eval {
  78. my $val = $copy_zkh->FIRSTKEY();
  79. };
  80. like($@, qr/invalid handle/,
  81. 'FETCHKEY(): invalid handle');
  82. eval {
  83. my $val = $copy_zkh->NEXTKEY('data_read_len');
  84. };
  85. like($@, qr/invalid handle/,
  86. 'NEXTKEY(): invalid handle');
  87. my @keys = keys(%{$zkh});
  88. is(scalar(@keys), 7,
  89. 'keys(): count of keys from handle');
  90. @keys = keys(%{$copy_zkh});
  91. is(scalar(@keys), 7,
  92. 'keys(): count of keys from copied dereferenced handle');
  93. is($attr->FIRSTKEY(), 'data_read_len',
  94. 'FIRSTKEY(): retrieved first key using inner hash');
  95. is($attr->NEXTKEY('session_id'), 'pending_watches',
  96. 'NEXTKEY(): retrieved last key using inner hash');
  97. is($attr->NEXTKEY('pending_watches'), undef,
  98. 'NEXTKEY(): undef returned after last key using inner hash');
  99. ok(scalar(%{$zkh}),
  100. 'scalar(): true value returned for dereferenced handle');
  101. ok($zkh->SCALAR(),
  102. 'SCALAR(): true value returned');
  103. ## FETCH()
  104. eval {
  105. my $val = $copy_zkh->FETCH('data_read_len');
  106. };
  107. like($@, qr/invalid handle/,
  108. 'FETCH(): invalid handle');
  109. {
  110. my $msg;
  111. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  112. my $val = $zkh->{'foo'};
  113. ok(!defined($val),
  114. 'FETCH(): undef returned for invalid element');
  115. like($msg, qr/invalid element/,
  116. 'FETCH(): invalid element');
  117. }
  118. is($zkh->{'data_read_len'}, 1023,
  119. 'FETCH(): default data read length');
  120. is($zkh->{'path_read_len'}, 1023,
  121. 'FETCH(): default path read length');
  122. is($zkh->{'hosts'}, $hosts,
  123. 'FETCH(): server hosts');
  124. is($zkh->{'session_timeout'}, 10000,
  125. 'FETCH(): default session timeout');
  126. ok(defined($zkh->{'session_id'}),
  127. 'FETCH(): session ID');
  128. SKIP: {
  129. my $zkh = Net::ZooKeeper->new('0.0.0.0:0');
  130. skip 'no valid handle with invalid host', 1 unless (defined($zkh));
  131. is($zkh->{'session_id'}, '',
  132. 'FETCH(): empty session ID with invalid host');
  133. }
  134. is($zkh->{'pending_watches'}, 0,
  135. 'FETCH(): default pending watch list length');
  136. is($attr->FETCH('data_read_len'), 1023,
  137. 'FETCH(): default data read length using inner hash');
  138. ## STORE()
  139. eval {
  140. my $val = $copy_zkh->STORE('data_read_len', 'foo');
  141. };
  142. like($@, qr/invalid handle/,
  143. 'STORE(): invalid handle');
  144. {
  145. my $msg;
  146. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  147. $zkh->{'foo'} = 'foo';
  148. like($msg, qr/invalid element/,
  149. 'STORE(): invalid element');
  150. }
  151. eval {
  152. $zkh->{'data_read_len'} = -3;
  153. };
  154. like($@, qr/invalid data read length/,
  155. 'STORE(): invalid data read length');
  156. eval {
  157. $zkh->{'path_read_len'} = -3;
  158. };
  159. like($@, qr/invalid path read length/,
  160. 'STORE(): invalid path read length');
  161. {
  162. my $msg;
  163. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  164. $zkh->{'hosts'} = 'foo';
  165. like($msg, qr/read-only element: hosts/,
  166. 'STORE(): read-only server hosts element');
  167. }
  168. {
  169. my $msg;
  170. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  171. $zkh->{'session_timeout'} = 0;
  172. like($msg, qr/read-only element: session_timeout/,
  173. 'STORE(): read-only session timeout element');
  174. }
  175. {
  176. my $msg;
  177. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  178. $zkh->{'session_id'} = 'foo';
  179. like($msg, qr/read-only element: session_id/,
  180. 'STORE(): read-only session ID element');
  181. }
  182. {
  183. my $msg;
  184. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  185. $zkh->{'pending_watches'} = 0;
  186. like($msg, qr/read-only element: pending_watches/,
  187. 'STORE(): read-only pending watch list length element');
  188. }
  189. $zkh->{'data_read_len'} = 200;
  190. is($zkh->{'data_read_len'}, 200,
  191. 'STORE(): updated data read length');
  192. $zkh->{'path_read_len'} = 100;
  193. is($zkh->{'path_read_len'}, 100,
  194. 'STORE(): updated path read length');
  195. $attr->STORE('data_read_len', 100);
  196. is($zkh->{'data_read_len'}, 100,
  197. 'STORE(): updated data read length using inner hash');
  198. ## EXISTS()
  199. eval {
  200. my $val = $copy_zkh->EXISTS('data_read_len');
  201. };
  202. like($@, qr/invalid handle/,
  203. 'EXISTS(): invalid handle');
  204. ok(!exists($zkh->{'foo'}),
  205. 'exists(): invalid element of handle');
  206. ok(exists($zkh->{'data_read_len'}),
  207. 'exists(): data read length');
  208. ok(exists($zkh->{'path_read_len'}),
  209. 'exists(): path read length');
  210. ok(exists($zkh->{'hosts'}),
  211. 'exists(): server hosts');
  212. ok(exists($zkh->{'session_timeout'}),
  213. 'exists(): session timeout');
  214. ok(exists($zkh->{'session_id'}),
  215. 'exists(): session ID');
  216. ok(exists($zkh->{'pending_watches'}),
  217. 'exists(): pending watch list length');
  218. ok($attr->EXISTS('data_read_len'),
  219. 'EXISTS(): data read length using inner hash');
  220. ## DELETE(), CLEAR()
  221. {
  222. my $msg;
  223. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  224. delete($zkh->{'data_read_len'});
  225. like($msg,
  226. qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
  227. 'delete(): deleting hash elements not supported');
  228. }
  229. {
  230. my $msg;
  231. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  232. $zkh->DELETE({'data_read_len'});
  233. like($msg,
  234. qr/deleting elements from hashes of class Net::ZooKeeper not supported/,
  235. 'DELETE(): deleting hash elements not supported');
  236. }
  237. {
  238. my $msg;
  239. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  240. %{$zkh} = ();
  241. like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
  242. 'assign: clearing hashes not supported');
  243. }
  244. {
  245. my $msg;
  246. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  247. $zkh->CLEAR();
  248. like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/,
  249. 'CLEAR(): clearing hashes not supported');
  250. }
  251. }