]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/41-clone.t
Update VPIT::TestHelpers to 3edc6d15
[perl/modules/Variable-Magic.git] / t / 41-clone.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Variable::Magic qw<
7  wizard cast dispell getdata
8  VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT
9 >;
10
11 use lib 't/lib';
12 use VPIT::TestHelpers (
13  threads => [ 'Variable::Magic' => 'Variable::Magic::VMG_THREADSAFE()' ],
14 );
15
16 use Test::Leaner 'no_plan';
17
18 my $destroyed : shared = 0;
19 my $c         : shared = 0;
20
21 sub spawn_wiz {
22  my ($op_info) = @_;
23
24  my $desc = "wizard with op_info $op_info in main thread";
25
26  local $@;
27  my $wiz = eval {
28   wizard(
29    data    => sub { $_[1] + threads->tid() },
30    get     => sub { lock $c; ++$c; 0 },
31    set     => sub {
32     my $op = $_[-1];
33     my $tid = threads->tid();
34
35     if ($op_info == VMG_OP_INFO_OBJECT) {
36      is_deeply { class => ref($op),   name => $op->name },
37                { class => 'B::BINOP', name => 'sassign' },
38                "op object in thread $tid is correct";
39     } else {
40      is $op, 'sassign', "op name in thread $tid is correct";
41     }
42
43     return 0
44    },
45    free    => sub { lock $destroyed; ++$destroyed; 0 },
46    op_info => $op_info,
47   );
48  };
49  is $@,     '',    "$desc doesn't croak";
50  isnt $wiz, undef, "$desc is defined";
51  is $c,     0,     "$desc doesn't trigger magic";
52
53  return $wiz;
54 }
55
56 sub try {
57  my ($dispell, $wiz) = @_;
58  my $tid = threads->tid;
59
60  my $a = 3;
61
62  {
63   local $@;
64   my $res = eval { cast $a, $wiz, sub { 5 }->() };
65   is $@, '', "cast in thread $tid doesn't croak";
66  }
67
68  {
69   local $@;
70   my $b;
71   eval { $b = $a };
72   is $@, '', "get in thread $tid doesn't croak";
73   is $b, 3,  "get in thread $tid returns the right thing";
74  }
75
76  {
77   local $@;
78   my $d = eval { getdata $a, $wiz };
79   is $@, '',       "getdata in thread $tid doesn't croak";
80   is $d, 5 + $tid, "getdata in thread $tid returns the right thing";
81  }
82
83  {
84   local $@;
85   eval { $a = 9 };
86   is $@, '', "set in thread $tid (check opname) doesn't croak";
87  }
88
89  if ($dispell) {
90   {
91    local $@;
92    my $res = eval { dispell $a, $wiz };
93    is $@, '', "dispell in thread $tid doesn't croak";
94   }
95
96   {
97    local $@;
98    my $b;
99    eval { $b = $a };
100    is $@, '', "get in thread $tid after dispell doesn't croak";
101    is $b, 9,  "get in thread $tid after dispell returns the right thing";
102   }
103  }
104
105  return;
106 }
107
108 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
109 my $wiz_obj  = spawn_wiz VMG_OP_INFO_OBJECT;
110
111 for my $dispell (1, 0) {
112  for my $wiz ($wiz_name, $wiz_obj) {
113   {
114    lock $c;
115    $c = 0;
116   }
117   {
118    lock $destroyed;
119    $destroyed = 0;
120   }
121
122   my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2;
123   $_->join for @threads;
124
125   {
126    lock $c;
127    is $c, 2, "get triggered twice";
128   }
129   {
130    lock $destroyed;
131    is $destroyed, (1 - $dispell) * 2, 'destructors';
132   }
133  }
134 }
135
136 {
137  my @threads;
138  my $flag : shared = 0;
139  my $destroyed;
140
141  {
142   my $wiz = wizard(
143    set => sub {
144     my $tid = threads->tid;
145     pass "set callback called in thread $tid"
146    },
147    free => sub { ++$destroyed },
148   );
149
150   my $var = 123;
151   cast $var, $wiz;
152
153   @threads = map spawn(
154    sub {
155     my $tid = threads->tid;
156     my $exp = 456 + $tid;
157     {
158      lock $flag;
159      threads::shared::cond_wait($flag) until $flag;
160     }
161     $var = $exp;
162     is $var, $exp, "\$var could be assigned to in thread $tid";
163    }
164   ), 1 .. 5;
165  }
166
167  is $destroyed, 1, 'wizard is destroyed';
168
169  {
170   lock $flag;
171   $flag = 1;
172   threads::shared::cond_broadcast($flag);
173  }
174
175  $_->join for @threads;
176 }