22_stat_tie.t 11 KB

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