X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F41-clone.t;h=90f15e0fc08fceb517af25711869b441f1862524;hb=fd2b4b28517f7f12044530f6c3ceca07181fba70;hp=e5ae4986d2a10fa7dc655c59e2e269fb7d524be2;hpb=699cf35ef9ecbc8eec8ca35c2c10125ad6ec40e7;p=perl%2Fmodules%2FVariable-Magic.git diff --git a/t/41-clone.t b/t/41-clone.t index e5ae498..90f15e0 100644 --- a/t/41-clone.t +++ b/t/41-clone.t @@ -18,34 +18,46 @@ use threads::shared; use Test::More; -use Variable::Magic qw/wizard cast dispell getdata getsig VMG_THREADSAFE/; +use Variable::Magic qw/wizard cast dispell getdata getsig VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/; if (VMG_THREADSAFE) { - plan tests => 3 + 2 * (2 * 8 + 2) + 2 * (2 * 5 + 2); + plan tests => 2 * 3 + 4 * (2 * 10 + 2) + 4 * (2 * 7 + 2); my $v = $threads::VERSION; diag "Using threads $v" if defined $v; $v = $threads::shared::VERSION; diag "Using threads::shared $v" if defined $v; - diag 'This will leak a few scalars'; } else { plan skip_all => 'This Variable::Magic isn\'t thread safe'; } my $destroyed : shared = 0; my $c : shared = 0; -my $wiz = eval { - wizard get => sub { ++$c }, - data => sub { $_[1] + threads->tid() }, - free => sub { ++$destroyed } -}; -is($@, '', "wizard in main thread doesn't croak"); -isnt($wiz, undef, "wizard in main thread is defined"); -is($c, 0, "wizard in main thread doesn't trigger magic"); -my $sig; +sub spawn_wiz { + my ($op_info) = @_; + + my $wiz = eval { + wizard data => sub { $_[1] + threads->tid() }, + get => sub { ++$c; 0 }, + set => sub { + my $name = $_[-1]; + $name = $name->name if $op_info == VMG_OP_INFO_OBJECT; + my $tid = threads->tid(); + is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct"; + 0 + }, + free => sub { ++$destroyed; 0 }, + op_info => $op_info + }; + is($@, '', "wizard with op_info $op_info in main thread doesn't croak"); + isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined"); + is($c, 0, "wizard with op_info $op_info in main thread doesn't trigger magic"); + + return $wiz; +} sub try { - my ($dispell) = @_; + my ($dispell, $sig) = @_; my $tid = threads->tid(); my $a = 3; my $res = eval { cast $a, $sig, sub { 5 }->() }; @@ -57,37 +69,31 @@ sub try { my $d = eval { getdata $a, $sig }; is($@, '', "getdata in thread $tid doesn't croak"); is($d, 5 + $tid, "getdata in thread $tid returns the right thing"); + eval { $a = 9 }; + is($@, '', "set in thread $tid (check opname) doesn't croak"); if ($dispell) { $res = eval { dispell $a, $sig }; is($@, '', "dispell in thread $tid doesn't croak"); undef $b; eval { $b = $a }; is($@, '', "get in thread $tid after dispell doesn't croak"); - is($b, 3, "get in thread $tid after dispell returns the right thing"); + is($b, 9, "get in thread $tid after dispell returns the right thing"); } return; # Ugly if not here } -for my $dispell (1, 0) { - $c = 0; - $destroyed = 0; - $sig = $wiz; - - my @t = map { threads->create(\&try, $dispell) } 1 .. 2; - $t[0]->join; - $t[1]->join; +my $wiz_name = spawn_wiz VMG_OP_INFO_NAME; +my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT; - is($c, 2, "get triggered twice"); - is($destroyed, (1 - $dispell) * 2, 'destructors'); - - $c = 0; - $destroyed = 0; - $sig = getsig $wiz; +for my $dispell (1, 0) { + for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) { + $c = 0; + $destroyed = 0; - @t = map { threads->create(\&try, $dispell) } 1 .. 2; - $t[0]->join; - $t[1]->join; + my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2; + $_->join for @t; - is($c, 2, "get triggered twice"); - is($destroyed, (1 - $dispell) * 2, 'destructors'); + is($c, 2, "get triggered twice"); + is($destroyed, (1 - $dispell) * 2, 'destructors'); + } }