60_watch.t 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 => 30;
  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 $path = $zkh->create($node_path, 'foo',
  28. 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh));
  29. skip 'no connection to ZooKeeper', 20 unless
  30. (defined($path) and $path eq $node_path);
  31. ## exists()
  32. $zkh->{'watch_timeout'} = 100;
  33. my $watch = $zkh->watch();
  34. my $ret = $zkh->exists($node_path, 'watch' => $watch);
  35. ok($ret,
  36. 'exists(): checked node existence with watch handle');
  37. $ret = $watch->wait();
  38. ok(!$ret,
  39. 'wait(): watch after checking node existence timed out');
  40. $ret = $zkh->exists($node_path, 'watch' => $watch);
  41. ok($ret,
  42. 'exists(): checked node existence with renewed watch handle');
  43. $ret = $watch->wait();
  44. ok(!$ret,
  45. 'wait(): watch after checking node existence timed out with ' .
  46. 'renewed watch handle');
  47. undef $watch;
  48. ok(!defined($watch),
  49. 'undef: released watch handle');
  50. my $pending_watches = $zkh->{'pending_watches'};
  51. is($pending_watches, 2,
  52. '_zk_release_watches(): report pending watches');
  53. ## get_children()
  54. $watch = $zkh->watch('timeout' => 50);
  55. my $num_children = $zkh->get_children($node_path, 'watch' => $watch);
  56. ok((defined($num_children) and $num_children == 0),
  57. 'get_children(): retrieved zero count of child nodes with ' .
  58. 'watch handle');
  59. $ret = $watch->wait();
  60. ok(!$ret,
  61. 'wait(): watch after retrieving child nodes timed out with ' .
  62. 'watch handle');
  63. $watch->{'timeout'} = 100;
  64. my @child_paths = $zkh->get_children($node_path, 'watch' => $watch);
  65. ok((@child_paths == 0),
  66. 'get_children(): retrieved empty list of child nodes with ' .
  67. 'renewed watch handle');
  68. $ret = $watch->wait();
  69. ok(!$ret,
  70. 'wait(): watch after retrieving child nodes timed out with ' .
  71. 'renewed watch handle');
  72. $pending_watches = $zkh->{'pending_watches'};
  73. is($pending_watches, 4,
  74. '_zk_release_watches(): report pending watches');
  75. ## get()
  76. $watch = $zkh->watch();
  77. my $node = $zkh->get($node_path, 'watch' => $watch);
  78. is($node, 'foo',
  79. 'get(): retrieved node value with watch handle');
  80. $ret = $watch->wait('timeout' => 0);
  81. ok(!$ret,
  82. 'wait(): watch after retrieving node value timed out with ' .
  83. 'watch handle');
  84. $node = $zkh->get($node_path, 'watch' => $watch);
  85. is($node, 'foo',
  86. 'get(): retrieved node value with renewed watch handle');
  87. $ret = $watch->wait();
  88. ok(!$ret,
  89. 'wait(): watch after retrieving node value timed out with ' .
  90. 'renewed watch handle');
  91. $pending_watches = $zkh->{'pending_watches'};
  92. is($pending_watches, 6,
  93. '_zk_release_watches(): all watches pending');
  94. ## _zk_release_watches()
  95. $ret = $zkh->DESTROY();
  96. ok($ret,
  97. 'DESTROY(): destroyed handle with pending watches');
  98. my $event = $watch->{'event'};
  99. is($event, 0,
  100. '_zk_release_watches(): watch not destroyed when tied to watch handle');
  101. $zkh = Net::ZooKeeper->new($hosts);
  102. SKIP: {
  103. my $ret = $zkh->exists($node_path, 'watch' => $watch);
  104. skip 'no connection to ZooKeeper', 2 unless
  105. (defined($ret) and $ret);
  106. ok($ret,
  107. 'exists(): checked node existence with renewed watch handle ' .
  108. 'from prior connection');
  109. $ret = $watch->wait();
  110. ok(!$ret,
  111. 'wait(): watch after checking node existence timed out with ' .
  112. 'renewed watch handle from prior connection');
  113. }
  114. }
  115. my $pid = fork();
  116. SKIP: {
  117. skip 'unable to fork', 4 unless (defined($pid));
  118. my $zkh = Net::ZooKeeper->new($hosts);
  119. my $ret = $zkh->exists($node_path) if (defined($zkh));
  120. if ($pid == 0) {
  121. ## child process
  122. my $code = 0;
  123. if (defined($ret) and $ret) {
  124. sleep(1);
  125. my $ret = $zkh->set($node_path, 'foo');
  126. diag(sprintf('set(): failed in child process: %d, %s',
  127. $zkh->get_error(), $!)) unless ($ret);
  128. $code = !$ret;
  129. sleep(1);
  130. my $path = $zkh->create("$node_path/c", 'foo',
  131. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  132. diag(sprintf('create(): failed in child process: %d, %s',
  133. $zkh->get_error(), $!)) unless
  134. (defined($path) and $path eq "$node_path/c");
  135. $code &= !$ret;
  136. sleep(1);
  137. $ret = $zkh->delete("$node_path/c");
  138. diag(sprintf('delete(): failed in child process: %d, %s',
  139. $zkh->get_error(), $!)) unless ($ret);
  140. $code &= !$ret;
  141. sleep(1);
  142. $ret = $zkh->set($node_path, 'foo');
  143. diag(sprintf('set(): failed in child process: %d, %s',
  144. $zkh->get_error(), $!)) unless ($ret);
  145. $code &= !$ret;
  146. }
  147. exit($code);
  148. }
  149. else {
  150. ## parent process
  151. SKIP: {
  152. skip 'no connection to ZooKeeper', 9 unless
  153. (defined($ret) and $ret);
  154. my $watch = $zkh->watch('timeout' => 5000);
  155. ## wait()
  156. my $ret = $zkh->exists($node_path, 'watch' => $watch);
  157. ok($ret,
  158. 'exists(): checked node existence with watch handle ' .
  159. 'in parent');
  160. $ret = $watch->wait();
  161. ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and
  162. $watch->{'state'} == ZOO_CONNECTED_STATE),
  163. 'wait(): waited for event after checking node existence');
  164. my $num_children = $zkh->get_children($node_path,
  165. 'watch' => $watch);
  166. ok((defined($num_children) and $num_children == 0),
  167. 'get_children(): retrieved zero count of child nodes with ' .
  168. 'watch handle in parent');
  169. $ret = $watch->wait();
  170. ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and
  171. $watch->{'state'} == ZOO_CONNECTED_STATE),
  172. 'wait(): waited for create child event after ' .
  173. 'retrieving child nodes');
  174. my @child_paths = $zkh->get_children($node_path,
  175. 'watch' => $watch);
  176. ok((@child_paths == 1 and $child_paths[0] eq 'c'),
  177. 'get_children(): retrieved list of child nodes with ' .
  178. 'watch handle in parent');
  179. $ret = $watch->wait();
  180. ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and
  181. $watch->{'state'} == ZOO_CONNECTED_STATE),
  182. 'wait(): waited for delete child event after ' .
  183. 'retrieving child nodes');
  184. my $node = $zkh->get($node_path, 'watch' => $watch);
  185. is($node, 'foo',
  186. 'get(): retrieved node value with watch handle in parent');
  187. $ret = $watch->wait();
  188. ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and
  189. $watch->{'state'} == ZOO_CONNECTED_STATE),
  190. 'wait(): waited for event after retrieving node value');
  191. undef $watch;
  192. my $pending_watches = $zkh->{'pending_watches'};
  193. is($pending_watches, 0,
  194. '_zk_release_watches(): no watches pending');
  195. }
  196. my $reap = waitpid($pid, 0);
  197. diag(sprintf('child process failed: exit %d, signal %d%s',
  198. ($? >> 8), ($? & 127),
  199. (($? & 128) ? ', core dump' : ''))) if
  200. ($reap == $pid and $? != 0);
  201. }
  202. }
  203. ## cleanup
  204. {
  205. my $zkh = Net::ZooKeeper->new($hosts);
  206. my $ret = $zkh->exists($node_path) if (defined($zkh));
  207. if (defined($ret) and $ret) {
  208. $ret = $zkh->delete($node_path);
  209. diag(sprintf('unable to delete node %s: %d, %s',
  210. $node_path, $zkh->get_error(), $!)) unless ($ret);
  211. }
  212. }