15_thread.t 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Config;
  19. use File::Spec;
  20. use Test::More;
  21. BEGIN {
  22. if ($Config{'useithreads'}) {
  23. plan tests => 10;
  24. }
  25. else {
  26. plan skip_all => 'no thread support';
  27. }
  28. }
  29. use threads;
  30. BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
  31. my $test_dir;
  32. (undef, $test_dir, undef) = File::Spec->splitpath($0);
  33. require File::Spec->catfile($test_dir, 'util.pl');
  34. my($hosts, $root_path, $node_path) = zk_test_setup(0);
  35. my $zkh = Net::ZooKeeper->new($hosts);
  36. SKIP: {
  37. skip 'no valid handle', 9 unless (defined($zkh));
  38. my($thread) = threads->new(\&thread_test, $zkh);
  39. SKIP: {
  40. skip 'no valid thread', 3 unless (defined($thread));
  41. my(@ret) = $thread->join;
  42. ok((@ret == 3 and $ret[0]),
  43. 'CLONE_SKIP(): handle reference after spawning thread');
  44. ok((@ret == 3 and $ret[1]),
  45. 'CLONE_SKIP(): scalar handle reference after spawning thread');
  46. ok((@ret == 3 and $ret[2]),
  47. 'CLONE_SKIP(): undef handle reference after spawning thread');
  48. }
  49. my $stat = $zkh->stat();
  50. ($thread) = threads->new(\&thread_test, $stat);
  51. SKIP: {
  52. skip 'no valid thread', 3 unless (defined($thread));
  53. my(@ret) = $thread->join;
  54. ok((@ret == 3 and $ret[0]),
  55. 'stat CLONE_SKIP(): stat handle reference after spawning thread');
  56. ok((@ret == 3 and $ret[1]),
  57. 'stat CLONE_SKIP(): scalar stat handle reference after ' .
  58. 'spawning thread');
  59. ok((@ret == 3 and $ret[2]),
  60. 'stat CLONE_SKIP(): undef stat handle reference after ' .
  61. 'spawning thread');
  62. }
  63. my $watch = $zkh->watch();
  64. ($thread) = threads->new(\&thread_test, $watch);
  65. SKIP: {
  66. skip 'no valid thread', 3 unless (defined($thread));
  67. my(@ret) = $thread->join;
  68. ok((@ret == 3 and $ret[0]),
  69. 'watch CLONE_SKIP(): watch handle reference after spawning thread');
  70. ok((@ret == 3 and $ret[1]),
  71. 'watch CLONE_SKIP(): scalar watch handle reference after ' .
  72. 'spawning thread');
  73. ok((@ret == 3 and $ret[2]),
  74. 'watch CLONE_SKIP(): undef watch handle reference after ' .
  75. 'spawning thread');
  76. }
  77. }
  78. sub thread_test
  79. {
  80. my $zkh = shift;
  81. my @ret;
  82. $ret[0] = ref($zkh) ? 1 : 0;
  83. $ret[1] = ($ret[0] and ref($zkh) eq 'SCALAR') ? 1 : 0;
  84. $ret[2] = ($ret[1] and !defined(${$zkh})) ? 1 : 0;
  85. return @ret;
  86. }