30_connect.t 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 => 29;
  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. ## new(), DESTROY()
  26. Net::ZooKeeper::set_deterministic_conn_order(1);
  27. my $zkh = Net::ZooKeeper->new($hosts);
  28. isa_ok($zkh, 'Net::ZooKeeper',
  29. 'new(): created handle');
  30. SKIP: {
  31. skip 'no valid handle', 3 unless (defined($zkh));
  32. my $ret = $zkh->DESTROY();
  33. ok($ret,
  34. 'DESTROY(): destroyed handle');
  35. $ret = $zkh->DESTROY();
  36. ok(!$ret,
  37. 'DESTROY(): no action on destroyed handle');
  38. undef $zkh;
  39. ok(!defined($zkh),
  40. 'undef: released handle');
  41. }
  42. Net::ZooKeeper::set_deterministic_conn_order(0);
  43. SKIP: {
  44. my $zkh = Net::ZooKeeper->new($hosts);
  45. skip 'no valid handle', 10 unless (defined($zkh));
  46. my $copy_zkh = $zkh;
  47. isa_ok($copy_zkh, 'Net::ZooKeeper',
  48. 'assign: copied handle');
  49. my $ret = $zkh->exists($root_path);
  50. ok(defined($ret),
  51. 'exists(): no error from original handle');
  52. undef $zkh;
  53. ok(!defined($zkh),
  54. 'undef: released original handle');
  55. $ret = $copy_zkh->exists($root_path);
  56. ok(defined($ret),
  57. 'exists(): no error from first copy of handle');
  58. $zkh = $copy_zkh;
  59. isa_ok($zkh, 'Net::ZooKeeper',
  60. 'assign: re-copied handle');
  61. $ret = $copy_zkh->DESTROY();
  62. ok($ret,
  63. 'DESTROY(): destroyed first copy of handle');
  64. eval {
  65. $zkh->exists($root_path);
  66. };
  67. like($@, qr/invalid handle/,
  68. 'exists(): invalid second copy of handle');
  69. undef $copy_zkh;
  70. ok(!defined($copy_zkh),
  71. 'undef: released first copy of handle');
  72. $ret = $zkh->DESTROY();
  73. ok(!$ret,
  74. 'DESTROY(): no action on second copy of destroyed handle');
  75. undef $zkh;
  76. ok(!defined($zkh),
  77. 'undef: released second copy of handle');
  78. }
  79. SKIP: {
  80. my $zkh = Net::ZooKeeper->new($hosts);
  81. skip 'no valid handle', 6 unless (defined($zkh));
  82. my $copy_zkh;
  83. {
  84. my %copy_zkh = %{$zkh};
  85. $copy_zkh = \%copy_zkh;
  86. }
  87. bless($copy_zkh, 'Net::ZooKeeper');
  88. isa_ok($copy_zkh, 'Net::ZooKeeper',
  89. 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle');
  90. eval {
  91. $copy_zkh->exists($root_path);
  92. };
  93. like($@, qr/invalid handle/,
  94. 'exists(): invalid copy of dereferenced handle');
  95. $ret = $copy_zkh->DESTROY();
  96. ok(!$ret,
  97. 'DESTROY(): no action on copy of dereferenced handle');
  98. undef $copy_zkh;
  99. ok(!defined($copy_zkh),
  100. 'undef: released copy of dereferenced handle');
  101. my $ret = $zkh->exists($root_path);
  102. ok(defined($ret),
  103. 'exists(): no error from original handle');
  104. undef $zkh;
  105. ok(!defined($zkh),
  106. 'undef: released original handle');
  107. }
  108. Net::ZooKeeper::set_deterministic_conn_order(1);
  109. my $zkh1 = Net::ZooKeeper->new($hosts, 'session_timeout' => 0x3FFF_FFFF);
  110. isa_ok($zkh1, 'Net::ZooKeeper',
  111. 'new(): created handle with maximum session timeout');
  112. SKIP: {
  113. my $ret = $zkh1->exists($root_path) if (defined($zkh1));
  114. skip 'no connection to ZooKeeper', 7 unless
  115. (defined($ret) and $ret);
  116. ## FETCH() of read-only attributes
  117. ok(($zkh1->{'session_timeout'} > 0 and
  118. $zkh1->{'session_timeout'} <= 0x3FFF_FFFF),
  119. 'FETCH(): session timeout reset after connection');
  120. my $session_id1 = $zkh1->{'session_id'};
  121. ok((length($session_id1) > 0),
  122. 'FETCH(): non-empty session ID after connection');
  123. SKIP: {
  124. skip 'no session ID after connection', 1 unless
  125. (length($session_id1) > 0);
  126. my @nonzero_bytes = grep($_ != 0, unpack('c' x length($session_id1),
  127. $session_id1));
  128. ok((@nonzero_bytes > 0),
  129. 'FETCH(): non-zero session ID after connection');
  130. }
  131. ## NOTE: to test re-connections with saved session IDs we create a second
  132. ## connection with the same ID while the first is still active;
  133. ## this is bad practice in normal usage
  134. ##
  135. ## Test disabled because it breaks with current ZooKeeper servers:
  136. ## $zkh1's connection gets closed as soon as $zkh2 connects, which
  137. ## causes it to reconnect, which kills $zkh2's connection, etc.
  138. ## TODO: figure out a way to test this.
  139. SKIP: {
  140. skip 'does not work with current ZK servers', 4;
  141. my $zkh2 = Net::ZooKeeper->new($hosts,
  142. 'session_id' => $session_id1,
  143. 'session_timeout' => 20000);
  144. isa_ok($zkh2, 'Net::ZooKeeper',
  145. 'new(): created handle with session ID and valid session timeout');
  146. $ret = $zkh2->exists($root_path);
  147. ok($ret,
  148. 'new(): reconnection with session ID');
  149. SKIP: {
  150. skip 'no connection to ZooKeeper', 2 unless ($ret);
  151. is($zkh2->{'session_timeout'}, 20000,
  152. 'FETCH(): session timeout unchanged after connection');
  153. my $session_id2 = $zkh2->{'session_id'};
  154. ok((length($session_id2) == length($session_id1)
  155. and $session_id2 eq $session_id1),
  156. 'FETCH(): reconnect with session ID');
  157. }
  158. }
  159. }