40_basic.t 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 => 35;
  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. my $zkh = Net::ZooKeeper->new($hosts);
  26. my $path;
  27. SKIP: {
  28. my $ret = $zkh->exists($root_path) if (defined($zkh));
  29. skip 'no connection to ZooKeeper', 1 unless
  30. (defined($ret) and $ret);
  31. $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE);
  32. is($path, $node_path,
  33. 'create(): created node');
  34. }
  35. SKIP: {
  36. skip 'no connection to ZooKeeper', 21 unless
  37. (defined($path) and $path eq $node_path);
  38. ## exists()
  39. my $ret = $zkh->exists($node_path);
  40. ok($ret,
  41. 'exists(): checked node existence');
  42. $ret = $zkh->exists($node_path . '/NONE');
  43. ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  44. 'exists(): checked node non-existence');
  45. my $stat = $zkh->stat();
  46. $ret = $zkh->exists($node_path, 'stat' => $stat);
  47. ok(($ret and $stat->{'data_len'} == 3),
  48. 'exists(): checked node existence with stat handle');
  49. ## get()
  50. my $node = $zkh->get($node_path);
  51. is($node, 'foo',
  52. 'get(): retrieved node value');
  53. $node = $zkh->get($node_path . '/NONE');
  54. ok((!defined($node) and $zkh->get_error() == ZNONODE and $! eq ''),
  55. 'get(): undef returned for non-extant node');
  56. $node = $zkh->get($node_path, 'data_read_len', 2);
  57. is($node, 'fo',
  58. 'get(): retrieved truncated node value');
  59. $node = $zkh->get($node_path, 'data_read_len' => 0);
  60. is($node, '',
  61. 'get(): retrieved zero-length node value');
  62. $node = $zkh->get($node_path, 'stat' => $stat);
  63. ok(($node eq 'foo' and $stat->{'data_len'} == 3),
  64. 'get(): retrieved node value with stat handle');
  65. ## set()
  66. $ret = $zkh->set($node_path, 'foo');
  67. ok($ret,
  68. 'set(): set node value');
  69. SKIP: {
  70. my $ret = $zkh->exists($node_path, 'stat' => $stat);
  71. skip 'invalid node data', 2 unless
  72. ($ret and $stat->{'version'} == 1);
  73. $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'});
  74. ok($ret,
  75. 'set(): set node value with matching version');
  76. $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'});
  77. ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  78. 'set(): node value unchanged if non-matching version');
  79. }
  80. $ret = $zkh->set($node_path, 'foobaz', 'stat' => $stat);
  81. ok(($ret and $stat->{'data_len'} == 6),
  82. 'set(): retrieved node value with stat handle');
  83. ## create(), delete()
  84. $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE);
  85. ok((!defined($path) and $zkh->get_error() == ZNODEEXISTS and $! eq ''),
  86. 'create(): undef when attempting to create extant node');
  87. $ret = $zkh->delete($node_path . '/NONE');
  88. ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  89. 'delete(): no deletion of non-extant node');
  90. $ret = $zkh->delete($node_path);
  91. ok($ret,
  92. 'delete(): deleted node');
  93. my $path_read_len = length($node_path) - 2;
  94. $path = $zkh->create($node_path, 'foo',
  95. 'path_read_len' => $path_read_len,
  96. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  97. is($path, substr($node_path, 0, -2),
  98. 'create(): created node with small return path buffer');
  99. $path = $zkh->create("$node_path/s", 'foo',
  100. 'flags' => ZOO_SEQUENCE,
  101. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  102. like($path, qr/^$node_path\/s[0-9]+$/,
  103. 'create(): created sequential node');
  104. SKIP: {
  105. my $ret = $zkh->exists($path, 'stat' => $stat);
  106. unless ($ret and $stat->{'version'} == 0) {
  107. my $ret = $zkh->delete($path);
  108. diag(sprintf('unable to delete node %s: %d, %s',
  109. $path, $zkh->get_error(), $!)) unless ($ret);
  110. skip 'invalid node data', 2;
  111. }
  112. $ret = $zkh->delete($path, 'version' => ($stat->{'version'} + 1));
  113. ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  114. 'delete(): node not deleted if non-matching version');
  115. $ret = $zkh->delete($path, 'version' => $stat->{'version'});
  116. ok($ret,
  117. 'delete(): deleted sequential node with matching version');
  118. }
  119. $path = $zkh->create("$node_path/e", 'foo',
  120. 'flags' => ZOO_EPHEMERAL,
  121. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  122. is($path, "$node_path/e",
  123. 'create(): created ephemeral node');
  124. $path = $zkh->create("$node_path/es", 'foo',
  125. 'flags' => (ZOO_SEQUENCE | ZOO_EPHEMERAL),
  126. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  127. like($path, qr/^$node_path\/es[0-9]+$/,
  128. 'create(): created ephemeral sequential node');
  129. undef $zkh;
  130. }
  131. $zkh = Net::ZooKeeper->new($hosts);
  132. SKIP: {
  133. my $ret = $zkh->exists($node_path) if (defined($zkh));
  134. skip 'no connection to ZooKeeper', 12 unless
  135. (defined($ret) and $ret);
  136. $ret = $zkh->exists("$node_path/e");
  137. ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  138. 'exists(): checked ephemeral node non-extant after reconnection');
  139. $ret = $zkh->exists($path);
  140. ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''),
  141. 'exists(): checked ephemeral sequential node non-extant ' .
  142. 'after reconnection');
  143. ## get_children()
  144. my @child_paths = ('abc');
  145. @child_paths = $zkh->get_children($node_path);
  146. ok((@child_paths == 0 and $zkh->get_error() == ZOK),
  147. 'get_children(): retrieved empty list of child nodes');
  148. my $num_children = $zkh->get_children($node_path);
  149. ok((defined($num_children) and $num_children == 0),
  150. 'get_children(): retrieved zero count of child nodes');
  151. @child_paths = $zkh->get_children($node_path . '/NONE');
  152. ok((@child_paths == 0 and $zkh->get_error() == ZNONODE and $! eq ''),
  153. 'get_children(): empty list returned for non-extant node');
  154. $num_children = $zkh->get_children($node_path . '/NONE');
  155. ok((!defined($num_children) and $zkh->get_error() == ZNONODE and $! eq ''),
  156. 'get_children(): undef returned for non-extant node');
  157. SKIP: {
  158. my $path = $zkh->create("$node_path/c1", 'foo',
  159. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  160. skip 'no connection to ZooKeeper', 6 unless
  161. (defined($path) and $path eq "$node_path/c1");
  162. my @child_paths = ('abc');
  163. @child_paths = $zkh->get_children($node_path);
  164. ok((@child_paths == 1 and $child_paths[0] eq 'c1'),
  165. 'get_children(): retrieved list of single child node');
  166. my $num_children = $zkh->get_children($node_path);
  167. ok((defined($num_children) and $num_children == 1),
  168. 'get_children(): retrieved count of single child node');
  169. SKIP: {
  170. my $path = $zkh->create("$node_path/c2", 'foo',
  171. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  172. skip 'no connection to ZooKeeper', 2 unless
  173. (defined($path) and $path eq "$node_path/c2");
  174. my @child_paths = ('abc');
  175. @child_paths = $zkh->get_children($node_path);
  176. ok((@child_paths == 2 and $child_paths[0] eq 'c1' and
  177. $child_paths[1] eq 'c2'),
  178. 'get_children(): retrieved list of two child nodes');
  179. my $num_children = $zkh->get_children($node_path);
  180. ok((defined($num_children) and $num_children == 2),
  181. 'get_children(): retrieved count of two child nodes');
  182. my $ret = $zkh->delete("$node_path/c2");
  183. diag(sprintf('unable to delete node %s: %d, %s',
  184. "$node_path/c2", $zkh->get_error(), $!)) unless
  185. ($ret);
  186. }
  187. @child_paths = ('abc');
  188. @child_paths = $zkh->get_children($node_path);
  189. ok((@child_paths == 1 and $child_paths[0] eq 'c1'),
  190. 'get_children(): retrieved list of single child node');
  191. $num_children = $zkh->get_children($node_path);
  192. ok((defined($num_children) and $num_children == 1),
  193. 'get_children(): retrieved count of single child node');
  194. my $ret = $zkh->delete("$node_path/c1");
  195. diag(sprintf('unable to delete node %s: %d, %s',
  196. "$node_path/c1", $zkh->get_error(), $!)) unless ($ret);
  197. }
  198. ## cleanup
  199. $ret = $zkh->delete($node_path);
  200. diag(sprintf('unable to delete node %s: %d, %s',
  201. $node_path, $zkh->get_error(), $!)) unless ($ret);
  202. }