24_watch_tie.t 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 => 42;
  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 $watch = $zkh->watch() if (defined($zkh));
  28. skip 'no valid watch handle', 4 unless (defined($watch));
  29. ## DESTROY()
  30. my $attr = tied(%{$watch});
  31. my $ret = $attr->DESTROY();
  32. ok($ret,
  33. 'watch DESTROY(): destroyed inner watch hash');
  34. $ret = $attr->DESTROY();
  35. ok(!$ret,
  36. 'watch DESTROY(): no action on destroyed inner watch hash');
  37. $ret = $watch->DESTROY();
  38. ok(!$ret,
  39. 'watch DESTROY(): no action on watch handle with destroyed inner hash');
  40. undef $watch;
  41. ok(!defined($watch),
  42. 'undef: released watch handle with destroyed inner hash');
  43. }
  44. SKIP: {
  45. my $zkh = Net::ZooKeeper->new($hosts);
  46. my $watch = $zkh->watch() if (defined($zkh));
  47. skip 'no valid watch handle', 37 unless (defined($watch));
  48. ## TIEHASH(), UNTIE()
  49. eval {
  50. tie(%{$watch}, 'Net::ZooKeeper::Watch');
  51. };
  52. like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/,
  53. 'tie(): tying watch hashes not supported');
  54. eval {
  55. Net::ZooKeeper::Watch::TIEHASH('Net::ZooKeeper::Watch');
  56. };
  57. like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/,
  58. 'watch TIEHASH(): tying watch hashes not supported');
  59. eval {
  60. untie(%{$watch});
  61. };
  62. like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
  63. 'untie(): untying watch hashes not supported');
  64. my $attr = tied(%{$watch});
  65. eval {
  66. $attr->UNTIE(0);
  67. };
  68. like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/,
  69. 'watch UNTIE(): untying watch hashes not supported');
  70. ## FIRSTKEY(), NEXTKEY(), SCALAR()
  71. my $copy_watch;
  72. {
  73. my %copy_watch = %{$watch};
  74. $copy_watch = \%copy_watch;
  75. }
  76. bless($copy_watch, 'Net::ZooKeeper::Watch');
  77. is(ref($copy_watch), 'Net::ZooKeeper::Watch',
  78. 'watch FIRSTKEY(), NEXTKEY(): copied dereferenced watch handle');
  79. eval {
  80. my $val = $copy_watch->FIRSTKEY();
  81. };
  82. like($@, qr/invalid handle/,
  83. 'watch FETCHKEY(): invalid watch handle');
  84. eval {
  85. my $val = $copy_watch->NEXTKEY('czxid');
  86. };
  87. like($@, qr/invalid handle/,
  88. 'watch NEXTKEY(): invalid watch handle');
  89. my @keys = keys(%{$watch});
  90. is(scalar(@keys), 3,
  91. 'keys(): count of keys from watch handle');
  92. @keys = keys(%{$copy_watch});
  93. is(scalar(@keys), 3,
  94. 'keys(): count of keys from copied dereferenced watch handle');
  95. is($attr->FIRSTKEY(), 'timeout',
  96. 'watch FIRSTKEY(): retrieved first key using inner watch hash');
  97. is($attr->NEXTKEY('event'), 'state',
  98. 'watch NEXTKEY(): retrieved last key using inner watch hash');
  99. is($attr->NEXTKEY('state'), undef,
  100. 'NEXTKEY(): undef returned after last key using inner watch hash');
  101. ok(scalar(%{$watch}),
  102. 'scalar(): true value returned for dereferenced watch handle');
  103. ok($watch->SCALAR(),
  104. 'watch SCALAR(): true value returned');
  105. ## FETCH()
  106. eval {
  107. my $val = $copy_watch->FETCH('version');
  108. };
  109. like($@, qr/invalid handle/,
  110. 'watch FETCH(): invalid watch handle');
  111. {
  112. my $msg;
  113. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  114. my $val = $watch->{'foo'};
  115. ok(!defined($val),
  116. 'watch FETCH(): undef returned for invalid element');
  117. like($msg, qr/invalid element/,
  118. 'watch FETCH(): invalid element');
  119. }
  120. is($watch->{'timeout'}, 60000,
  121. 'watch FETCH(): default timeout');
  122. is($watch->{'event'}, 0,
  123. 'watch FETCH(): default event');
  124. is($watch->{'state'}, 0,
  125. 'watch FETCH(): default state');
  126. is($attr->FETCH('timeout'), 60000,
  127. 'watch FETCH(): default timeout using inner watch hash');
  128. ## STORE()
  129. eval {
  130. my $val = $copy_watch->STORE('version', 'foo');
  131. };
  132. like($@, qr/invalid handle/,
  133. 'watch STORE(): invalid watch handle');
  134. {
  135. my $msg;
  136. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  137. $watch->{'foo'} = 'foo';
  138. like($msg, qr/invalid element/,
  139. 'watch STORE(): invalid element');
  140. }
  141. {
  142. my $msg;
  143. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  144. $watch->{'event'} = 'foo';
  145. like($msg, qr/read-only element: event/,
  146. 'watch STORE(): read-only event element');
  147. }
  148. {
  149. my $msg;
  150. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  151. $watch->{'state'} = 'foo';
  152. like($msg, qr/read-only element: state/,
  153. 'watch STORE(): read-only state element');
  154. }
  155. $watch->{'timeout'} = 100;
  156. is($watch->{'timeout'}, 100,
  157. 'watch STORE(): updated timeout');
  158. $attr->STORE('timeout', 200);
  159. is($watch->{'timeout'}, 200,
  160. 'watch STORE(): updated timeout using inner hash');
  161. ## EXISTS()
  162. eval {
  163. my $val = $copy_watch->EXISTS('version');
  164. };
  165. like($@, qr/invalid handle/,
  166. 'watch EXISTS(): invalid watch handle');
  167. ok(!exists($watch->{'foo'}),
  168. 'exists(): invalid element of watch handle');
  169. ok(exists($watch->{'timeout'}),
  170. 'exists(): timeout');
  171. ok(exists($watch->{'event'}),
  172. 'exists(): event');
  173. ok(exists($watch->{'state'}),
  174. 'exists(): state');
  175. ok($attr->EXISTS('timeout'),
  176. 'watch EXISTS(): timeout using inner watch hash');
  177. ## DELETE(), CLEAR()
  178. {
  179. my $msg;
  180. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  181. delete($watch->{'version'});
  182. like($msg,
  183. qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/,
  184. 'delete(): deleting watch hash elements not supported');
  185. }
  186. {
  187. my $msg;
  188. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  189. $watch->DELETE({'version'});
  190. like($msg,
  191. qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/,
  192. 'watch DELETE(): deleting watch hash elements not supported');
  193. }
  194. {
  195. my $msg;
  196. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  197. %{$watch} = ();
  198. like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/,
  199. 'assign: clearing watch hashes not supported');
  200. }
  201. {
  202. my $msg;
  203. $SIG{'__WARN__'} = sub { $msg = $_[0]; };
  204. $watch->CLEAR();
  205. like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/,
  206. 'watch CLEAR(): clearing watch hashes not supported');
  207. }
  208. }